13

I have a couple products at checkout that I need to be able to get all of the custom options that are selected for them through code.

Any help is much appreciated!

DaveC
  • 1,039
  • 2
  • 9
  • 6
  • There's a lot of ways (built-in, modules, etc.) to get a custom option attached to a product in Magento. If you let the community know how you're adding the options you'll have a better chance of getting an answer. – Alana Storm May 13 '10 at 18:41
  • Just simply having a productid in php and being able to get the custom options that are attached to that product. – DaveC May 13 '10 at 18:54
  • It's the term "custom options" that's causing confusion. How were the custom options added to the product? Through Magento? Through an commercial extension? Through a custom extension? – Alana Storm May 13 '10 at 21:15
  • Just in the backend; I clicked on a product and went to custom options.. then I added two custom options. I need to be able to request these via php code. – DaveC May 13 '10 at 21:53
  • 3
    Dave: I don't have time to try and dig it up now, but have you gone through the core code to find how Magento does it on their product view (or in the admin product edit, custom options tab)? I often find the most useful ways to do things are to try and figure out how Magento does it first, then customize it to work the way I need it to work. – Prattski May 14 '10 at 12:22
  • @Dave - Please mark any correct reply as the answer. – Knowledge Craving Feb 07 '11 at 15:24

7 Answers7

23

I will just give you an example of one product. Let's say that you know the Sku (for example, let it be "ABCDE") of your required product. So you will be able to get the ID of that product.

The code will be somewhat like:-

$productSku = "ABCDE";
$product = Mage::getModel('catalog/product');
$productId = $product->getIdBySku( $productSku );
$product->load($productId);

/**
 * In Magento Models or database schema level, the product's Custom Options are
 * executed & maintained as only "options". So, when checking whether any product has
 * Custom Options or not, we should check by using this method "hasOptions()" only.
 */
if($product->hasOptions()) {
    echo '<pre>';

    foreach ($product->getOptions() as $o) {
        $optionType = $o->getType();
        echo 'Type = '.$optionType;

        if ($optionType == 'drop_down') {
            $values = $o->getValues();

            foreach ($values as $k => $v) {
                print_r($v);
            }
        }
        else {
            print_r($o);
        }
    }

    echo '</pre>';
}

I think this will let you get started.

Depending upon the type of the option in the variable "$optionType", you need to call another nested "foreach" loop. I have worked on text boxes, text fields, drop downs, but not on other types. So I suppose you need to do some more RnD by yourself.

Knowledge Craving
  • 7,955
  • 13
  • 49
  • 92
  • I'm having a problem querying for custom options as well. For a product that variable sizes (Large, Medium, Small), I want to display the options to the user. I posted a related question here: http://stackoverflow.com/questions/5033427/magento-query-for-product-options – John Feb 18 '11 at 17:06
  • Thanks! It works for me :) However I used this in a phtml file and even having the product loaded, there was no options until I loaded it again. Weird, but works! – s3v3n Jul 07 '11 at 10:37
  • @Knowledge Craving, when i try to do this i get an error, can you help please? this is the error: `Fatal error: Call to a member function hasOptions() on a non-object` Thanks a lot! – Abude Mar 18 '13 at 18:21
  • @Jimmy - This error means that the product object has not loaded correctly. Please check whether you've provided correct SKU or ID of the product, when loading it. For verifying this, try `echo $product->getSku();` before the `if` condition. Also please check whether the product you're trying to load is enabled for the current website/store or not. Hope it helps! – Knowledge Craving Mar 19 '13 at 05:10
  • 1
    @KnowledgeCraving Thanks for the answer, i've almost made it work, it's showing but not autosubmitting, like i didn't pick the option, you can see my update code in the qusetion here: http://stackoverflow.com/questions/15479612/how-to-display-cutom-options-dropdown-on-the-product-page-magento Thanks! – Abude Mar 19 '13 at 06:52
7

Please note that

$product->hasCustomOptions()

in "Knowledge Craving"'s solution does always return false (at least in my case, Magento 1.6.2). Therefore the if-condition is never fulfilled and the block below is not executed.

marco birchler
  • 1,566
  • 2
  • 21
  • 45
  • 1
    Thanks a lot for your answer. Yes, I have to admit that from Magento v1.5.x, it didn't work, but I forgot to update this answer. So thanks again for showing me the right way! – Knowledge Craving Jun 10 '12 at 15:30
  • @KnowledgeCraving seems like you forgot to update it once again :D Still don't see any mention of this in your answer... – OZZIE Sep 12 '14 at 14:28
7

