简体   繁体   English

获取magento中添加到购物车的最新产品的ID

[英]Get ID of the last product added to the cart in magento

I am trying to get the ID of the product that was most recently added to a user's cart. 我正在尝试获取最近添加到用户购物车的产品的ID。 A quick google search revealed this function 快速谷歌搜索显示此功能

Mage::getSingleton('checkout/session')->getLastAddedProductId(true);

which is also used in Mage_Checkout_Block_Cart_Crosssell. 这也用在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. 我还试图创建Crossell块并使受保护的方法包含getLastAddedProductId函数public但是返回null就像我尝试自己调用它时一样。

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. 我尝试了解源代码,特别是核心中的checkout / model / session.php文件,我似乎无法找到函数的定义。 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 (eg the user adds a laptop the cart when there already is one) 我知道这些项目是按顺序添加的,我或许可以从购物车中获取项目列表的最后一项,但是在用户将相同项目添加到购物车的情况下这实际上不会增加​​数量而不是实际项目本身(例如,用户在已经有一台笔记本电脑时添加一台笔记本电脑)

The call to 打电话给

Mage::getSingleton('checkout/session')->getLastAddedProductId(true);

Is actually clearing the session variable after it is read. 实际上是在读取后清除会话变量。 Magento uses magic methods extensively. Magento广泛使用魔法。 In this case you are using the __call magic method which in turn uses the getData() method. 在这种情况下,您使用__call魔术方法,后者又使用getData()方法。 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). 在Mage_Core_Model_Session_Abstract_Varien中,您将看到它们覆盖了getData()的默认行为,以期望第二个参数是布尔值(getData的第一个参数是您要查找的值的键名)。 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. 您始终可以侦听checkout_cart_product_add_after事件,并将该项添加到会话中您自己的变量中。 That event is actually fired on the line before setLastAddedProductId() is called. 在调用setLastAddedProductId()之前,该事件实际上是在该行上触发的。

try to grep the variable you are looking for. 尝试grep你正在寻找的变量。 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 由于它们来自魔术方法,因此很难找到您所追求的确切功能,因此更容易看到数据设置的位置比使用它的位置更容易

grep '>setLastAddedProductId' app/code -rsn

to see where the product id gets set to that session variable 查看产品ID设置为该会话变量的位置

app/code/core/Mage/Checkout/Model/Cart.php:255:        $this->getCheckoutSession()->setLastAddedProductId($product->getId());

and then you can ask this variable (if it is set else empty()) 然后你可以问这个变量(如果设置为其他为空())

Mage::getSingleton('checkout/session')->getLastAddedProductId();

and you can see all the things that are in checkout/session and verify if the data is there. 并且您可以查看结帐/会话中的所有内容并验证数据是否存在。

var_dump(Mage::getSingleton('checkout/session'));

Haven't a clue why it works this way but it works for me in Magento 1.6... 不知道为什么它以这种方式工作但它在Magento 1.6中适用于我......

<?php
require_once ( "app/Mage.php" );
umask(0);

Mage::app("default");

Mage::getSingleton('core/session', array('name'=>'frontend'));
$session = Mage::getSingleton('checkout/session');

$lastadded = $session->getData("last_added_product_id");

print_r($lastadded);

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! 希望这可以帮助你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM