1

I am getting add to cart url by product object like below.

Block file.

 use Magento\Catalog\Api\ProductRepositoryInterface;     
 use \Magento\Checkout\Helper\Cart as CartHelper;
 class CustomAdd extends \Magento\Framework\View\Element\Template
  { 
   protected $_productRepository; 
   protected $cartHelper;
   public function __construct(
      ProductRepositoryInterface $productRepository,
      )
     {   
      $this->_productRepository = $productRepository;
       $this->cartHelper = $cartHelper;
     }

   public function getAddToCartUrlForProduct($productSku)
   {
     $product = $this->_productRepository->get($productSku);
     return $this->cartHelper->getAddUrl($product);
   }
 }

Using like below in template

   <?php 
    $addtoCartUrl = $block->getAddToCartUrlForProduct(11101);
   ?>
  <div class="col-md-3">
        <div class="checkout-sec">
            <a href=""><img src="" alt="" /></a>                
            <p><a href="<?php echo $addtoCartUrl; ?>" class="btn-addtocart">Add to Cart</a></p>
        </div>
    </div>  

This is not adding the product to cart. can anyone please suggest me . Where i am wrong here. Thanks

  • Are you getting correct add to cart url? – Sukumar Gorai Dec 18 '18 at 7:55
  • @SukumarGorai, yes i got like below, cart/add/uenc/aHR0cDovL2VjMi0zNC0yNDQtNTctMjEwLmV1LXdlc3QtMS5jb21wdXRlLmFtYXpvbmF3cy5jb20vQXNobW9sZWFuL2NoZWNrb3V0L2NhcnQv/product/7967/in_cart/1/, but its not adding when i add it – jafar pinjar Dec 18 '18 at 7:57
  • Can you check my answer? – Sukumar Gorai Dec 18 '18 at 8:36
  • @SukumarGorai, tried but not worked for me – jafar pinjar Dec 18 '18 at 10:02
  • @SukumarGorai, 2.2.6 is my magento version, have u checked it? – jafar pinjar Dec 18 '18 at 10:51
1

Try the below code instead of yours:

class CustomAdd extends \Magento\Framework\View\Element\Template
{ 
  protected $_productRepository; 
  protected $_listProduct;
  public function __construct(
    \Magento\Catalog\Model\ProductRepository $productRepository,
    \Magento\Catalog\Block\Product\ListProduct $listProduct
  )
  {   
    $this->_productRepository = $productRepository;
    $this->_listProduct = $listProduct;
  }

  public function getAddToCartUrlForProduct($id)
  {
    $product = $this->_productRepository->getById($id);
    return $this->_listBlock->getAddToCartUrl($product);
  }
}

Run di:compile and other required commands and test.

  • tried your solution, its not worked for me, replaced _listBlock to _listProduct, still not worked – jafar pinjar Dec 18 '18 at 9:58
  • What is the error you are getting? – Sukumar Gorai Dec 18 '18 at 10:14
  • no error, page will refresh once add to cart is clicked, but product not added to cart – jafar pinjar Dec 18 '18 at 10:17
  • Can you try it in private browser? – Sukumar Gorai Dec 18 '18 at 10:18
  • yes tried in private window also, same issue – jafar pinjar Dec 18 '18 at 10:20

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.