How to get Custom Options Programmatically in Magento

前端 未结 7 2191
天涯浪人
天涯浪人 2020-12-09 05:20

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! <

相关标签:
7条回答
  • 2020-12-09 05:37

    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 ); 
                }
    
    0 讨论(0)
  • 2020-12-09 05:39

    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

    0 讨论(0)
  • 2020-12-09 05:41

    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'];
               }
            }
        }
    
    0 讨论(0)
  • 2020-12-09 05:47

    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.

    0 讨论(0)
  • 2020-12-09 05:55

    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 />";
            }
        }
    ?>
    
    0 讨论(0)
  • 2020-12-09 05:57

    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.

    0 讨论(0)
提交回复
热议问题