0

My template file code is

$listBlock = $this->_listBlock;
     $addToCartUrl =  $listBlock->getAddToCartUrl($_product);
     ?>
     <form data-role="tocart-form" action="<?php echo $addToCartUrl; ?>" method="post"> 

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

       <span>Add to Cart</span>
     </button>
 </form>

My Block code is

<?php
namespace Vendor\Modulename\Block;
class AddProduct extends \Magento\Framework\View\Element\Template
{

  protected $_productCollectionFactory;
   public $_listBlock;

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

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

    \Magento\Catalog\Block\Product\ListProduct $listBlock,
    array $data = []
)
{


    $this->_productCollectionFactory = $productCollectionFactory;

    $this->_listBlock = $listBlock;

    parent::__construct($context, $data);
}
}
3
  • can u please share your block class code?
    – Amit Bera
    Commented Dec 29, 2016 at 9:01
  • I am getting message like You added Harmony Lumaflex™ Strength Band Kit to your shopping cart.. But it's not adding to the shopping cart. Please can you guide me, where exactly i'm wrong
    – Pinku
    Commented Dec 29, 2016 at 9:29
  • @Pinku Did you get your point?
    – zus
    Commented Aug 17, 2018 at 7:56

1 Answer 1

0

Need some change at block class

  • change extends class

    \Magento\Framework\View\Element\Template

to

 \Magento\Catalog\Block\Product\AbstractProduct

Add implements class Magento\Framework\DataObject\IdentityInterface;

Add add to cart function

 public function getAddToCartPostParams(\Magento\Catalog\Model\Product $product)
    {
        $url = $this->getAddToCartUrl($product);
        return [
            'action' => $url,
            'data' => [
                'product' => $product->getEntityId(),
                \Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED =>
                    $this->urlHelper->getEncodedUrl($url),
            ]
        ];
    }
  • Change template file

change template to Magento_Catalog::product/list.phtml

Modified class

<?php
namespace Vendor\Modulename\Block;
/* add some class */
use Magento\Catalog\Model\Product;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\DataObject\IdentityInterface;

class AddProduct extends \Magento\Catalog\Block\Product\AbstractProduct implements IdentityInterface
{
....
 public function getAddToCartPostParams(\Magento\Catalog\Model\Product $product)
    {
        $url = $this->getAddToCartUrl($product);
        return [
            'action' => $url,
            'data' => [
                'product' => $product->getEntityId(),
                \Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED =>
                    $this->urlHelper->getEncodedUrl($url),
            ]
        ];
    }

  public function getLoadedProductCollection()
    {
        return  $this->_productCollectionFactory;
    }

}
3
  • Sorry but i can not change extend class because it will effect my other work. so extend class should be \Magento\Framework\View\Element\Template
    – Pinku
    Commented Dec 29, 2016 at 9:42
  • it my mistake extends class should be ` \Magento\Catalog\Block\Product\AbstractProduct` and \Magento\Catalog\Block\Product\AbstractProduct extends \Magento\Framework\View\Element\Template
    – Amit Bera
    Commented Dec 29, 2016 at 11:05
  • can you pls explain me clearly what to extend. I get abstract error
    – Ramesh KR
    Commented Jan 19, 2018 at 11:41

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.