1

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.

4

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

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

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.