How to show success message after product add to cart on home page like this "You added product-name to your shopping cart." I am using that code on template file -

    <form data-role="tocart-form" action="<?php echo $postParams['action']; ?>" method="post">
                                                <input type="hidden" name="product" value="<?php echo $postParams['data']['product']; ?>">
                                                <input type="hidden" name="<?php echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php echo $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">
                                                <?php if(isset($_category_config['qty_field']) && $_category_config['qty_field']):?>
                                                    <div class="qty-box">
                                                        <a href="javascript:void(0)" class="qtyminus"><i class="porto-icon-minus"></i></a>
                                                        <input type="text" name="qty" id="qty" maxlength="12" value="<?php /* @escapeNotVerified */ echo $block->getProductDefaultQty() * 1 ?>" title="<?php /* @escapeNotVerified */ echo __('Qty') ?>" class="input-text qty" data-validate="<?php echo $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"/>
                                                        <a href="javascript:void(0)" class="qtyplus"><i class="porto-icon-plus"></i></a>
                                                <?php endif;?>
                                                <?php echo $block->getBlockHtml('formkey')?>
                                                <button type="submit"
                                                        title="<?php echo $block->escapeHtml(__('Add to Cart154')); ?>"
                                                        class="action tocart primary">
                                                    <span><?php echo __('Add to Cart154') ?></span>
 <?php if (!$block->isRedirectToCartEnabled()) : ?>
        <script type="text/x-magento-init">
            "[data-role=tocart-form], .form.map.checkout": {
                "catalogAddToCart": {}
    <?php endif; ?>

example(like this on home page,this image is from category page) - enter image description here Product is added successfully in cart but add to cart success message is not showing

2 Answers 2


From where you doing add to cart? From a product details page, Or category page?

  • in my questions you can see i am talking about home page Commented Jan 6, 2020 at 7:43

we need to assign a placeholder for the message through frontend layout xml used in the module as given below inside the tag:

So add below layout code in you theme's


file inside body tag

<referenceContainer name="page.messages">
        <block class="Magento\Framework\View\Element\Template" name="ajax.message.placeholder" template="Magento_Theme::html/messages.phtml"/>
        <block class="Magento\Framework\View\Element\Messages" name="messages" as="messages" template="Magento_Theme::messages.phtml"/>

Or add in admin Content > Pages > and edit Home page and add above code in Design > layout update scetion

  • not working,not showing message for add to cart Commented Jan 6, 2020 at 7:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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