Zahirabbas
Zahirabbas

Reputation: 1119

get selected custom option price for simple product in observer

How to get selected custom option price in observer. I'm using checkout_cart_product_add_after event for observer and observer code:

public function applyCartPriceChange(Varien_Event_Observer $observer)
{

    $item     = $observer->getQuoteItem();
    $product  = $item->getProduct();
    $productOptions = $product->getTypeInstance(true)->getOrderOptions($product);
    echo '<pre>';
    print_r($productOptions);
    foreach($productOptions['options'] as $key=>$value){
        if($value['label'] !='Date'){
            echo $value['option_id'];
        }
    }
    exit;

    if ($specialPrice > 0) {
        $item->setCustomPrice($specialPrice);
        $item->setOriginalCustomPrice($specialPrice);
        $item->getProduct()->setIsSuperMode(true);
    }
}

I'm getting all selected custom option detail but not price using this code $productOptions = $product->getTypeInstance(true)->getOrderOptions($product); it print array something like:

Array
(
    [info_buyRequest] => Array
    (
        [uenc] => aHR0cDovLzE5Mi4xNjguMS45My9sbWR0L2luZGV4LnBocC9kYXktdG91cnMvdGVzdDIuaHRtbA,,
        [product] => 35
                [form_key] => UnobrzsuAmTK6rJy
                [related_product] => 
                [options] => Array
(
    [635] => Array
    (
        [date] => 12/19/2013
                                [date_internal] => 2013-12-19 00:00:00
                            )

                    [633] => 1735
                    [636] => Array
(
    [0] => 1749
)

[634] => 1741
                    [637] => Array
(
    [0] => 1751
)

                )

            [validate_datetime_635] => 
            [qty] => 0
        )

    [options] => Array
(
    [0] => Array
    (
        [label] => Date
                    [value] => Dec 19, 2013
                    [print_value] => Dec 19, 2013
                    [option_id] => 635
                    [option_type] => date
                    [option_value] => 2013-12-19 00:00:00
                    [custom_view] => 
                )

            [1] => Array
(
    [label] => Adult
                    [value] => 7
                    [print_value] => 7
                    [option_id] => 633
                    [option_type] => drop_down
                    [option_value] => 1735
                    [custom_view] => 
                )

            [2] => Array
(
    [label] => Lunch
                    [value] => Adult Lunch
                    [print_value] => Adult Lunch
                    [option_id] => 636
                    [option_type] => checkbox
                    [option_value] => 1749
                    [custom_view] => 
                )

            [3] => Array
(
    [label] => Child
                    [value] => 3
                    [print_value] => 3
                    [option_id] => 634
                    [option_type] => drop_down
                    [option_value] => 1741
                    [custom_view] => 
                )

            [4] => Array
(
    [label] => Lunch
                    [value] => Child Lunch
                    [print_value] => Child Lunch
                    [option_id] => 637
                    [option_type] => checkbox
                    [option_value] => 1751
                    [custom_view] => 
                )

        )

)

Any help would be much appreciated.Thanks

Upvotes: 2

Views: 9833

Answers (2)

Zahirabbas
Zahirabbas

Reputation: 1119

public function applyCartPriceChange(Varien_Event_Observer $observer)
{
    $item = $observer->getQuoteItem();
    $product = $item->getProduct();
    $productOptions = $product->getTypeInstance(true)->getOrderOptions($product);
    //echo '<pre>';
    //print_r($productOptions);exit;
    foreach ($productOptions['options'] as $key => $value) {

        $product = Mage::getModel("catalog/product")->load($product->getId()); //product id 1
        foreach ($product->getOptions() as $o) {
            $values = $o->getValues();
            if ($o->getTitle() == 'Adult') { //change your custom option title to compare
                foreach ($values as $v) {
                    if ($value['option_value'] == $v->getOptionTypeId()) {
                        $adultPrice = $v->getprice(); /* get price of custom option*/
                        $noOfAdult = $v->getTitle();
                    }
                }
            }
        }
    }
}

Upvotes: 8

Giel Berkers
Giel Berkers

Reputation: 2960

If you have the option value ID you can also do I direct query to get the option price. I know this is not completely the Magento way and you might have to do some custom calculating (for procent prices for example), but you could do something like this:

$optionValueId = 1234; // replace with you option value ID

$resource = Mage::getSingleton('core/resource');
$connection = $resource->getConnection('read');

$optionvaluePrice = $connection->fetchRow(
    sprintf('SELECT * FROM %1$s WHERE option_type_id = %2$d', 
        $resource->getTableName('catalog/product_option_type_price'), 
        $optionValueId
    )
);

Sadly, Magento doesn't seem to have a model to load a single option price separately.

Upvotes: 1

Related Questions