Magento 购物车:检查清空购物车的物品

发布于 2024-09-17 22:33:26 字数 1010 浏览 2 评论 0原文

我是 Magento 的新手,但我认为直到今天我已经掌握了它。这是我的问题。

我正在编写一个新的观察者,以在页面加载时将优惠券添加到购物车。优惠券代码在 URL 中传递,我希望该代码可以通过任何工作 URL 传递。

例如: http://magento/?coupon=MYCOUPON

我正在捕捉事件“controller_front_init_routers”获取优惠券代码。

我让观察者工作,但如果我的购物车中已经有一个商品并且我传递了优惠券代码,我的购物车就会显示为空,以下是我添加优惠券的方式:

    public function applyCoupon($observer){
        $coupon_code = $observer->getEvent()->getData('front')->getRequest()->getParam('coupon');
        if(!empty($coupon_code)){
            Mage::getSingleton('checkout/session')->setData('coupon_code', $coupon_code);
            Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode($coupon_code)->save();

            Mage::log('Coupon Code: '. $coupon_code);
        }
    }

似乎每当我致电 Mage::getSingleton('checkout/ session')->anything() 我丢失了购物车的会话。

我想也许我只需要获取当前的购物车 ID 并加载它,但我似乎也找不到办法做到这一点。

有没有人有这方面的经验,也许有解决方案?

I am new to Magento, but I thought I had a grasp on it until today. Here is my problem.

I am writing a new Observer to add a coupon to the cart on page load. The coupon code is passed in the URL and I desire the code to be passable through ANY working URL.

For example: http://magento/?coupon=MYCOUPON

I am catching on the event "controller_front_init_routers" to capture the coupon code.

I have the observer working, but if I already have an item in the cart and I pass a coupon code my cart appears empty, here is how I am adding the coupon:

    public function applyCoupon($observer){
        $coupon_code = $observer->getEvent()->getData('front')->getRequest()->getParam('coupon');
        if(!empty($coupon_code)){
            Mage::getSingleton('checkout/session')->setData('coupon_code', $coupon_code);
            Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode($coupon_code)->save();

            Mage::log('Coupon Code: '. $coupon_code);
        }
    }

It seems that anytime I call Mage::getSingleton('checkout/session')->anything() I lose the session for the cart.

I thought maybe I simply needed to fetch the current cart ID and load it, but I can't seem to find a way to do that either.

Has any one had experience in this, maybe has a solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新雨望断虹 2024-09-24 22:33:26

问题在于你正在观察的事件。因为 Magento 会话当时尚未初始化,所以 cookie 的名称与核心名称不同。

使用 controller_action_predispatch 根据请求设置一些会话数据。

The problem is in event, that you are observing. Because Magento session wasn't initialized at that moment, so cookie has different name, than core one.

Use controller_action_predispatch for setting up some session data from request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文