3

I have custom option in my product i want to show selected custom option with price in order. i have used below code to get all custom option

foreach ($sales_model->getAllItems() as $item) {

     $opts = $item->getProductOptions();


}

when print option array it will give array without price.so how can i get option price.

[options] => Array
    (
        [0] => Array
            (
                [label] => Tour Date
                [value] => 10/04/2014
                [print_value] => 10/04/2014
                [option_id] => 3433
                [option_type] => date
                [option_value] => 2014-04-10 00:00:00
                [custom_view] => 
            )

        [1] => Array
            (
                [label] => Adult
                [value] => 4
                [print_value] => 4
                [option_id] => 3434
                [option_type] => drop_down
                [option_value] => 14224
                [custom_view] => 
            )

        [2] => Array
            (
                [label] => Adult - Cruise + Lunch
                [value] => Yes
                [print_value] => Yes
                [option_id] => 3435
                [option_type] => checkbox
                [option_value] => 14231
                [custom_view] => 
            )

any help would be much appreciate.

2

The problem is that sales item contains information about selected option but not about option price.

So to get the price you need first:

  • get product object,
  • after get custom option collection(which contains all product custom options with prices).
  • get product custom option from collection by option id and get its price.

Try this:

foreach ($sales_model->getAllItems() as $item) {
     $selectedOpts = $item->getProductOptions();
     $productOpsCollection = Mage::getModel('catalog/product')->load($item->getProduct()->getId())->getProductOptionsCollection();
     foreach ($selectedOpts as $selectedOpt) {
         $optPrice = $productOpsCollection->getItemById($selectedOpt['option_id'])->getPrice();
     }
}
3
  • 1
    it gives error Fatal error: Call to a member function getPrice() on a non-object – Zaheerabbas Apr 7 '14 at 4:15
  • Then check what values do you have in $productOpsCollection and if option with $selectedOpt['option_id'] option id exist in collection. – oleksii.svarychevskyi Apr 7 '14 at 7:14
  • Now i get price for Date option only it still doesn't print price for dropdown or checkbox type. – Zaheerabbas Apr 7 '14 at 9:32
1

After hours of trying I got this way:

$quote = Mage::helper('checkout/cart')->getCart()->getQuote();
    $finalPrice = 0;
    foreach ($quote->getItemsCollection() as $item){
        if ($optionIds = $item->getProduct()->getCustomOption('option_ids')) {
            foreach (explode(',', $optionIds->getValue()) as $optionId) {
                if ($option = $item->getProduct()->getOptionById($optionId)) {
                    $confItemOption = $item->getProduct()->getCustomOption('option_'.$option->getId());

                    $group = $option->groupFactory($option->getType())
                        ->setOption($option)
                        ->setConfigurationItemOption($confItemOption);
                    $finalPrice += $group->getOptionPrice($confItemOption->getValue(), 0);
                }
            }
        }
    }
0

Open up your template file

app/design/frontend/default/theme/template/checkout/cart/item/default.phtml

Find the foreach loop:

<?php foreach ($_options as $_option) : ?>

Within that foreach loop, add this code:

<?php

        $optionId = $_option['option_id'];
        $product = $_item->getProduct();
        $option = $product->getOptionById($optionId);
        $itemOption = $_item->getOptionByCode('option_' . $option->getId());
        $price = false;
        foreach ($option->getValues() as $values) {
            if ($values->getId() == $itemOption['value']) {
                $price = $values->price;
            }
        }
        if ($price !== false) {
            echo Mage::helper('core')->currency($price);
        }

?>

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.