4

I have work on Custom Module. In Our Module I had use

<event name="catalog_product_save_after">
    <observer name="add_custom_option" instance="Namespace\Modulename\Observer\AddCustomOption" />
</event>

In observer i Had Generate Product Custom Option Programmatically

            $product->setHasOptions(1);
            $product->setCanSaveCustomOptions(true);
            foreach ($options as $arrayOption) {
                $isHasOptions = $product->getTypeInstance()->hasOptions($product);

                 $option = \Magento\Framework\App\ObjectManager::getInstance()
                        ->create('\Magento\Catalog\Model\Product\Option')
                        ->setProductId($product->getId())
                        ->setStoreId($product->getStoreId())
                        ->addData($arrayOption);

                $this->customoptionOperation($option, 'save');
                $product->addOption($option);
                //$product->save();
            }

i have use this code for generate Product custom Option.

//$product->save();

i had comment this code because it provide me Error

No Such Entity

After saving Product.

Product Options are generated Successfully in Product But Not Display in Front-end

I had Run All Commands

  • setup:upgrade
  • setup:static-content:deploy -f
  • indexer:reindex
  • cache:flush

But Custom Option Not Appears in Front-end

But When i have save product second time after saving product the Options are Appears in Front-end Without need Run any Commands.

I Need Solution to save Product One time and Get Product Option Front End

Is This Possible ?

1
  • I have also the same problems Commented May 9, 2019 at 5:19

2 Answers 2

1

You can try using a Product Repository Magento\Catalog\Api\ProductCustomOptionRepositoryInterface

Like this:

$product->setHasOptions(1);
$product->setCanSaveCustomOptions(true);
foreach ($options as $arrayOption) {
    $isHasOptions = $product->getTypeInstance()->hasOptions($product);

    $option = \Magento\Framework\App\ObjectManager::getInstance()
                ->create('\Magento\Catalog\Api\ProductCustomOptionRepositoryInterface')
                ->setProductId($product->getId())
                ->setStoreId($product->getStoreId())
                ->addData($arrayOption);

    $this->customoptionOperation($option, 'save');
    $product->addOption($option);
    $product->save();
} 
6
  • it is not working and read My Question $product->save() provide me Error No Such Entity Commented May 9, 2019 at 5:50
  • updated it using ProductCustomOptionRepositoryInterface , using it, save() will work without error
    – fmsthird
    Commented May 9, 2019 at 5:56
  • No it is not working it will provide me Error [ screencast.com/t/Xp8XahLTV ] Commented May 9, 2019 at 7:03
  • which Magento version you are using?
    – fmsthird
    Commented May 9, 2019 at 7:30
  • i have use magento 2.3.1 Commented May 9, 2019 at 7:37
1

Save Product Using ProductRepositoryInterface :-

$option = $this->_objectManager->create(\Magento\Catalog\Model\Product\Option::class)
        ->setProductId($_product->getId())
        ->setStoreId($_product->getStoreId())
        ->addData($arrayOption);
    $option->save();
    $product->addOption($option);
    $productRepo = $this->_objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface');
    $productRepo->save($product);
4
  • try this....... Commented May 9, 2019 at 6:19
  • it will provide Me Error No such Entity [screencast.com/t/hZhbqSSinL] Commented May 9, 2019 at 6:49
  • Page Not Found in your screenshot Commented May 9, 2019 at 6:56
  • TypeError: Argument 1 passed to Magento\Catalog\Model\ProductRepository\Interceptor::save() must implement interface Magento\Catalog\Api\Data\ProductInterface, instance of Magento\Catalog\Model\Product\Option Commented Mar 26, 2022 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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