My layout file code is


<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd" layout="2columns-left"> 

    <referenceContainer name="content">
        <block   class="Vendor\Modulename\Block\ProductTab" name="producttab" template="producttab.phtml" ifconfig="form_section/form_group_general/status">

Template file


$collection = $block->getProductCollection(); 

<div class="tabContent" id="best_seller">
<div id="best_header"><h1><strong><center></center></strong></h1></div>

<div id="owl-demo-1" class="owl-demo owl-carousel owl-theme">


  foreach ($collection as $_productLoad){

      $_items = $_productLoad->getAllItems();
      foreach ($_items as $item) {

        $_product = $this->getLoadProduct($item->getProductId());

    $addToCartUrl =  $listBlock->getAddToCartUrl($_product); ?>

<div id="item">


    $imageUrl = $this->getImageConfig($_product);

    <a href="<?php echo $_product->getProductUrl();?>"><img src="<?php echo $imageUrl; ?>" ></a>
    <div class="primary header">

    <!-- <a class="product-name"> -->

       echo $_product->getName().'<br>';

       <!-- </a> -->

       <div class="price slide"> 
           echo '<strong>'.$specialPrice = $priceHelper->currency($_product->getFinalPrice(), true, false).'</strong>';


       <div class="secondary header">
           <form data-role="tocart-form" action="<?php echo $addToCartUrl; ?>" method="post"> 

        <?php echo $block->getBlockHtml('formkey')?>
        <div class="addtocart slide">
         <button type="submit" title="Add to Cart" class="action tocart primary">

           <!-- <span>Add to Cart</span> -->
           <img src="<?php echo $block->getViewFileUrl('Vendor_Modulename::images/m1.jpg'); ?>" alt="<?php echo __('Add to Cart'); ?>" title="<?php echo __('Add to Cart'); ?>" width="35" height="35" />Add to cart
         <div class="addto_wish_compare slide">
          <a href="#" data-post='<?php  echo $this->helper('Magento\Wishlist\Helper\Data')->getAddParams($_product) ?>' class="action towishlist" data-action="add-to-wishlist"><span>
    Add to Wish List</span></a>

          <a href="#" data-post='<?php  echo $this->helper('Magento\Catalog\Helper\Product\Compare')->getPostDataParams($_product);?>'
                class="action tocompare">

        <span><?php  echo __('Compare') ?></span>




My block file

    namespace Vendor\Modulename\Block;
    class ProductTab extends \Magento\Framework\View\Element\Template

      protected $_productloader;  

      protected $_productCollectionFactory;
      protected $_productVisibility;
      protected $_orderCollectionFactory;
      protected $orders;
      protected $_scopeConfig;

      public $_listBlock;
      public $_priceHelper;
      public $_imageUrl;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,

       \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,

         \Magento\Catalog\Model\ProductFactory $_productloader,

    \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,

    \Magento\Catalog\Model\Product\Visibility $productVisibility,

    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,

    //These variables Used in phtml file
    \Magento\Catalog\Block\Product\ListProduct $listBlock,

   \Magento\Framework\Pricing\Helper\Data $priceHelper,

   \Magento\Catalog\Helper\Image $imageUrl,

    array $data = []

    $this->_productCollectionFactory = $productCollectionFactory;

    $this->_productloader = $_productloader;

     $this->_orderCollectionFactory = $orderCollectionFactory;

    $this->_productVisibility = $productVisibility;

    $this->_scopeConfig = $scopeConfig;

    $this->_listBlock = $listBlock;
    $this->_priceHelper = $priceHelper;
    $this->_imageUrl = $imageUrl;

    parent::__construct($context, $data);

    public function _prepareLayout()
        return parent::_prepareLayout();

public function getProductCollection()

    if (!$this->orders) {
        $this->orders = $this->_orderCollectionFactory->create()->addFieldToSelect('*');

    return $this->orders;

 public function getLoadProduct($id)
        return $this->_productloader->create()->load($id);


Getting Output after clicking of addtocart button

enter image description here

Actually, when I checked at checkout page, the product had added successfully

enter image description here

My problem has why the product has not appeared in the shopping cart after clicking of addtocart button.

2 Answers 2


add section.xml to etc/frontend in your custom module and copy paste below code.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <action name="[frontName]/[ActionPath]/[ActionName]">
        <section name="cart"/>

after adding above file run the below commands.

php bin/magento cache:flush and php bin/magento cache:clean.

Now hit the Add to Cart button it will update the shopping cart forcefully.

  • In your code you mention ** [frontName]/[ActionPath]/[ActionName]** . How to identify these because in my module i didn't use these things. All the things are core only.
    – Pinku
    Commented Jan 2, 2017 at 17:45
  • @Pinku, are you extending catalog Module?
    – Bojjaiah
    Commented Jan 3, 2017 at 5:30
  • I extending my block class from \Magento\Framework\View\Element\Template and from this class of constructor i creating object of \Magento\Catalog\Block\Product\ListProduct
    – Pinku
    Commented Jan 3, 2017 at 5:42
  • @Pinku, your module don't have controller? where did you showing products? otherwise post your sample code with file path? it will help to find bug?
    – Bojjaiah
    Commented Jan 3, 2017 at 5:51
  • Yes in my module there is no controller. I am showing products at home page. And the file path is
    – Pinku
    Commented Jan 3, 2017 at 6:05

Just add :

    <script type="text/x-magento-init">
        "[data-role=tocart-form], .form.map.checkout": {
        "catalogAddToCart": {}

in bottom of your file .phtml Or Download Free Owl Carousel Slider and explore the code it's have the same technique

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.