0

I am checking all the products in the cart, as if any of them contain custom options.

$items = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
    foreach ($items as $item) {
       echo $item->getId(); //prints ids eg. 427 429
       $_product = Mage::getModel('catalog/product')->load($item->getId());
       if($_product->hasCustomOptions()){
              echo 'has custom option'; //doesn't print even the product has custom options
         }
     }

But the code above doesn't print "has option" even the product has. How to know if the product has custom option or not regardless of product type ?

  • I reckon you don't need to load the product here: you can try $item->getProduct()->hasCustomOptions() ? – Raphael at Digital Pianism Mar 21 '16 at 10:30
  • @RaphaelatDigitalPianism , it printed. but it also printed for the product with configurable options. I want for only products with custom option. – Suman K.C Mar 21 '16 at 10:54
0

You have doing wrong.

It should be $item->getProductId() from $item->getId().

$item->getId() give u cart item id basically it given sales_quote_item table' primary key field value and getProductId() give you product id of item which you are cart.

So try this:

 $_product = Mage::getModel('catalog/product')->load($item->getProductId());
       if($_product->hasCustomOptions()){
              echo 'has custom option'; //doesn't print even the product has custom options
         }
| improve this answer | |
  • It print's nothing. Rather $item->getProduct()->hasCustomOptions() prints but it's printing for both custom products as well as configurable products without custom options. ? – Suman K.C Mar 21 '16 at 11:04
  • you have tried wrong,see the update – Amit Bera Mar 21 '16 at 11:07
  • yeah, tried that exactly. foreach ($items as $item) { $_product = Mage::getModel('catalog/product')->load($item->getProductId()); if($_product->hasCustomOptions()){ echo 'has custom option'; } } No result – Suman K.C Mar 21 '16 at 11:09
  • is this given result or not? – Amit Bera Mar 21 '16 at 11:10
  • No result. prints nothing. – Suman K.C Mar 21 '16 at 11:11
0

Use below code:

   $items = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
            foreach ($items as $item) {          
               $_product = Mage::getModel('catalog/product')->load($item->getProductId());
              $options = Mage::getSingleton('catalog/product_option')->getProductOptionCollection($_product);
$optionsSize = $options->getSize();
                  if($optionsSize){
                   echo 'has custom option'; //doesn't print even the product has custom options
                  }                           

            }
| improve this answer | |
  • I don't have to check for product type. so i guess it's same. – Suman K.C Mar 21 '16 at 11:19
  • Check updated answer.May be helpful to you – Prashant Valanda Mar 21 '16 at 11:22
  • 2
    Please do not use this code in production. Loading objects in loops & collections is extremely bad code and will slow down your page load considerably. – Mayers Mar 21 '16 at 11:42

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.