Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Magento2 删除已添加到购物车的消息Magento 2_Magento2_Magento 2.0_Magento2.2 - Fatal编程技术网

Magento2 删除已添加到购物车的消息Magento 2

Magento2 删除已添加到购物车的消息Magento 2,magento2,magento-2.0,magento2.2,Magento2,Magento 2.0,Magento2.2,我想在添加到购物车时删除成功消息。现在,当您单击添加到购物车按钮时,它会显示一条消息已成功添加到购物车,但我不想显示此消息。有没有办法做到这一点?实现这一点相当容易。在下面创建一个基本模块 app/code/with /registration.php <?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MO

我想在添加到购物车时删除成功消息。现在,当您单击
添加到购物车
按钮时,它会显示一条消息
已成功添加到购物车
,但我不想显示此消息。有没有办法做到这一点?

实现这一点相当容易。在下面创建一个基本模块
app/code/
with

/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);
因此,当调度
checkout\u car\u add\u product\u complete
时,调用观察者
AfterAddToCart
。这样创建它:

<?php
namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Checkout\Model\Cart as CustomerCart;

class AfterAddCart implements ObserverInterface
{

    private $cart;

    public function __construct(
        CustomerCart $cart
    ){
        $this->cart = $cart;
    }

    public function execute(EventObserver $observer)
    {
        $this->cart->getQuote()->setHasError(true);
    }
}

实现这一点相当容易。在下面创建一个基本模块
app/code/
with

/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);
因此,当调度
checkout\u car\u add\u product\u complete
时,调用观察者
AfterAddToCart
。这样创建它:

<?php
namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Checkout\Model\Cart as CustomerCart;

class AfterAddCart implements ObserverInterface
{

    private $cart;

    public function __construct(
        CustomerCart $cart
    ){
        $this->cart = $cart;
    }

    public function execute(EventObserver $observer)
    {
        $this->cart->getQuote()->setHasError(true);
    }
}

  • di.xml
  • execute()中扩展添加购物车控制器和
  • $this->messageManager->getMessages()->deleteMessageByIdentifier('addCartSuccessMessage');
    
  • di.xml
  • execute()中扩展添加购物车控制器和
  • $this->messageManager->getMessages()->deleteMessageByIdentifier('addCartSuccessMessage');
    
    我曾在Magento 2.3.2上尝试过此功能,但它不起作用。我需要修改你的代码吗?Thanks@Tobi此解决方案是否故意注入错误以阻止显示“已添加到购物车”消息?这似乎是一种破坏性的/不明智的方法。我在Magento 2.3.2上尝试过这种方法,但它不起作用。我需要修改你的代码吗?Thanks@Tobi此解决方案是否故意注入错误以阻止显示“已添加到购物车”消息?这似乎是一种极具破坏性/不明智的做法。