Question

I am working on magento2 and i am try to access product all custom option. For now i have added to text box and for access custom option bellow code i have use.

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product = $objectManager->get('Magento\Catalog\Model\Product')->load(1001);
        $productOption = $product->getOptions();
        foreach($productOption as $op)
        {
            echo "<pre>";
            print_r($op);
            die;
        }
Was it helpful?

Solution

I have found the solutions.

Here is my code.

class Data extends AbstractHelper
{
    public function __construct(Context $context,
        \Magento\Catalog\Model\ProductRepository $productRepository
    ) {
        $this->_productRepository = $productRepository;
        parent::__construct($context);
    }
    protected function _saveCustomOption($sku, $qty, $code, $_options)
    {

        $product = $this->_productRepository->get(1001);
        $productOption = $product->getOptions();
        foreach($productOption as $op)
        {
            echo "<pre>";
            print_r($op->getData());
            die;
        }
    }
}

I hope this code help to other people.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top