Skip to main content
New
Stack Overflow Jobs powered by Indeed: A job site that puts thousands of tech jobs at your fingertips (U.S. only). Search jobs
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.3k
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
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.5k
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,566
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
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
12 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
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
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,613
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.9k
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,332
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,692
9 votes

magento2 send email programmatically without template

Send email programmatically without email template, code is tested in Magento 2.4 /** * @var \Magento\Framework\Mail\MessageInterfaceFactory */ protected $messageInterfaceFactory; /** * @var \...
Avinash Sonune'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

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
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,586
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
  • 4,004
8 votes

Magento2 how to add configurable product to cart programmatically

Process of adding configurable product to cart //Get cart object $cart = $this->_objectManager->get('Magento\Checkout\Model\Cart'); // Load Configurable Product $productId = 10;//configurable ...
satya prakash patel's user avatar
7 votes

Programatically update customer address by its entity id

Replace this $address->setData($addressCode, $addressData[$addressValue]); with $address->setData($addressCode, $addressValue); <?php $addressId = '5'; $addressData = array( ...
Banna Online's user avatar
  • 1,069

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