Magento – Order Item get price of selected custom option

custom-optionsexportmagento-1orders

I've in my order a simple product with a custom option of type radio for choose

Product price 100
() no installation service
(*) yes install it + 50 €

Now I'm exporting my order to an ERP product and in my order_item I have the total price (100+ 50) but I need to know if one of the option is selected and the option price.

My problem is How I can get the price of a product's custom option starting from an order_item object ?

I've tried the info_buyRequest and I get the option but no option price

Array (
    [info_buyRequest] => Array         ( 
        [uenc] => a....0lEPVU,
         [product] => 29517
         [options] => Array  (
                 [66668] => 134343 )
         [qty] => 0
         [return_url] =>
         [related_product] =>          )
   [options] => Array         (
         [0] => Array                 (
                 [label] => Servizi aggiuntivi 
                 [value] => Installazione (Include trasporto al piano) e ritiro usato
                 [print_value] => Installazione (Include trasporto al piano) e ritiro usato
                 [option_id] => 66668
                 [option_type] => radio
                 [option_value] => 134343
                 [custom_view] =>                  )
      )
  )

Then after some google I'm able to load the product options using:

$product = Mage::getModel('catalog/product')->load($item->getProductId());
$options = Mage::getModel('catalog/product_option')->getProductOptionCollection($product);
                foreach ($options as $option) {

The above get me all product's custom option and respective value but, how I can match the product's custom options and the one selected in the order_item?

the option_id 66668 and the value of 13434 is only present on the table sales_flat_quote_item_option (with no price) and in the catalog_product_option_xx there is no option with id 66668…

Best Answer

You can try code given below. It works for me hope also help you.

$options = $item->getBuyRequest()->getData('options');
$totalCustomOptionPrice = 0;

foreach ($options as $key => $value) 
{
    $optionData = $item->getProduct()->getOptionById($key);
    if ($optionData['type'] == 'field') 
    {
        $totalCustomOptionPrice = $optionData['price'];
    }
    else
    {
        foreach($value as $optionId)
        {
             foreach ($optionData->getValues() as $v) 
             {
                 if ($v['option_type_id'] == $optionId) 
                 {
                     $totalCustomOptionPrice += $v->getPrice();
                 }
             }
        }
    }
}