【问题标题】:Set custom total row price in basket - Magento在购物篮中设置自定义总行价格 - Magento
【发布时间】:2014-05-28 12:15:12
【问题描述】:

我正在 Magento 中开发 B2B 网上商店。将产品添加到购物篮时,它将调用外部 API 以根据用户、产品和数量查找折扣。问题是 API 只返回总折扣价。

例如添加 10 件 5 美元的商品可能会返回 40 美元的总折扣价。所以理想情况下,购物车会显示$5 x 10 = $40

我已经通过在我的模块 config.xml 中覆盖 Mage_Sales_Model_Quote_Item 来实现这一点:

<global>
    <models>
        <sales>
            <rewrite>
                <quote_item>Frigg_Import_Model_QuoteItem</quote_item>
            </rewrite>
        </sales>
    </models>
</global>

然后覆盖calcRowTotal()

class Frigg_Import_Model_QuoteItem extends Mage_Sales_Model_Quote_Item
{
    protected $customRowTotalPrice = null;

    public function setCustomRowTotalPrice($price)
    {
        $this->customRowTotalPrice = $price;
    }

    public function calcRowTotal()
    {
        if ($this->customRowTotalPrice !== null) {
            $this->setRowTotal($this->getStore()->roundPrice($this->customRowTotalPrice));
            $this->setBaseRowTotal($this->getStore()->roundPrice($this->customRowTotalPrice));
            return $this;
        }

        $qty = $this->getTotalQty();
        $total = $this->getStore()->roundPrice($this->getCalculationPriceOriginal()) * $qty;
        $baseTotal = $this->getStore()->roundPrice($this->getBaseCalculationPriceOriginal()) * $qty;

        $this->setRowTotal($this->getStore()->roundPrice($total));
        $this->setBaseRowTotal($this->getStore()->roundPrice($baseTotal));
        return $this;
    }

}

然后处理事件checkout_cart_product_add_after 并将其传递给我的观察者方法setPriceForItem

<?php

class Frigg_Import_Model_Observer
{
    // Event: Price for single item
    public function setPriceForItem(Varien_Event_Observer $observer)
    {
        $customer = Mage::getSingleton('customer/session')->getCustomer();
        $item = $observer->getQuoteItem();
        if ($item->getParentItem()) {
            $item = $item->getParentItem();
        }

        $quantity = $item->getQty(); // e.g. 5
        $product = $item->getProduct();

        // Call API here and get the total price based on quantity (e.g. 40)
        // ....
        $customTotalRowPriceFromAPI = 40;

        if ($customTotalRowPriceFromAPI) {
            $item->setCustomRowTotalPrice($customTotalRowPriceFromAPI);
            $item->getProduct()->setIsSuperMode(true);
            $item->save();
        }
    }
}

现在这有效,但仅在添加到购物篮时。当我重新加载浏览器或进入购物车页面时,行价格已重置为原始价格(在本例中为$5 x 10 = $50)。

有人发现我的错误吗?我希望我已经正确解释了。

【问题讨论】:

  • 报价项目表是怎么说的?
  • 我不熟悉报价项目表(对 Magento 来说有点新)。你能详细说明一下吗?谢谢。
  • 有一个表格,里面有报价项目,在 phpmyadmin 中查看,看看是什么价格。
  • 将产品添加到购物篮时,sales_flat_quote_item 表中正确设置了行总数,但在访问购物车时,价格已重置。我猜我需要覆盖计算购物车中总行数的方法,但不知道如何(还)。有什么想法吗?
  • 我不太熟悉它,但是,它正在将产品加载到购物车中并从那里获取数据以获取图像和链接等明智的东西。我怀疑价格来自产品而不是报价对象。如果您直接结帐,是否显示预期价格?

标签: php magento


【解决方案1】:

解决了。只需在添加到购物车时将每个客户、产品和数量的价格存储在新表中,然后从 calcRowTotal 的新表中获取价格。

【讨论】:

    猜你喜欢
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多