How to get Custom Options Programmatically in Magento

前端 未结 7 2193
天涯浪人
天涯浪人 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: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 getSku(); ?>

    and before it's row's closing tag (be careful, it's different for each file)

    Insert code:

            _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 ' '. $optionValues['label'].': ';
    
                        $_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 '
    '; } } } } //---------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

提交回复
热议问题