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

问题描述 投票:0回答:1

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

magento2 magento-2.0 magento2.2
1个回答
0
投票

实现这一目标非常简单。在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);
    }
}

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

这个解决方案原本不是我的,但我不记得我发现它的位置。

© www.soinside.com 2019 - 2024. All rights reserved.