29

Try it like this: ->addForeignKey( $installer->getFkName('namespace_module/shop', 'area_id', 'namespace_module/area','area_id'), 'area_id', $installer->getTable('namespace_module/area'), 'area_id', Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE ) The difference in my code is that I ...


26

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 longer the correct way to handle this, because $product->save() is deprecated since Magento 2.1. The correct way to do this is by using the ...


26

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 = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $state = $objectManager->get('Magento\Framework\App\State'); $state->...


21

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\CatalogInventory\Api\StockRegistryInterface. The first is generated, so don't get too concerned if it shows up as not existing in your IDE. public function ...


18

add this to the your class: private $blockFactory; and make your constructor look like this: public function __construct( PageFactory $pageFactory, \Magento\Cms\Model\BlockFactory $blockFactory ) { $this->pageFactory = $pageFactory; $this->blockFactory = $blockFactory; } you can even remove the $pageFactory dependency if you don't ...


13

You can try the following solution: Add 2 classes to the constructor: /** * @var \Magento\Cms\Model\BlockFactory */ protected $blockFactory; /** * @var \Magento\Cms\Model\BlockRepository */ protected $blockRepository; /** * @param PageFactory $resultPageFactory * @param \Magento\Cms\Model\BlockFactory $blockFactory * @param \Magento\Cms\Model\...


12

Load is pretty expensive memory wise. You can use this for a faster update: Mage::getSingleton('catalog/product_action')->updateAttributes( array($productId), array('name' => $storeSpecificName), $storeId );


11

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( \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry ) { $this->stockRegistry = $stockRegistry; parent::__construct(); } public ...


10

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 //Magento\Catalog\Api\Data\ProductLinkInterface It should like this: /** @var \Magento\Catalog\Api\Data\ProductLinkInterfaceFactory $productLinks **/ $linkData =...


9

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 this: protected $_wishlistRepository; protected $_productRepository; public function __construct( ... \Magento\Wishlist\Model\WishlistFactory $...


9

use \Magento\Framework\App\Bootstrap; include('../app/bootstrap.php'); $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $url = \Magento\Framework\App\ObjectManager::getInstance(); $storeManager = $url->get('\Magento\Store\Model\StoreManagerInterface'); $mediaurl= $storeManager->getStore()->getBaseUrl(...


8

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 = $this->objectManager->create('Magento\Catalog\Api\Data\ProductCustomOptionInterface'); $customOption->setTitle('Text') ->setType('area') -&...


8

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 will explain it below. First of all I was trying to add a product on event checkout_cart_product_add_after. This event is fired when product is added to the ...


8

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]); /** * Add attributes to the eav/attribute */ $eavSetup->addAttribute( \Magento\Catalog\Model\Product::ENTITY, 'shop_by_brand', [ ...


8

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/createSimpleProductAndRedirect", array('name' => $_product->getName())); ?>">Create product</a> </button> Update your function ...


7

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' => $quantity, 'is_in_stock' => 1]); and it started working for me. I still am setting the $product->setStockData too if you edit a product and inspect element ...


7

You can create user using userFactory /** * User model factory * * @var \Magento\User\Model\UserFactory */ protected $_userFactory; public function __construct( \Magento\User\Model\UserFactory $userFactory, ) { $this->_userFactory = $userFactory; } public function execute(){ $adminInfo = [ 'username' => 'killer', ...


7

protected $formKey; protected $cart; protected $product; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\Data\Form\FormKey $formKey, \Magento\Checkout\Model\Cart $cart, \Magento\Catalog\Model\Product $product, array $data = []) { $this->formKey = $formKey; $this->cart = $cart; $this->...


7

Using Magento\Customer\Api\AddressRepositoryInterface; you can create customer address. namespace {NameSpace}; /** * Description of Address */ class Address { /** * @var Magento\Customer\Api\Data\AddressInterfaceFactory */ protected $addressDataFactory; /** * @var \Magento\Customer\Api\AddressRepositoryInterface */ protected $addressRepository; ...


6

I believe the simplest way to do this is to use the product's "duplicate" method. You'll have to reload the duplicated product, should you need then to change any of it's properties and save again: // assume $product is a loaded product to duplicate $duplicated = $product->duplicate(); // re-load the duplicated product to set it's SKU $duplicated = $...


6

I assume you have this piece of xml in the config.xml file inside the <global> tag <resources> <[module]_setup> <setup> <module>Company_Marketplace</module> </setup> </[module]_setup> </resources> You need to tell magento to use the ...


6

Found it! Thanks to https://magento.stackexchange.com/a/90342/4090 $_product = Mage::getModel('catalog/product')->setStoreId($storeId)->load($productId); $_product->setName($storeSpecificName); $_product->getResource()->saveAttribute($_product, 'name');


6

Take a look at Mage_Core_Model_Url_Rewrite class. This class provides a loadByRequestPath($path) method which seems to be what you need So, code would be... //$path is your request_path $rewrite = Mage::getModel('core/url_rewrite')->loadByRequestPath($path); if ($rewrite->getId()){ // rewrite exists $rewrite->delete(); } Other way could ...


6

use \Magento\Framework\App\Bootstrap; include('app/bootstrap.php'); // add bootstrap $bootstraps = Bootstrap::create(BP, $_SERVER); $object_Manager = $bootstraps->getObjectManager(); $app_state = $object_Manager->get('\Magento\Framework\App\State'); $app_state->setAreaCode('frontend'); // get date $today_date = date("m/d/Y"); $added_date = date('...


6

You can try with: /** @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $_productCollectionFactory **/ $this->_productCollectionFactory->create() ->addAttributeToSelect('*') ->addAttributeToFilter('status', \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED); Need to inject \Magento\Catalog\Model\...


6

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="A" --admin-email="admin@admin.com" --admin-password="admin@5252"


6

Here is an example how to insert cms data in m2 Here is an example how to update cms data in m2 Now you can try following way for insert cms data: public function __construct( \Magento\Cms\Model\PageFactory $pageFactory ) { $this->pageFactory = $pageFactory; } And now: $cmsPage = [ 'title' => 'TEST TITLE', 'identifier' => 'test-content-1'...


5

if you want update shipping address of an order then you need load order then load it address object: Step1:Order load by orded id $order = Mage::getModel('sales/order')->load($orderId); Step2:Get shipping address Id. $order->getShippingAddress()->getId(); Step3:Then reset shipping address object: $shippingAddress = Mage::getModel('sales/...


5

The following code worked perfectly for me to create Grouped product with it's associate products. Hope this will help someone to create grouped products programmatically. public function createGroupedProduct() { $sku = 'AL108'; $title = 'my test product'; $description = 'this is a description about the product...'; $...


5

Replace this $address->setData($addressCode, $addressData[$addressValue]); with $address->setData($addressCode, $addressValue); <?php $addressId = '5'; $addressData = array( 'postal_code' => 123456, 'country' => 'US' ); $address = Mage::getModel('customer/address')->load($addressId); $address->...


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