2013-03-08 94 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;  
} 

問候