I have redirected customer to cart from checkout in a specific scenario using observer with a custom error message. Here is the observer code.
namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
public function __construct(
\Magento\Framework\Url $url,
\Magento\Framework\Message\ManagerInterface $messageManager,
\Magento\Framework\App\Response\Http $http,
// other dependencies
)
{
$this->url = $url;
$this->messageManager = $messageManager;
$this->http = $http;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$cartUrl = $this->url->getUrl('checkout/cart');
$mySpecificCondition = true;
if ($mySpecificCondition) {
$this->messageManager->addErrorMessage('Custom error message here.');
$this->http->setRedirect($cartUrl);
}
}
}
This error message is shown forever if I do not refresh this page. How I can program it so that this custom error message is hidden after 5 seconds?
jQuery('.messages').delay(5000).fadeOut('slow');
in cart page, but is there a better approach available?