2014-09-02 80 views
0

我注意到使用item-> getProduct()從購物車或結帳,有自定義選項的項目的問題。特別是如果您的購物車有兩個相同的產品,但具有不同的自定義選項選擇。例如:購物車包含2 x產品A,其中一個具有尺寸小(價格+ $ 0)的自定義選項,另一個具有大(價格+ $ 5)的自定義選項。

我觀察事件 'catalog_product_get_final_price'

這是我的觀察器功能:

public function onGetFinalPrice($observer) 
{ 
    $items = $this->getCheckout()->getQuote()->getAllItems(); 
    foreach($items as $item){ 
     $product = $item->getProduct(); 
    } 

} 

價格在車兩個項目,現在將+ $ 5。 這裏的getProduct功能從Mage_Sales_Model_Quote_Item_Abstract:

public function getProduct() 
{ 
    $product = $this->_getData('product'); 
    if ($product === null && $this->getProductId()) { 
     $product = Mage::getModel('catalog/product') 
      ->setStoreId($this->getQuote()->getStoreId()) 
      ->load($this->getProductId()); 
     $this->setProduct($product); 
    } 

    /** 
    * Reset product final price because it related to custom options 
    */ 
    $product->setFinalPrice(null); 
    if (is_array($this->_optionsByCode)) { 
     $product->setCustomOptions($this->_optionsByCode); 
    } 
    return $product; 
} 

看來,該報價項目變量$ _optionsByCode不保留獨特的價值。調用getProduct()將重置這些值並因此破壞它們。任何想法在這裏修復?

+0

可以嘗試'$用品 - > getFinalPrice()'而不是'$ item-> getProduct() - > getFinalPrice()'? – 2014-09-02 03:33:23

+0

調用$ item-> getProduct()是必需的,因爲我需要從產品中檢索信息。我的興趣是爲什麼自定義選項在您執行此調用時被重置。 – wsagen 2014-09-02 13:43:47

回答

0

我建議,而不是catalog_product_get_final_price使用 checkout_cart_product_add_afte [R使用this event you set any price of product at cart.

配置在config.xml中更好看process.because:

<events> 
     <checkout_cart_product_add_after> 
      <observers> 
       <apply_custom_price> 
        <class>custompriceset/observer</class> 
        <method>applyCustomPrice</method> 
       </apply_custom_price> 
      </observers> 
     </checkout_cart_product_add_after> 
    </events>