观察者Checkout_cart_product_add_fter获取购物数量
-
16-10-2019 - |
题
我已经构建了一个观察者,该观察者在Checkout_cart_product_add_after上发射。在观察者中,我设置了这样的数量:
$item->setQty($quantity);
我想获取该产品购物车中已经存在的数量,以便将产品的数量改为更新或覆盖。然而,当我尝试将产品数量在购物车中获取(Mage :: Helper('Checkout/Cart'))时,我会得到“新”数量,这可能是因为更新数量代码已经触发。
我如何获取产品的购物车数量?
提前致谢,
乔斯特
编辑:
这是观察者的完整代码:
public function modifyPrice(Varien_Event_Observer $obs)
{
$item = $obs->getQuoteItem();
$item = ( $item->getParentItem() ? $item->getParentItem() : $item );
$price = $this->_getPrice($item);
$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);
$quantity = $this->_getQuantity($item);
$item->setQty($quantity);
$item->getProduct()->setIsSuperMode(true);
}
protected function _getPrice(Mage_Sales_Model_Quote_Item $item)
{
$price = 0;
Mage::app()->getRequest()->getPost();
$qtydropdownbox = Mage::app()->getRequest()->getParam('qtydropdownbox');
$prices = $item->getProduct()->getData('qty_dropdownbox_prices');
$quantities = $item->getProduct()->getData('qty_dropdownbox_quantities');
$array_prices = explode(';', $prices);
$array_quantities = explode(';', $quantities);
$price = $array_prices[$qtydropdownbox] / $array_quantities[$qtydropdownbox];
return $price;
}
protected function _getQuantity(Mage_Sales_Model_Quote_Item $item)
{
$quantity = 0;
Mage::app()->getRequest()->getPost();
$qty = Mage::app()->getRequest()->getParam('qty');
$qtydropdownbox = Mage::app()->getRequest()->getParam('qtydropdownbox');
$quantities = $item->getProduct()->getData('qty_dropdownbox_quantities');
$array_quantities = explode(';', $quantities);
$quantity = $array_quantities[$qtydropdownbox] * $qty;
return $quantity;
}
解决方案
我不明白您想实现的目标,但是要获得旧数量,您为时已晚,甚至Origdata也被覆盖了(我认为),但请检查一下。
我认为最简单的是 quote_item_load_after
并添加数量以稍后阅读,例如$ quoteItem-> setMypersonalqty($ quoteItem-> getQty())
其他提示
您可以使用活动 sales_quote_product_add_after
在保存新项目之前触发的,以便您可以使用 $item->getOrigData('qty')
获取原始数量。
该事件有一个参数, items
这是该产品生成的所有单一报价项目的数组。对于简单产品,这始终是一项,对于捆绑产品和可配置的产品,它是主要产品及其子女的项目,父母始终是第一项。