0

I want to display a custom message on product page after Ajax add to cart success message in list page. Can you suggest anyway?

  • 1
    Can you please explain more what you do actually? 1. Add div after add to cart button 2. Add text message after the success message – Vithal Bariya May 17 '18 at 8:47
  • I want add custom message in div..after addtocart success message.. – Mano M May 17 '18 at 8:49
  • Use this extension. It lets you add custom message to cart, checkout & order success page. You can also use attributes and conditions to display the message. – Henry Roger Sep 2 at 10:44
1

You can use plugin or event/observer to update the success message.

app/code/{Vendor}/{Module}/etc/frontend/events.xml

<event name="controller_action_postdispatch_checkout_cart_add">
    <observer name="updatemessage" instance="{Vendor}\{Module}\Observer\UpdateMessage" />
</event>

In observer, Use \Magento\Framework\Message\ManagerInterface to get last added message and add new/updated success message.

app/code/{Vendor}/{Module}/Observer/UpdateMessage.php

<?php
namespace {Vendor}\{Module}\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;

class UpdateMessage implements ObserverInterface
{
    /** @var \Magento\Framework\Message\ManagerInterface */
    protected $messageManager;

    /** @var \Magento\Framework\UrlInterface */
    protected $url;

    public function __construct(
        \Magento\Framework\Message\ManagerInterface $managerInterface,
        \Magento\Framework\UrlInterface $url
    ) {
        $this->messageManager = $managerInterface;
        $this->url = $url;
    }

    public function execute(\Magento\Framework\Event\Observer $observer) {
        $messageCollection = $this->messageManager->getMessages(true);
        $customMessage = '<div>Your Message Here</div>';
        $this->messageManager->addSuccess($messageCollection->getLastAddedMessage()->getText() . '  ' . $customMessage);
    }
}
  • I need this message in list page.(Ajax Addtocart) – Mano M May 17 '18 at 9:07
  • @ManoM please add all the information in your question. – Nikunj Vadariya May 17 '18 at 9:08
  • Ok ...I will update.... – Mano M May 17 '18 at 9:11
  • @ManoM It's common add to cart event. You can customize on your way in that code. – Vithal Bariya May 17 '18 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.