Passing the Product id and form key but not adding to cart in magento2Product not adding to cartAdd to cart url do not workAdd to cart url returns invalid urlError on cart and checkout In magento2Magento 2: How to get add to cart url in custom templete file and add product to cartCan not add product to cart rest APIuenc in the urlMagento 2.2.2 On product save i get error ''Invalid Form Key. Please refresh the page'Error on checkout page while adding product to cartAdd a product to the cart via query string without form_key parameter in magento2

Expand and Contract

How would I stat a creature to be immune to everything but the Magic Missile spell? (just for fun)

Gatling : Performance testing tool

What about the virus in 12 Monkeys?

Solving a recurrence relation (poker chips)

Saudi Arabia Transit Visa

If human space travel is limited by the G force vulnerability, is there a way to counter G forces?

What does the expression "A Mann!" means

Why didn't Boeing produce its own regional jet?

Why do I get two different answers for this counting problem?

Dreadful Dastardly Diseases, or Always Atrocious Ailments

How do I gain back my faith in my PhD degree?

Should I cover my bicycle overnight while bikepacking?

Is it acceptable for a professor to tell male students to not think that they are smarter than female students?

Arrow those variables!

ssTTsSTtRrriinInnnnNNNIiinngg

Do UK voters know if their MP will be the Speaker of the House?

Mathematica command that allows it to read my intentions

Venezuelan girlfriend wants to travel the USA to be with me. What is the process?

Im going to France and my passport expires June 19th

How do I handle a potential work/personal life conflict as the manager of one of my friends?

Size of subfigure fitting its content (tikzpicture)

Is it logically or scientifically possible to artificially send energy to the body?

How can I determine if the org that I'm currently connected to is a scratch org?



Passing the Product id and form key but not adding to cart in magento2


Product not adding to cartAdd to cart url do not workAdd to cart url returns invalid urlError on cart and checkout In magento2Magento 2: How to get add to cart url in custom templete file and add product to cartCan not add product to cart rest APIuenc in the urlMagento 2.2.2 On product save i get error ''Invalid Form Key. Please refresh the page'Error on checkout page while adding product to cartAdd a product to the cart via query string without form_key parameter in magento2













0















passing product id and form key through url but product not added to cart in magento2.



http://127.0.0.1/magento2.3/checkout/cart/add/uenc/aHR0cDovLzEyNy4wLjAuMS9waGlsaXBzX0hVRV8yXzAvZmVlZC9leHBvcnQvZXhlY3V0ZT9jYWxsYmFjaz1qUXVlcnkxMTI0MDM0MDU1MTE2ODg3NTA4NTNfMTU1NDEyNjcxMzYxMyZpZD0xOCZtb2RlPWNvbnRpbnVlJnJhbmQ9MC4wNTYyODUxODIzMTAyODkyMyZfPTE1NTQxMjY3MTM2MTUmaXNBamF4PXRydWU%2C/product/1/form_key/uiYQPpblGQ6hsldy









share|improve this question






















  • How you pass this? Write more.

    – Sohel Rana
    2 days ago











  • Generated the cart url and added the parameters (product id and form_key) but product is not added to cart

    – venkata prasad
    2 days ago















0















passing product id and form key through url but product not added to cart in magento2.



http://127.0.0.1/magento2.3/checkout/cart/add/uenc/aHR0cDovLzEyNy4wLjAuMS9waGlsaXBzX0hVRV8yXzAvZmVlZC9leHBvcnQvZXhlY3V0ZT9jYWxsYmFjaz1qUXVlcnkxMTI0MDM0MDU1MTE2ODg3NTA4NTNfMTU1NDEyNjcxMzYxMyZpZD0xOCZtb2RlPWNvbnRpbnVlJnJhbmQ9MC4wNTYyODUxODIzMTAyODkyMyZfPTE1NTQxMjY3MTM2MTUmaXNBamF4PXRydWU%2C/product/1/form_key/uiYQPpblGQ6hsldy









share|improve this question






















  • How you pass this? Write more.

    – Sohel Rana
    2 days ago











  • Generated the cart url and added the parameters (product id and form_key) but product is not added to cart

    – venkata prasad
    2 days ago













0












0








0


1






passing product id and form key through url but product not added to cart in magento2.



http://127.0.0.1/magento2.3/checkout/cart/add/uenc/aHR0cDovLzEyNy4wLjAuMS9waGlsaXBzX0hVRV8yXzAvZmVlZC9leHBvcnQvZXhlY3V0ZT9jYWxsYmFjaz1qUXVlcnkxMTI0MDM0MDU1MTE2ODg3NTA4NTNfMTU1NDEyNjcxMzYxMyZpZD0xOCZtb2RlPWNvbnRpbnVlJnJhbmQ9MC4wNTYyODUxODIzMTAyODkyMyZfPTE1NTQxMjY3MTM2MTUmaXNBamF4PXRydWU%2C/product/1/form_key/uiYQPpblGQ6hsldy









