2

I have added a custom option in product from the back-end.


enter image description here

After that when I tried to update the product programmatically but Facing an issue: When I edited the product all the custom options of product has been deleted.


enter image description here

It removes all options while editing a product. I have checked all entries in the database that's also get deleted.

2

Finally I found the solution of this. Not great solution but it's working fine for me.

I have first getting all options of the product before save.

protected function getProductOptions($productObj)
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customOptions = $objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($productObj);

        $options = [];
        if($customOptions->count() > 0) {
            foreach($customOptions as $customOption) {

                $optionsValue = [];
                $customOptionValues = $objectManager->get('Magento\Catalog\Model\Product\Option\Value')->getValuesCollection($customOption);
                if($customOptionValues->count() > 0) {
                    foreach($customOptionValues as $customOptionValue) {
                        $optionsValue[] = array(
                            'record_id' => $customOptionValue->getRecordId(),
                            'title' => $customOptionValue->getTitle(),
                            'price' => $customOptionValue->getPrice(),
                            'price_type' => $customOptionValue->getPriceType(),
                            'sort_order' => $customOptionValue->getSortOrder(),
                            'sku' => $customOptionValue->getSku(),
                            'is_delete' => 0,
                        );                
                    }
                }


                $sku = $customOption->getSku();
                $title = $customOption->getTitle();
                $type = $customOption->getType();
                $price = $customOption->getPrice();
                $price_type = $customOption->getPriceType();
                $record_id = $customOption->getRecordId();

                $options[] = array(
                    'sort_order' => $customOption->getSortOrder(),
                    'title' => $title,
                    'price_type' => $price_type,
                    'price' => $price,
                    'type' => $type,
                    'values' => $optionsValue,
                    'is_require' => 1,
                );

            }
        }

        return $options;        
    }

After that I have added that all options after save the product.

protected function addProductCustomOptions($product, $productOption) 
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

        $product->setHasOptions(1);
        $product->setCanSaveCustomOptions(true);
        foreach ($productOption as $arrayOption) {
            $option = $objectManager->create('\Magento\Catalog\Model\Product\Option')
                    ->setProductId($product->getId())
                    ->setStoreId($product->getStoreId())
                    ->addData($arrayOption);
            $option->save();
            $product->addOption($option);
        }

    }

Now I am getting my all product options same as before.

  • How you manage products option while you are trying to import new products with custom options? Your code is working fine when there are already products available with custom options. – Indian Nov 17 '18 at 10:03

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.