2013-03-08 67 views
0

我试图根据一些自定义选项设置来更改价格。因此,我试图获取客户输入的值,而不是在后端设置的默认值。为此,我使用Mage_Bundle_Model_Product_Price中使用的事件catalog_product_get_final_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的新手,所以我可能会错过一些东西。

任何人都可以帮助我吗?

回答

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

问候

相关问题