Question

I have an category of Watches and in this category there is more then 2500 Products now I want to add Custom Option on each Product for that I was following This link so when I write script for it. it was working fine on one product but when I Run the script for the whole category then Custom Option was not added in any single product. While when I Break the loop after only one Iteration then again works for only one product my script is

<?php
use Magento\Framework\App\Bootstrap;
 
require __DIR__ . '/app/bootstrap.php';
 
$params = $_SERVER;
 
$bootstrap = Bootstrap::create(BP, $params);
 
$obj = $bootstrap->getObjectManager();
 
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
 
$category = $obj->get('\Magento\Catalog\Model\CategoryFactory')->create()->load(66);
$optionFactory = $obj->get('\Magento\Catalog\Model\Product\Option');
$productagain = $obj->get('\Magento\Catalog\Model\Product'); 
$productrepo = $obj->get('\Magento\Catalog\Api\ProductRepositoryInterface');
echo $category->getName();
$catproducts = $category->getProductCollection()->addAttributeToSelect('*');
$inputbox =array(
                "sort_order"    => 1,
                "title"         => "Weist Size (in cm)",
                "price_type"    => "fixed",
                "price"         => "",
                "type"          => "field",
                "is_require"    => 1
            );

foreach ($catproducts as $_product){
    echo $_product->getSku()."<br>";
    echo $_product->getId()."<br>";
    echo $_product->getStoreId()."<br>";
        $option = $optionFactory
                    ->setProductId($_product->getId())
                    ->setStoreId($_product->getStoreId())
                    ->addData($inputbox);
            $option->save();
            echo"Working";
            $_product->addOption($option);
            $productrepo->save($_product);
            //break;
}
?>

In Simple Words When I iterate through products and try to add custom options it fails but the same code works for single product. Thanks

Was it helpful?

Solution

Try to use Option factory

$obj->create('\Magento\Catalog\Model\Product\OptionFactory')->create()
 ->setProductId($_product->getId())
 ->setStoreId($_product->getStoreId())
 ->addData($inputbox);
$option->save();

Hope it would help :)

OTHER TIPS

Please try below code. Hope it will work. What i did is just moved $productrepo line inside loop.

<?php
use Magento\Framework\App\Bootstrap;
 
require __DIR__ . '/app/bootstrap.php';
 
$params = $_SERVER;
 
$bootstrap = Bootstrap::create(BP, $params);
 
$obj = $bootstrap->getObjectManager();
 
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
 
$category = $obj->get('\Magento\Catalog\Model\CategoryFactory')->create()->load(66);
$optionFactory = $obj->get('\Magento\Catalog\Model\Product\Option');
$productagain = $obj->get('\Magento\Catalog\Model\Product'); 
echo $category->getName();
$catproducts = $category->getProductCollection()->addAttributeToSelect('*');
$inputbox =array(
                "sort_order"    => 1,
                "title"         => "Weist Size (in cm)",
                "price_type"    => "fixed",
                "price"         => "",
                "type"          => "field",
                "is_require"    => 1
            );

foreach ($catproducts as $_product){
    $productrepo = $obj->get('\Magento\Catalog\Api\ProductRepositoryInterface');
    echo $_product->getSku()."<br>";
    echo $_product->getId()."<br>";
    echo $_product->getStoreId()."<br>";
        $option = $optionFactory
                    ->setProductId($_product->getId())
                    ->setStoreId($_product->getStoreId())
                    ->addData($inputbox);
            $option->save();
            echo"Working";
            $_product->addOption($option);
            $productrepo->save($_product);
            //break;
}
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top