获取自定义选项的值

时间:2013-03-08 11:00:10

标签: magento

我正在尝试根据一些自定义选项设置更改价格。因此,我试图获取客户输入的值,而不是后端设置的默认值。为此,我使用catalog_product_get_final_price中使用的事件Mage_Bundle_Model_Product_Price。我已经注册了以下观察员:

public function observer_callback($evt_obs)
{
    $event = $evt_obs->getEvent();
    $data  = $event->getData();
    /* @var $collection Mage_Catalog_Model_Resource_Product_Collection */
    $collection = $data['collection'];

    $items = $collection->getItems();

    /* @var $item Mage_Catalog_Model_Product */
    foreach ($items as $item) {
        if ( $item->getName() ==  'Bundel Test2') {

            $options = $item->getCustomOptions();

            /* @var $option Mage_Catalog_Model_Product_Option */
            foreach ($options as $option) {
                // Here I'm trying to get the value given by the user/customer
                var_dump($option->getData());
            }

        }
    }
    return $this;
}

它是捆绑类型的自定义选项。因此产品无法配置。 我是magento的新手所以我可能错过了一些东西。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

希望这段代码可以帮到你:

public function productFinalPrice($observer){
     $product    = $observer->getEvent()->getProduct();

     $productType=$product->getTypeID();

     if($productType == 'your_product_type')
     {

        $option = $product->getCustomOptions();
        $searchedOption = null;  

        //search for your option;
        foreach ($product->getOptions() as $o) {

            if($o->getTitle()=="your_attribute_title" && $o->getType()=="your_type_of_option(eg. area"){
                $optionId = $o->getOptionId();//got your searched optionId
                break;
            }
        }


        foreach($option as $key => $o) {
            if($key == "option_".$optionId) {
                $searchedOption = $o;
               //here you get the option object with the values in it
            }
        }


        $articleNumber = $searchedOption->getData('value'); // getthe value of your option


            //calculate final price like you need it
            $product->setFinalPrice($finalPrice);
     }  
     return $this;     
}

最好的问候