2

When i execute follow code

    $options = [
    [
        'title' => 'drop_down option',
        'type' => 'drop_down',
        'is_require' => true,
        'sort_order' => 4,
        'values' => [
            [
                'title' => 'drop_down option 1',
                'price' => 10,
                'price_type' => 'fixed',
                'sku' => 'drop_down option 1 sku',
                'sort_order' => 1,
            ],
            [
                'title' => 'drop_down option 2',
                'price' => 20,
                'price_type' => 'fixed',
                'sku' => 'drop_down option 2 sku',
                'sort_order' => 2,
            ],
        ],
    ]
];

foreach ($options as $arrayOption) {
    $option = $this->objectManager->create(\Magento\Catalog\Model\Product\Option::class)
        ->setProductId($_product->getId())
        ->setStoreId($_product->getStoreId())
        ->addData($arrayOption);
    $option->save();
    $_product->addOption($option);
    $_product->save();
}
  • Custom Option not appear on product frontend page.
  • When goto to product page from admin page and click save without any changing.

  • Then goto frontend product page custom show up.

I want create option programmatically and should shown on front page without saving product from admin panel.

2 Answers 2

7

Try following way:


$_product = $this->_objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface')->getById(1, true);
$options = [
    [
        'title' => 'drop_down option',
        'type' => 'drop_down',
        'is_require' => true,
        'sort_order' => 4,
        'values' => [
            [
                'title' => 'drop_down option 1',
                'price' => 10,
                'price_type' => 'fixed',
                'sku' => 'drop_down option 1 sku',
                'sort_order' => 1,
            ],
            [
                'title' => 'drop_down option 2',
                'price' => 20,
                'price_type' => 'fixed',
                'sku' => 'drop_down option 2 sku',
                'sort_order' => 2,
            ],
        ],
    ]
];

foreach ($options as $arrayOption) {
    $option = $this->_objectManager->create(\Magento\Catalog\Model\Product\Option::class)
        ->setProductId($_product->getId())
        ->setStoreId($_product->getStoreId())
        ->addData($arrayOption);
    $option->save();
    $_product->addOption($option);
    $this->_objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface')->save($_product);
}

Note: Try to use DI instead of $this->_objectManager

9
  • its not working please see the image link ibb.co/diBFNd Jun 2, 2018 at 13:51
  • @ArshadMuhammad I have tested M 2.3, it's working fine. Did you used exactly my code?
    – Sohel Rana
    Jun 2, 2018 at 13:59
  • when i use your code separate its working but with my code its not working again same result showing. Jun 2, 2018 at 15:03
  • 1
    For saving product use repository instead of product factory.
    – Sohel Rana
    Jun 2, 2018 at 15:05
  • $this->_objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface')->save($_product);
    – Sohel Rana
    Jun 2, 2018 at 15:05
0

I had a similar issue. I added a custom drop_down option to a product programmatically. It appeared in the admin, but not on the frontend until saving the record again in the admin manually.

The issue was that catalog_product_entity did not have has_options set to 1. Setting that via $product->setData('has_options',1) fixes that issue.

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.