【问题标题】:calling $item->getProduct() corrupts chosen custom options调用 $item->getProduct() 损坏选择的自定义选项
【发布时间】:2014-10-26 05:53:25
【问题描述】:

我注意到在购物车或结帐中使用 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 美元。 这是 Mage_Sales_Model_Quote_Item_Abstract 的 getProduct 函数:

 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() 正在重置这些值,因此会破坏它们。有什么解决办法吗?

【问题讨论】:

  • 你能用$item->getFinalPrice()代替$item->getProduct()->getFinalPrice()吗?
  • 调用 $item->getProduct() 是必需的,因为在观察者中我需要从产品中检索信息。我感兴趣的是为什么在您执行此调用时会重置自定义选项。

标签: php magento


【解决方案1】:

我建议不要使用catalog_product_get_final_price checkout_cart_product_add_after 更好的过程。因为使用this event you set any price of product at cart.

config.xml 中的配置查看:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 1970-01-01
    • 2012-01-20
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多