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 ...
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\...
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 = ...
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 $...
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\...
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(
\...
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->...
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
*/
...
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 ...
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\...
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 = $...
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' => $...
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\...
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 //...
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]);
/**
...
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();
$...
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 \...
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 ...
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 ...
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");
...
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 ...
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 ...
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\...
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' => ...
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 ...
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 = ...
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 ...
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\...
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/...
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="...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
programmatically × 394magento2 × 166
magento-1.9 × 79
product × 47
orders × 42
category × 23
addtocart × 23
import × 19
magento2.3 × 18
cart × 17
quote × 17
php × 16
product-attribute × 16
custom-options × 16
configurable-product × 15
database × 14
magento-1.7 × 12
magento2.2 × 11
magento-1 × 11
product-images × 11
magento-2.1 × 10
admin × 10
event-observer × 10
sales-order × 10
price × 9