TransWikia.com

magento 2 product add to cart URL doesnt have a form key

Magento Asked by mendz on February 1, 2021

I am trying to get the cart add to cart URL in magento 2 but the URL being returned by the getAddToCartUrl is not valid.

public function __construct(
   MagentoCatalogBlockProductListProduct $listBlock,
   MagentoCheckoutHelperCart $cartHelper
){
    $this->listBlock = $listBlock;
    $this->cartHelper = $cartHelper;
}

public funtion getUrl($product){ //this is a product instance
  //both this functions are returning the same
  $addUrl = $this->cartHelper->getAddUrl($product);
  $addUrl = $this->listBlock->getAddToCartUrl($product);

  return $addUrl;
}

the return URL are like this one.

http://$my_base_url/checkout/cart/add/uenc/$uenc_value/product/2772/

The generated URL doesn’t work when adding to cart. I also observed in M1 there should be a form_key parameter that should also be passed. anyone has an idea what wrong here? is there a new way to get a valid add to cart URL in M2?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP