在观察者中获得简单产品的选定自定义选项价格

时间:2013-12-19 04:43:30

标签: magento

如何在观察者中获得选定的自定义选项价格。我正在为观察者和观察者代码使用checkout_cart_product_add_after事件:

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);获取所有选定的自定义选项详细信息,但不是价格,它打印数组如下:

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

        )

)

非常感谢任何帮助。谢谢

2 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:0)

如果您有选项值ID,我也可以直接查询以获得期权价格。我知道这不完全是Magento的方式,你可能需要做一些自定义计算(比如版本价),但你可以这样做:

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

可悲的是,Magento似乎没有单独加载单一期权价格的模型。