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

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();
} 
  • it is not working and read My Question $product->save() provide me Error No Such Entity – Bhavesh Prajapati May 9 at 5:50
  • updated it using ProductCustomOptionRepositoryInterface , using it, save() will work without error – fmsthird May 9 at 5:56
  • No it is not working it will provide me Error [ screencast.com/t/Xp8XahLTV ] – Bhavesh Prajapati May 9 at 7:03
  • which Magento version you are using? – fmsthird May 9 at 7:30
  • i have use magento 2.3.1 – Bhavesh Prajapati May 9 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);

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.