share|improve this question














passing product id and form key through url but product not added to cart in magento2.



http://127.0.0.1/magento2.3/checkout/cart/add/uenc/aHR0cDovLzEyNy4wLjAuMS9waGlsaXBzX0hVRV8yXzAvZmVlZC9leHBvcnQvZXhlY3V0ZT9jYWxsYmFjaz1qUXVlcnkxMTI0MDM0MDU1MTE2ODg3NTA4NTNfMTU1NDEyNjcxMzYxMyZpZD0xOCZtb2RlPWNvbnRpbnVlJnJhbmQ9MC4wNTYyODUxODIzMTAyODkyMyZfPTE1NTQxMjY3MTM2MTUmaXNBamF4PXRydWU%2C/product/1/form_key/uiYQPpblGQ6hsldy






magento2 url magento2.3






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









venkata prasadvenkata prasad

57112




57112












  • How you pass this? Write more.

    – Sohel Rana
    2 days ago











  • Generated the cart url and added the parameters (product id and form_key) but product is not added to cart

    – venkata prasad
    2 days ago

















  • How you pass this? Write more.

    – Sohel Rana
    2 days ago











  • Generated the cart url and added the parameters (product id and form_key) but product is not added to cart

    – venkata prasad
    2 days ago
















How you pass this? Write more.

– Sohel Rana
2 days ago





How you pass this? Write more.

– Sohel Rana
2 days ago













Generated the cart url and added the parameters (product id and form_key) but product is not added to cart

– venkata prasad
2 days ago





Generated the cart url and added the parameters (product id and form_key) but product is not added to cart

– venkata prasad
2 days ago










1 Answer
1






active

oldest

votes


















0














Magento 2.3 has stopped adding product to cart via GET request. It will only add product to cart if you pass your parameters like product id and other needed parameters in POST. If you are using GET method, it will simply redirect with error message.



As a workaround,you can create your custom module.
Add following 2 files to your custom module.




[Vendor]/[Module]/etc/frontend/routes.xml




<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="customaddtocart" frontName="customaddtocart">
<module name="[Vendor]_[Module]"/>
</route>
</router>
</config>



[Vendor]/[Module]/Controller/Cart/Add.php




<?php

namespace [Vendor][Module]ControllerCart;

class AddAction extends MagentoFrameworkAppActionAction

protected $storeManager;
protected $cart;
protected $productRepository;

public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoCheckoutModelCart $cart,
MagentoCatalogModelProductRepository $productRepository
)

parent::__construct($context);
$this->storeManager = $storeManager;
$this->cart = $cart;
$this->productRepository = $productRepository;


public function execute()

$product_id = $this->getRequest()->getParam('product');
$product = $this->getProductById($product_id);
$params = [];
$params['product'] = $product->getId();
$params['form_key'] = $this->getRequest()->getParam('form_key');
$params['qty'] = 1;
$this->cart->addProduct($product, $params);

// You can either redirect to some page or return json response from here


public function getProductById($id)

return $this->productRepository->getById($id, false, $this->storeManager->getStore()->getId());




Now you can call your controller like, http://yourwebsite.com/customaddtocart/cart/add/product/1/form_key/xxxxx






share|improve this answer

























  • is there any alternative to add product to cart through url yash shah

    – venkata prasad
    2 days ago











  • You have to create your custom controller and add product to cart via coding and call that controller. Let me know if you need a sample code.

    – Yash Shah
    2 days ago











  • yes can you share the sample code module

    – venkata prasad
    2 days ago











  • Edited the answer with sample code, Have not tested the code but it should work. Also replace [Vendor] and [Module] with your vendor and module name where needed.

    – Yash Shah
    2 days ago











  • it is required di.xml file right to override the add.php

    – venkata prasad
    2 days ago











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f268218%2fpassing-the-product-id-and-form-key-but-not-adding-to-cart-in-magento2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Magento 2.3 has stopped adding product to cart via GET request. It will only add product to cart if you pass your parameters like product id and other needed parameters in POST. If you are using GET method, it will simply redirect with error message.



As a workaround,you can create your custom module.
Add following 2 files to your custom module.




[Vendor]/[Module]/etc/frontend/routes.xml




<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="customaddtocart" frontName="customaddtocart">
<module name="[Vendor]_[Module]"/>
</route>
</router>
</config>



