【问题标题】:Remove added to cart message Magento 2删除已添加到购物车的消息 Magento 2
【发布时间】:2019-03-13 15:10:15
【问题描述】:

我想在添加到购物车时删除成功消息。现在,当您单击Add to Cart 按钮时,它会显示Successfully added <product> to cart 的消息,但我不想显示此消息。有没有办法做到这一点?

【问题讨论】:

    标签: magento2 magento-2.0 magento2.2


    【解决方案1】:

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

    /registration.php

    <?php
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'Vendor_Module',
        __DIR__
    );
    

    /etc/module.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="Vendor_Module" setup_version="0.1.0">
        </module>
    </config>
    

    现在您可以删除“添加到购物车”消息的方法是观察它的可观察事件并在发送后将其删除。使用以下内容创建/etc/events.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
        <event name="checkout_cart_add_product_complete">
            <observer name="your_observer_name" instance="Vendor\Module\Observer\AfterAddToCart" />
        </event>
    </config>
    

    所以当checkout_car_add_product_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);
        }
    }
    

    就是这样。添加到购物车消息将不再显示,而所有其他消息(如添加到比较等)仍将显示。

    这个解决方案最初不是我的,但我不记得在哪里找到它了。

    【讨论】:

    • 我已经在 Magento 2.3.2 上尝试过,但它不起作用。我需要修改你的代码吗?谢谢
    • @Tobi 此解决方案是否故意注入错误以阻止显示“已添加到购物车”消息?这似乎是一种破坏性/不明智的做法。
    【解决方案2】:
    1. di.xml &lt;preference for="Magento\Checkout\Controller\Cart\Add" type="&lt;Vendor&gt;\&lt;Module&gt;\Controller\Cart\Add"/&gt;
    2. 扩展添加购物车控制器和execute()
    $this->messageManager->getMessages()->deleteMessageByIdentifier('addCartSuccessMessage');
    

    【讨论】:

      【解决方案3】:

      您必须将afterExecute 插件添加到Magento\Checkout\Controller\Cart\Add 类:

      class DisableAddToCartMessage
      {
          /** @var string  */
          protected const DEFAULT_MESSAGE_IDENTIFIER = 'default_message_identifier';
      
          /** @var string  */
          protected const ADD_TO_CART_SUCCESS_MESSAGE_IDENTIFIER = 'addCartSuccessMessage';
      
          /** @var string  */
          protected const SUCCESS_MESSAGE_TYPE = 'success';
      
          /** @var MessageManagerInterface */
          protected MessageManagerInterface $messageManager;
      
          public function __construct(
              MessageManagerInterface $messageManager
          ) {
              $this->messageManager = $messageManager;
          }
      
          public function afterExecute(Action $subject, $result)
          {
              if ($this->messageManager->getMessages()->getLastAddedMessage()->getIdentifier() === self::ADD_TO_CART_SUCCESS_MESSAGE_IDENTIFIER) {
                  $this->messageManager->getMessages()->deleteMessageByIdentifier(self::ADD_TO_CART_SUCCESS_MESSAGE_IDENTIFIER);
              } else if ($this->messageManager->getMessages()->getLastAddedMessage()->getType() == self::SUCCESS_MESSAGE_TYPE){
                  $this->messageManager->getMessages()->deleteMessageByIdentifier(self::DEFAULT_MESSAGE_IDENTIFIER);
              }
      
              return $result;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-10
        • 1970-01-01
        • 1970-01-01
        • 2017-05-18
        • 1970-01-01
        相关资源
        最近更新 更多