【问题标题】:get quote item id after adding product to cart将产品添加到购物车后获取报价项目 ID
【发布时间】:2016-04-13 11:28:39
【问题描述】:

我需要在添加到购物车时获取产品的报价项目 ID,因为我需要在此事件之后更新 Db 的字段,我已参考 How to get Quote Item Id after adding a product to cart?,但这不起作用,我已使用事件: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 />

请让我知道我做错了什么,我也参考了许多其他链接,但它们都不起作用。

【问题讨论】:

  • 首先检查 echo $quoteItem;看看这里是不是你得到了结果
  • var_dump($quoteItem) 的结果;是:NULL
  • $observer->getEvent()->getQuoteItem() 这不起作用检查你的功能
  • 请告诉我,我应该怎么做才能完成这项工作,我已经尝试了一切。
  • 你知道得到这个id吗?

标签: php magento


【解决方案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();
}

【讨论】:

  • 这没有返回正确的报价项目 ID,当购物车中有多个项目时,这是返回报价项目 ID 的第一个值。
  • 抱歉,没有测试项目 id 位。此方法返回 Quote Item 对象。请尝试在项目对象上调用 getItemId()。我已经更新了我的答案以反映这一点。
【解决方案2】:

据我了解,您不会获得此事件的 QuoteItems 键。此事件提供 items 数组,其中包含为产品添加的所有项目。所以你不必改变你的代码很多。

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

它应该会给你想要的结果。

【讨论】:

  • 这是返回空结果
【解决方案3】:

以下代码可能会对您有所帮助,

$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 加载客户并获取报价信息,以便您获取报价详情。

【讨论】:

    猜你喜欢
    • 2014-07-29
    • 1970-01-01
    • 2015-03-31
    • 2011-05-02
    • 2017-04-27
    • 1970-01-01
    • 2012-06-10
    • 2019-03-11
    • 2012-06-09
    相关资源
    最近更新 更多