37 votes

Magento 2: Programmatically Updating Inventory

This works for me: $item = ['qty' => 11]; // For example $product->setStockData(['qty' => $item['qty'], 'is_in_stock' => $item['qty'] > 0]); $product->save(); Edit: This is no ...
user avatar
  • 11.6k
37 votes

Programmatically create a simple product in magento 2

First, in your constructor you'll want to include three classes for dependency injection: Magento\Catalog\Api\Data\ProductInterfaceFactory, Magento\Catalog\Api\ProductRepositoryInterface and Magento\...
user avatar
  • 1,642
30 votes
Accepted

How to add a foreign key to a table programmatically?

Try it like this: ->addForeignKey( $installer->getFkName('namespace_module/shop', 'area_id', 'namespace_module/area','area_id'), 'area_id', $installer->getTable('...
user avatar
  • 195k
28 votes
Accepted

Programmatically create a simple product in magento 2

Here, I found the solutions to create a product programmatically via custom php file i.e. test.php . <?php use Magento\Framework\App\Bootstrap; include('app/bootstrap.php'); $bootstrap = ...
user avatar
26 votes
Accepted

How to add a CMS block programmatically in Magento 2?

add this to the your class: private $blockFactory; and make your constructor look like this: public function __construct( PageFactory $pageFactory, \Magento\Cms\Model\BlockFactory $...
user avatar
  • 195k
24 votes

Magento 2: How to add customer address programmatically?

Using Magento\Customer\Api\AddressRepositoryInterface; you can create customer address. namespace {NameSpace}; /** * Description of Address */ class Address { /** * @var Magento\Customer\Api\...
user avatar
  • 75.7k
20 votes

Magento 2: Programmatically Updating Inventory

If you are using the @giel-berkers solution, you may need to set isInStock also, because it will not be automatically sets. So, the following code was useful for me: public function __construct( \...
user avatar
14 votes
Accepted

How to add programmatically product to wishlist in Magento 2?

To do so, you will first have to inject the following in your constructor: Magento\Wishlist\Model\WishlistFactory Magento\Catalog\Api\ProductRepositoryInterface So update your class constructor like ...
user avatar
14 votes

How to add a CMS block programmatically in Magento 2?

You can try the following solution: Add 2 classes to the constructor: /** * @var \Magento\Cms\Model\BlockFactory */ protected $blockFactory; /** * @var \Magento\Cms\Model\BlockRepository */ ...
user avatar
  • 3,728
14 votes
Accepted

Magento 2 programatically add product to cart?

protected $formKey; protected $cart; protected $product; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\Data\Form\FormKey $formKey, \Magento\...
user avatar
12 votes
Accepted

Programatically update a single attribute in Store View Scope

Load is pretty expensive memory wise. You can use this for a faster update: Mage::getSingleton('catalog/product_action')->updateAttributes( array($productId), array('name' => $...
user avatar
  • 195k
12 votes

Magento 2 - Change customer password programmatically

The save method of the class Magento\Customer\Model\ResourceModel\CustomerRepository has an extra argument passwordHash Use this argument to set a new password: $customer = $this->...
user avatar
11 votes
Accepted

Magento2 programmatically adding a custom option

You don't need to specify values for the option type you are trying to add. Please try example below. /** @var \Magento\Catalog\Api\Data\ProductCustomOptionInterface $customOption */ $customOption = $...
user avatar
11 votes
Accepted

set related/upsell products programatically only works for 1 product

This part caused your issue, if you created this object outsize the foreach loop, that means it is a "global" object. So, we need to create a new object inside the loop. $linkData = $productLinks //...
user avatar
11 votes

Magento 2 how to add product attribute of type textarea with editor programmatically

I have use this script to add custom attribute in products. It's working fine for me. /** @var EavSetup $eavSetup */ $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); /** ...
user avatar
10 votes
Accepted

Create a bundle product programmatically - Magento 2

You code should like: /** @var $objectManager \Magento\TestFramework\ObjectManager */ $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager /** @var \...
user avatar
  • 20.2k
10 votes
Accepted

How to retrieve custom attribute set ID by name

You can retrieve attribute set ID by using following code: protected $_attributeSetCollection; public function __construct( ... ,\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\...
user avatar
9 votes
Accepted

Magento2 - How to add a product into cart programatically when checkout_cart_product_add_after is fired

For all those who might waste their day in future, please note below answer which will be helpful to you. The above code to add product into cart works fine. However the problem is with the logic. I ...
user avatar
  • 3,554
9 votes

How to retrieve custom attribute set ID by name

Based on Rizwan Dhuka answer, you can: reduce response size with select 'attribute_set_id' instead of '*' avoid a loop on the object with combine getFirstItem and toArray methods namespace ...
user avatar
  • 503
9 votes

Create an admin user programmatically in Magento 2.0

You can create user using userFactory /** * User model factory * * @var \Magento\User\Model\UserFactory */ protected $_userFactory; public function __construct( \Magento\User\Model\...
user avatar
  • 7,342
9 votes
Accepted

How to create directory and text file and write content in file in media folder programmatically in Magento 2?

Finally, I got a solution for creating files and contents programmatically in Magento 2. Here is a code <?php try { $om = \Magento\Framework\App\ObjectManager::getInstance(); $filesystem = $...
user avatar
9 votes
Accepted

magento2 send email programmatically without template

Magento 2: The simplest way I found to send a plain text - use Zend1 mail $to = ['email1@test.com', 'email2@test.com']; $email = new \Zend_Mail(); $email->setSubject("Feedback email"); ...
user avatar
  • 1,779
9 votes
Accepted

What does "setIsSuperMode" do on Quote Object in Magento 2?

Reference Link The quote is set into super mode in app/code/core/Mage/Adminhtml/Model/Session/Quote.php: $this->_quote->setIsSuperMode(true); Essentially it is a flag on the quote that this ...
user avatar
  • 5,596
8 votes

Magento 2: Programmatically Updating Inventory

I struggled with this same issue. While debugging I found that the product data has a quantity_and_stock_status array, so I tried setting it with: $product->setQuantityAndStockStatus(['qty' => ...
user avatar
8 votes

Magento 2: Programmatically Updating Inventory

One thing the other answers missed is that if you setQty($qty), it will apply the exact value you provide. But if a sale was made for that product a moment before your save, the original qty could ...
user avatar
  • 263
8 votes
Accepted

Magento 2 - Set simple product Color when creating product programmatically

$attr = $_product->getResource()->getAttribute('color'); $avid = $attr->getSource()->getOptionId('Blue'); //name in Default Store View $_product->setData('color', $avid); I found the answer from here ...
user avatar
8 votes
Accepted

magento 2 create categories programmatically

use \Magento\Framework\App\Bootstrap; include('../app/bootstrap.php'); $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $url = \Magento\Framework\App\...
user avatar
  • 3,446
8 votes
Accepted

Create Product Programatically with same Product name

Here is what you can do. Update your button code to pass current product name to your controller action like below. <button> <a href="<?php echo Mage::getUrl("example/amasty/...
user avatar
8 votes

Create an admin user programmatically in Magento 2.0

You can create the admin user by running the following command via SSH on the root directory level. php bin/magento admin:user:create --admin-user="admin" --admin-firstname="Admin" --admin-lastname="...
user avatar
  • 3,582
8 votes
Accepted

Add shipping method programmaticaly

Need to add following dependency in constructor: \Magento\Quote\Model\Quote\Address\Rate $shippingRate and then use the following code: $quote=$this->quote->create(); //Create object of ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible