1

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

  • you used $again->getId() and $again->getStoreId() inside the loop, it seems should be $_product->getId() and $_product->getStoreId() instead – L.C. Echo Chan Sep 10 at 5:21
  • sorry it was coding mistake I set it but in this scenario same thing happening – Asad Khan Sep 10 at 5:27
1

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 :)

| improve this answer | |
  • Thank you so much Sir this was the issue – Asad Khan Sep 10 at 7:14
0

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;
}
?>
| improve this answer | |
  • Thank you Sir for your response but the same scenario is happening it works only for one product... for full loop it doesnt work I'm not getting that why this is not working – Asad Khan Sep 10 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.