For those who want to see selected custom options later in admin panel in Order/Invoice/Shipment/Creditmemo, find files: /app/design/adminhtml/[default]/default/template/sales/order/view/items/renderer/default.phtml
/app/design/adminhtml/[default]/default/template/sales/order/invoice/view/items/renderer/default.phtml /app/design/adminhtml/[default]/default/template/sales/order/shipment/view/items/renderer/default.phtml /app/design/adminhtml/[default]/default/template/sales/order/creditmemo/view/items/renderer/default.phtml PS: I haven't changed configurated.phtml files for invoice/shipment/creditmemo

and insert code somewhere after <?php echo $_item->getSku(); ?></div> and before it's row's closing tag </td> (be careful, it's different for each file)

Insert code:

        <?php  
    //---------start:---------------          
    // if ($allOptions = $_item->_getData('product_options')) {             // only for order item
    if ($allOptions = $_item->getOrderItem()->getData('product_options')) { // for invoice, shipping, creditmemo
        $options = unserialize($allOptions);

        if (isset($options['options'])) { 
            foreach ($options['options'] as $optionValues) {
                if ($optionValues['value']) {
                    echo '&nbsp;<strong><i>'. $optionValues['label'].'</i></strong>: ';

                    $_printValue = isset($optionValues['print_value']) ? $optionValues['print_value'] : strip_tags($optionValues['value']);
                    $values = explode(', ', $_printValue);
                    foreach ($values as $value) {
                        if (is_array($value))
                          foreach ($value as $_value) 
                              echo $_value;
                        else echo $value; 
                    }
                    echo '<br />';
                }
            }    
        }
    }
    //---------end:---------------                  
    ?>        

Also note that in code there is a line (if sentence) that works only in order default.phtml file, and the second if sentence works in invoice/shipping/creditmemo files. It depends where you post the code, make sure the right sentence is commented out.

hope this helps, thanks also to Knowledge Craving whose code helped me quite a bit :-) jazkat

jazkat
  • 5,600
  • 3
  • 25
  • 17
3
    $quote=$observer->getEvent()->getQuote();
    $quoteid=$quote->getId();
    $session= Mage::getSingleton('checkout/session');
    $getotal = Mage::helper('checkout')->getQuote()->getGrandTotal();

    foreach($session->getQuote()->getAllItems() as $item)
        {

         $sellcheck = Mage::getModel('catalog/product')->load($item->getProduct()->getId())->getissellbool();
         $options = Mage::getModel('catalog/product')->load($item->getProduct()->getId())->getProductOptionsCollection();
         foreach ($options as $o) 
             { 
                $title = $o->getTitle();
                $values = $o->getValues();
                foreach($values as $v)
                  {
                     $mydata = $v->getPrice();
                     echo 'options price: ' . $mydata;                      
                        }

               } 

          }

To access product custom options at the shopping cart you can utilise this code.

Chiragit007
  • 1,646
  • 2
  • 16
  • 31
2

I hope it will useful to you for get only Custom Dropdown values in Product page

Just paste the following code in this file at last

app/design/frontend/base/default/template/catalog/product/view/options.phtml

<?php
    $product = Mage::getModel("catalog/product")->load($this->getProduct()->getId()); //product id
    foreach ($product->getOptions() as $_option) {
        $values = $_option->getValues();
        foreach ($values as $v) {
            print_r($v->getTitle());
            echo "<br />";
        }
    }
?>
Naresh
  • 73
  • 3
  • 13
1

We can also solve like that, that can display on checkout page.

 $items = Mage::getModel('checkout/cart')->getQuote()->getAllVisibleItems();
 foreach($items as $product) {
     $options = $product->getProduct()->getTypeInstance(true)->getOrderOptions($product->getProduct());
     if ($options)
     {
        if (isset($options['options']))
        {
           $result = $options['options'];
        }
        if(count($result)>0){
           foreach($result as $key =>$value){
                $resultoption =  $value['value'];
           }
        }
    }
502_Geek
  • 2,046
  • 2
  • 22
  • 32
0

You can try this code in template/checkout/cart/item/default.php:

if($Product->hasOptions)
            {
                $optionsArr = $Product->getOptions();
                 foreach ($optionsArr as  $optionKey => $optionVal)
                {
                          $optStr.= "<select style='display:block; clear:both;' name='options[".$optionVal->getId()."]'>";    
                  foreach($optionVal->getValues() as $valuesKey => $valuesVal)
                    {
                          $optStr.= "<option value='".$valuesVal->getId()."'>".$valuesVal->getTitle()."</option>";
                    }
                    $optStr.= "</select>";
                    }
   echo($optStr ); 
            }
Surya prakash Patel
  • 1,512
  • 2
  • 24
  • 38