I am adding custom option using this code

$product->addCustomOption('custom', $id, $product);

Its save in database but how to display that custom option in cart item in magento 2?

  • No need to add custom code for the custom option and it is a default magento functionality. If you display custom option value and label for the cart page then try this code : $customOptions = $this->helper('Magento\Catalog\Helper\Product\Configuration')->getCustomOptions($_item); – Abdul Mar 30 '16 at 9:09
  • but i have to add that custom option programatically – ND17 Mar 30 '16 at 9:10
  • try this code : $customOptions = $this->helper('Magento\Catalog\Helper\Product\Configuration')->getCustomOptions($_item); – Abdul Mar 30 '16 at 9:11
  • If you added custom options with product in cart then display by default in cart view page. see code in file : magento2\vendor\magento\module-checkout\view\frontend\templates\cart\item\default.phtml – Abdul Mar 30 '16 at 9:14

You can display item option by overriding Magento_Checkout/templates/cart/item/default.phtml and get the custom option from quote item:

$_item = $block->getItem();
<!-- Custome code -->
<?php echo $_item->getOptionByCode('custom')->getValue(); ?>
<!-- Custome code -->
| improve this answer | |
  • We cannot write the php code on html template and seem that the path is wrong. – Khoa TruongDinh Aug 6 '16 at 15:33
  • You are right, I wrote a wrong file extension. The correct path is Magento_Checkout/templates/cart/item/default.phtml, I edited the answer. – Milko Lorinkov Aug 8 '16 at 3:36

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.