3

I am trying to get product custom options using next code:

$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $_objectManager->get(‘\Magento\Catalog\Model\Product’)->load($id);
$customOptions = $_objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($product);

but its not working for me. It's breaking the page.

Can you please suggest proper solution for it?

  • Welcome to MSE. As it is 3rd party extension.Then Please contact with theme – Amit Bera Nov 29 '16 at 7:54
  • 2
    It's not third party, the OP just references a tutorial, which was written by an extension vendor yes. – 7ochem Nov 29 '16 at 8:24
  • 1
    @zaptech, I assume that you have followed that tutorial, but could you edit your question and add the exact code YOU have written? Maybe you have done something differently than in the tutorial... – 7ochem Nov 29 '16 at 8:25
  • Looks like quotes have been copied from that tutorial () which causes a parse error. Replace them with ' – Fabian Schmengler Jun 22 '17 at 10:38
3

To achieve your goal you can use the product repository and the product options repository, like this:

public function __construct(
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
    \Magento\Catalog\Api\ProductCustomOptionRepositoryInterface $productOptionRepository
) {
    $productId = 14;
    /** @var \Magento\Catalog\Model\Product $product */
    $product = $productRepository->getById($productId);
    $options = $productOptionRepository->getProductOptions($product);
}

Result:

result

Another way you can load the product and get options directly from it (not better way):

public function __construct(
    \Magento\Catalog\Model\ProductFactory $productFactory
) {
    $productId = 14; // your product id (with options)
    /** @var \Magento\Catalog\Model\Product $product */
    $product = $productFactory->create()->load($productId);
    $options = $product->getOptions();
}

Result:

result in xdebug

Or you can use the product options repository:

public function __construct(
    \Magento\Catalog\Model\ProductFactory $productFactory,
    \Magento\Catalog\Api\ProductCustomOptionRepositoryInterface $productOptionRepository
) {
    $productId = 14;
    /** @var \Magento\Catalog\Model\Product $product */
    $product = $productFactory->create()->load($productId);
    $options = $productOptionRepository->getProductOptions($product);
}

And get the same result:

same result using product options repository

  • Thank you Siarhey, just one more enhance here is, you should use interface, not concrete class public function __construct( \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Catalog\Api\ProductCustomOptionRepositoryInterface $productOptionRepository ) { ... } – Hoang Trinh Jun 29 '17 at 4:43
  • @piavgh Thank you, I have replaced the arguments in the __construct method to more suitable ones. – Siarhey Uchukhlebau Jun 29 '17 at 8:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.