获取最后添加到 magento 购物车的产品的 ID
我正在尝试获取最近添加到用户购物车的产品的 ID。谷歌快速搜索发现了这个函数
Mage::getSingleton('checkout/session')->getLastAddedProductId(true);
,它也被用在 Mage_Checkout_Block_Cart_Crosssell 中。然而,当我尝试在自己的控制器中调用该函数时,它什么也没有返回。 我尝试通过以下方式实例化核心会话
Mage::getSingleton('core/session', array('name'=>'frontend'))
,但是这种方法似乎不起作用。我还尝试创建 Crossell 块,并将包含 getLastAddedProductId
函数的受保护方法设为公共,但它会返回 null,就像我尝试单独调用它时一样。
为了使用这个函数,我必须调用或实例化一些东西吗?这是我的源列表供参考。
class Mymodule_Addcartaftermath_ItemaddedController extends Mage_Core_Controller_Front_Action {
public function generatemessageAction() {
$parameters = $this->getRequest()->getParams();
if (isset($parameters['ajax_call'])) {
$latest_item_id = Mage::getSingleton('checkout/session')->getLastAddedProductId(true);
$response = array('response' => $latest_item_id);
echo json_encode($response);
} else {
$this->_redirect('/');
}
}
}
我尝试浏览源代码,特别是核心中的 checkout/model/session.php 文件,但我似乎找不到该函数的定义。我还查看了它的父类定义,但也找不到它。
如果我无法使用此方法,是否有其他方法可以检索最近添加的项目?我知道这些物品是按顺序添加的,我也许可以从购物车中获取物品列表的最后一项,但是,如果用户将相同的物品添加到购物车中,实质上增加了数量而不是实际数量,那么这将不起作用商品本身(例如,用户在购物车中已有一台笔记本电脑时添加一台笔记本电脑)
I am trying to get the ID of the product that was most recently added to a user’s cart. A quick google search revealed this function
Mage::getSingleton('checkout/session')->getLastAddedProductId(true);
which is also used in Mage_Checkout_Block_Cart_Crosssell. When I try calling the function in my own controller however, it returns nothing.
I have tried to instantiate a core session via
Mage::getSingleton('core/session', array('name'=>'frontend'))
however this approach does not seem to work. I also tried to create the Crossell block and making the protected method that wraps around getLastAddedProductId
function public however that returns null just like it does when I try calling it on its own.
Is there something I have to call or instantiate in order to use this function? Here’s my source listing for reference.
class Mymodule_Addcartaftermath_ItemaddedController extends Mage_Core_Controller_Front_Action {
public function generatemessageAction() {
$parameters = $this->getRequest()->getParams();
if (isset($parameters['ajax_call'])) {
$latest_item_id = Mage::getSingleton('checkout/session')->getLastAddedProductId(true);
$response = array('response' => $latest_item_id);
echo json_encode($response);
} else {
$this->_redirect('/');
}
}
}
I tried poking through the source code, particularly the checkout/model/session.php file in the core and I cannot seem to find the definition of the function. I also looked at it’s parent’s class definition but could not find it there either.
If this method is not available to me is there another way of retrieving the most recent item added? I know that the items are added sequentially and I could perhaps just get the last item of the list of items from the cart however this would not work in the case where the user adds the same item to the cart essentially increasing the quantity rather than actual item itself (e.g. the user adds a laptop the cart when there already is one)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
调用
实际上是在读取会话变量后清除它。 Magento 广泛使用魔法方法。在本例中,您使用的是 __call 魔术方法,该方法又使用 getData() 方法。在 Mage_Core_Model_Session_Abstract_Varien 中,您将看到它们覆盖 getData() 的默认行为,以期望第二个参数为布尔值(getData 的第一个参数是您正在查找的值的键名称)。该布尔值是一个标志,告诉会话在读取后清除变量。
您始终可以侦听 checkout_cart_product_add_after 事件并将该项目添加到会话中您自己的变量中。该事件实际上是在调用 setLastAddedProductId() 之前在线路上触发的。
The call to
Is actually clearing the session variable after it is read. Magento uses magic methods extensively. In this case you are using the __call magic method which in turn uses the getData() method. In Mage_Core_Model_Session_Abstract_Varien you will see that they override the default behaviour of getData() to expect the second parameter to be a boolean (The first parameter to getData is the key name for the value you are looking for). That boolean is a flag telling the session to clear the variable after reading.
You could always listen for the checkout_cart_product_add_after event and add the item to your own variable in the session. That event is actually fired on the line before setLastAddedProductId() is called.
尝试 grep 您要查找的变量。由于它们来自魔术方法,因此很难找到您想要的确切函数,因此更容易查看数据设置的位置,而不是用于
查看产品 id 设置为该会话变量的位置
,然后您可以询问此变量(如果设置为空())
,您可以看到结账/会话中的所有内容并验证数据是否存在。
try to grep the variable you are looking for. As they are coming from magic methods then its hard to find the exact function you are after so it's easier to see the places where data gets set than where it is used
to see where the product id gets set to that session variable
and then you can ask this variable (if it is set else empty())
and you can see all the things that are in checkout/session and verify if the data is there.
不知道为什么它会这样工作,但它在 Magento 1.6 中对我有用......
显然你必须实例化核心/会话,然后实例化结账/会话。我已经尝试过其他所有方法,但这是我发现它有效的唯一方法。也许有人可以解释为什么它会这样工作。希望这对您有帮助!
Haven't a clue why it works this way but it works for me in Magento 1.6...
Apparently you have to instantiate the core/session and then the checkout/session. I've tried it every other way but this is the only way I've found it to work. Perhaps someone can explain why it works this way. Hope this helps you out!