[Vendor]/[Module]/Controller/Cart/Add.php




<?php

namespace [Vendor][Module]ControllerCart;

class AddAction extends MagentoFrameworkAppActionAction

protected $storeManager;
protected $cart;
protected $productRepository;

public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoCheckoutModelCart $cart,
MagentoCatalogModelProductRepository $productRepository
)

parent::__construct($context);
$this->storeManager = $storeManager;
$this->cart = $cart;
$this->productRepository = $productRepository;


public function execute()

$product_id = $this->getRequest()->getParam('product');
$product = $this->getProductById($product_id);
$params = [];
$params['product'] = $product->getId();
$params['form_key'] = $this->getRequest()->getParam('form_key');
$params['qty'] = 1;
$this->cart->addProduct($product, $params);

// You can either redirect to some page or return json response from here


public function getProductById($id)

return $this->productRepository->getById($id, false, $this->storeManager->getStore()->getId());




Now you can call your controller like, http://yourwebsite.com/customaddtocart/cart/add/product/1/form_key/xxxxx






share|improve this answer

























  • is there any alternative to add product to cart through url yash shah

    – venkata prasad
    2 days ago











  • You have to create your custom controller and add product to cart via coding and call that controller. Let me know if you need a sample code.

    – Yash Shah
    2 days ago











  • yes can you share the sample code module

    – venkata prasad
    2 days ago











  • Edited the answer with sample code, Have not tested the code but it should work. Also replace [Vendor] and [Module] with your vendor and module name where needed.

    – Yash Shah
    2 days ago











  • it is required di.xml file right to override the add.php

    – venkata prasad
    2 days ago















0














Magento 2.3 has stopped adding product to cart via GET request. It will only add product to cart if you pass your parameters like product id and other needed parameters in POST. If you are using GET method, it will simply redirect with error message.



As a workaround,you can create your custom module.
Add following 2 files to your custom module.




[Vendor]/[Module]/etc/frontend/routes.xml




<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="customaddtocart" frontName="customaddtocart">
<module name="[Vendor]_[Module]"/>
</route>
</router>
</config>



[Vendor]/[Module]/Controller/Cart/Add.php




<?php

namespace [Vendor][Module]ControllerCart;

class AddAction extends MagentoFrameworkAppActionAction

protected $storeManager;
protected $cart;
protected $productRepository;

public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoCheckoutModelCart $cart,
MagentoCatalogModelProductRepository $productRepository
)

parent::__construct($context);
$this->storeManager = $storeManager;
$this->cart = $cart;
$this->productRepository = $productRepository;


public function execute()

$product_id = $this->getRequest()->getParam('product');
$product = $this->getProductById($product_id);
$params = [];
$params['product'] = $product->getId();
$params['form_key'] = $this->getRequest()->getParam('form_key');
$params['qty'] = 1;
$this->cart->addProduct($product, $params);

// You can either redirect to some page or return json response from here


public function getProductById($id)

return $this->productRepository->getById($id, false, $this->storeManager->getStore()->getId());




Now you can call your controller like, http://yourwebsite.com/customaddtocart/cart/add/product/1/form_key/xxxxx






share|improve this answer

























  • is there any alternative to add product to cart through url yash shah

    – venkata prasad
    2 days ago











  • You have to create your custom controller and add product to cart via coding and call that controller. Let me know if you need a sample code.

    – Yash Shah
    2 days ago











  • yes can you share the sample code module

    – venkata prasad
    2 days ago











  • Edited the answer with sample code, Have not tested the code but it should work. Also replace [Vendor] and [Module] with your vendor and module name where needed.

    – Yash Shah
    2 days ago











  • it is required di.xml file right to override the add.php

    – venkata prasad
    2 days ago













0












0








0







Magento 2.3 has stopped adding product to cart via GET request. It will only add product to cart if you pass your parameters like product id and other needed parameters in POST. If you are using GET method, it will simply redirect with error message.



As a workaround,you can create your custom module.
Add following 2 files to your custom module.




[Vendor]/[Module]/etc/frontend/routes.xml




<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="customaddtocart" frontName="customaddtocart">
<module name="[Vendor]_[Module]"/>
</route>
</router>
</config>



[Vendor]/[Module]/Controller/Cart/Add.php




<?php

namespace [Vendor][Module]ControllerCart;

class AddAction extends MagentoFrameworkAppActionAction

protected $storeManager;
protected $cart;
protected $productRepository;

public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoCheckoutModelCart $cart,
MagentoCatalogModelProductRepository $productRepository
)

parent::__construct($context);
$this->storeManager = $storeManager;
$this->cart = $cart;
$this->productRepository = $productRepository;


