本文介绍了在观察器中获取简单产品的选定定制选项价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在观察器中获取选定的自定义期权价格.我正在使用checkout_cart_product_add_after事件作为观察者和观察者代码:

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);
    }
}

我使用此代码获取所有选定的自定义选项详细信息,但未获取价格$productOptions = $product->getTypeInstance(true)->getOrderOptions($product);它打印出类似以下内容的数组:

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] => 
                )

        )

)

任何帮助将不胜感激.

推荐答案

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();
                    }
                }
            }
        }
    }
}

这篇关于在观察器中获取简单产品的选定定制选项价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 04:06