40 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 ...
Giel Berkers's user avatar
  • 12.2k
38 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\...
Navarr's user avatar
  • 1,712
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 = ...
Amit Maurya's user avatar
  • 2,861
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 $...
Marius's user avatar
  • 198k
25 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\...
Amit Bera's user avatar
  • 77.2k
22 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( \...
spiilmusic's user avatar
15 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->...
Vladimir Kerkhoff's user avatar
15 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 */ ...
Mageworx's user avatar
  • 3,786
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 ...
Raphael at Digital Pianism's user avatar
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\...
Nagaraju Kasa's user avatar
13 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 = $...
Igor Melnykov's 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' => $...
Marius's user avatar
  • 198k
12 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\...
Rizwan Dhuka's 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 //...
Khoa TruongDinh's 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]); /** ...
Dharmendra Jadav's user avatar
11 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(); $...
Niraj Patel's 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 \...
KAndy's user avatar
  • 20.7k
10 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 ...
Dexter's user avatar
  • 3,603
10 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 ...
Hassan Ali Shahzad's user avatar
10 votes
Accepted

magento2 send email programmatically without template

Magento 2: The simplest way I found to send a plain text - use Zend1 mail $to = ['[email protected]', '[email protected]']; $email = new \Zend_Mail(); $email->setSubject("Feedback email"); ...
Ravi Soni's user avatar
  • 1,809
10 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 ...
Ronak Rathod's user avatar
  • 6,249
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 ...
Nolwennig's user avatar
  • 521
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\...
Priyank's user avatar
  • 7,632
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' => ...
Kevin Chavez's 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 ...
cyk's user avatar
  • 293
8 votes

Programmatically create a simple product in magento 2

use \Magento\Framework\App\Bootstrap; include('app/bootstrap.php'); // add bootstrap $bootstraps = Bootstrap::create(BP, $_SERVER); $object_Manager = $bootstraps->getObjectManager(); $app_state = ...
Dhaval Dave's user avatar
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 ...
user3748173's 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\...
Arjun's user avatar
  • 3,546
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/...
Jaimin Sutariya's 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="...
sandip's user avatar
  • 3,924

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