将产品添加到购物车

时间:2016-04-13 11:28:40

标签: php magento

我需要在产品添加到购物车时获得产品的报价项ID,因为我需要在此事件后更新Db的字段,我已经参考了How to get Quote Item Id after adding a product to cart?,但这不起作用,我使用了event:sales_quote_product_add_after,我的oberver函数是:

$quoteItem = $observer->getEvent()->getQuoteItem();
$id = $quoteItem->getId();

我也试过

$id = $quoteItem->getItemId();

抛出以下致命错误:

Fatal error</b>:  Call to a member function getId() on a non-object in <b>C:\xampp\htdocs\project\app\code\local\Custom\Module\Model\Observer.php</b> on line <b>1053</b><br />

请让我知道我做错了什么,我也从其他许多链接中提到了参考资料,但它们都没有工作。

3 个答案:

答案 0 :(得分:1)

根据我的理解,您不会获得此事件的QuoteItems键。此事件提供items数组,其中包含为产品添加的所有项目。因此,您不必经常更改代码。

$item = $observer->getEvent()->getItems()[0];
$item_id =  $item->getId();

它应该给你想要的结果。

答案 1 :(得分:1)

您应该在模块配置文件中定义一个观察者,当有人将项目添加到购物篮时,该文件会调用方法。像下面这样的东西;

    <events>
        <sales_quote_item_set_product>
            <observers>
                <quoteitem_set_eta_data>
                    <type>singleton</type>
                    <class>NameSpace_Eta_Model_Observer</class>
                    <method>setEtaOnQuoteItem</method>
                </quoteitem_set_eta_data>
            </observers>
        </sales_quote_item_set_product>
    </events>

然后,您的观察者方法可以使用以下内容访问引用项目;

public function setEtaOnQuoteItem($oObserver) {
    $oQuoteItem = $oObserver->getQuoteItem();
    $quoteId = $oQuoteItem->getItemId();
}

答案 2 :(得分:0)

以下代码可以帮助您,

$customer = Mage::getModel('customer/customer')->load($customerId);
$quote = Mage::getModel('sales/quote')->setSharedStoreIds($storeIds)
    ->loadByCustomer($customer);
$collection = $quote->getItemsCollection();
print_r($collection->getData());

使用customer_id加载客户并获取报价信息,以便获得报价详情。

相关问题