public function execute()

$product_id = $this->getRequest()->getParam('product');
$product = $this->getProductById($product_id);
$params = [];
$params['product'] = $product->getId();
$params['form_key'] = $this->getRequest()->getParam('form_key');
$params['qty'] = 1;
$this->cart->addProduct($product, $params);

// You can either redirect to some page or return json response from here


public function getProductById($id)

return $this->productRepository->getById($id, false, $this->storeManager->getStore()->getId());




Now you can call your controller like, http://yourwebsite.com/customaddtocart/cart/add/product/1/form_key/xxxxx






share|improve this answer















Magento 2.3 has stopped adding product to cart via GET request. It will only add product to cart if you pass your parameters like product id and other needed parameters in POST. If you are using GET method, it will simply redirect with error message.



As a workaround,you can create your custom module.
Add following 2 files to your custom module.




[Vendor]/[Module]/etc/frontend/routes.xml




<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="customaddtocart" frontName="customaddtocart">
<module name="[Vendor]_[Module]"/>
</route>
</router>
</config>



[Vendor]/[Module]/Controller/Cart/Add.php




<?php

namespace [Vendor][Module]ControllerCart;

class AddAction extends MagentoFrameworkAppActionAction

protected $storeManager;
protected $cart;
protected $productRepository;

public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoCheckoutModelCart $cart,
MagentoCatalogModelProductRepository $productRepository
)

parent::__construct($context);
$this->storeManager = $storeManager;
$this->cart = $cart;
$this->productRepository = $productRepository;


public function execute()

$product_id = $this->getRequest()->getParam('product');
$product = $this->getProductById($product_id);
$params = [];
$params['product'] = $product->getId();
$params['form_key'] = $this->getRequest()->getParam('form_key');
$params['qty'] = 1;
$this->cart->addProduct($product, $params);

// You can either redirect to some page or return json response from here


public function getProductById($id)

return $this->productRepository->getById($id, false, $this->storeManager->getStore()->getId());




Now you can call your controller like, http://yourwebsite.com/customaddtocart/cart/add/product/1/form_key/xxxxx







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









Yash ShahYash Shah

69618




69618












  • is there any alternative to add product to cart through url yash shah

    – venkata prasad
    2 days ago











  • You have to create your custom controller and add product to cart via coding and call that controller. Let me know if you need a sample code.

    – Yash Shah
    2 days ago











  • yes can you share the sample code module

    – venkata prasad
    2 days ago











  • Edited the answer with sample code, Have not tested the code but it should work. Also replace [Vendor] and [Module] with your vendor and module name where needed.

    – Yash Shah
    2 days ago











  • it is required di.xml file right to override the add.php

    – venkata prasad
    2 days ago

















  • is there any alternative to add product to cart through url yash shah

    – venkata prasad
    2 days ago











  • You have to create your custom controller and add product to cart via coding and call that controller. Let me know if you need a sample code.

    – Yash Shah
    2 days ago











  • yes can you share the sample code module

    – venkata prasad
    2 days ago











  • Edited the answer with sample code, Have not tested the code but it should work. Also replace [Vendor] and [Module] with your vendor and module name where needed.

    – Yash Shah
    2 days ago











  • it is required di.xml file right to override the add.php

    – venkata prasad
    2 days ago
















is there any alternative to add product to cart through url yash shah

– venkata prasad
2 days ago





is there any alternative to add product to cart through url yash shah

– venkata prasad
2 days ago













You have to create your custom controller and add product to cart via coding and call that controller. Let me know if you need a sample code.

– Yash Shah
2 days ago





You have to create your custom controller and add product to cart via coding and call that controller. Let me know if you need a sample code.

– Yash Shah
2 days ago













yes can you share the sample code module

– venkata prasad
2 days ago





yes can you share the sample code module

– venkata prasad
2 days ago













Edited the answer with sample code, Have not tested the code but it should work. Also replace [Vendor] and [Module] with your vendor and module name where needed.

– Yash Shah
2 days ago





Edited the answer with sample code, Have not tested the code but it should work. Also replace [Vendor] and [Module] with your vendor and module name where needed.

– Yash Shah
2 days ago













it is required di.xml file right to override the add.php

– venkata prasad
2 days ago





it is required di.xml file right to override the add.php

– venkata prasad
2 days ago

















draft saved

draft discarded
















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f268218%2fpassing-the-product-id-and-form-key-but-not-adding-to-cart-in-magento2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Sum ergo cogito? 1 nng

419 nièngy_Soadمي 19bal1.5o_g

Queiggey Chernihivv 9NnOo i Zw X QqKk LpB