Hi guys,
I'm looking at upgrading from Magento 1 to Magento 2, but I need a way to add a product to the cart from an external site.
I use a simple CMS for all other website content, including product pages for ease & speed of development. With Magento 1, I could use a URL like the below to add a product to the cart from the CMS site:
/checkout/cart/add/?product=101
I did have to disable CSRF to get this working, but it did work nicely. I see the setting for disabling CSRF has been removed in Magento 2, so how can I achieve a similar thing using Magento 2?
Solved! Go to Solution.
Perfect, that did the trick, thank you! My final code is below:
<?php
namespace moduleName\addtobasket\Controller\Product;
class Index extends \Magento\Framework\App\Action\Action {
/**
* @var \Magento\Checkout\Model\Cart
*/
protected $cart;
/**
* @var \Magento\Catalog\Model\Product
*/
protected $product;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Catalog\Model\Product $product,
\Magento\Checkout\Model\Cart $cart
) {
$this->resultPageFactory = $resultPageFactory;
$this->cart = $cart;
$this->product = $product;
parent::__construct($context);
}
public function execute()
{
try {
$params = array();
$params['qty'] = '1';//product quantity
/* Get product id from a URL like /addtobasket/product?id=1,2,3 */
$pIds = explode(',',$_GET['id']);
foreach($pIds as $value) {
$_product = $this->product->load($value);
if ($_product) {
$this->cart->addProduct($_product, $params);
$this->cart->save();
}
}
$this->messageManager->addSuccess(__('Add to cart successfully.'));
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addException(
$e,
__('%1', $e->getMessage())
);
} catch (\Exception $e) {
$this->messageManager->addException($e, __('error.'));
}
/*cart page*/
$this->getResponse()->setRedirect('/checkout/cart/index');
}
}
Hello @AlexGillon,
You need to create a new route URL to add product in cart after create a router, Add below code in it.
<?php
namespace VendorName\ModuleName\Controller\Atc;
class Index extends \Magento\Framework\App\Action\Action {
/**
* @var \Magento\Checkout\Model\Cart
*/
protected $cart;
/**
* @var \Magento\Catalog\Model\Product
*/
protected $product;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Catalog\Model\Product $product,
\Magento\Checkout\Model\Cart $cart
) {
$this->resultPageFactory = $resultPageFactory;
$this->_customerSession = $customerSession;
$this->cart = $cart;
$this->product = $product;
parent::__construct($context);
}
public function execute()
{
try {
$params = array();
$params['qty'] = '1';//product quantity
/*get product id*/
$pId = '1';//productId
$_product = $this->product->load($pId);
if ($_product) {
$this->cart->addProduct($_product, $params);
$this->cart->save();
}
$this->messageManager->addSuccess(__('Add to cart successfully.'));
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addException(
$e,
__('%1', $e->getMessage())
);
} catch (\Exception $e) {
$this->messageManager->addException($e, __('error.'));
}
/*cart page*/
//$this->getResponse()->setRedirect('/checkout/cart/index');
}
}--
If the issue is solved, Click Kudos & Accept as Solution
Hi @gelanivishal, thank you so much for your quick reply!
I've never done customisation of this type on Magento though, so I'm afraid I'm still a little confused. I found the below URL which helped me understand routers, but it talks about a module, which I've never created before.
https://devdocs.magento.com/guides/v2.2/extension-dev-guide/routing.html
Exactly what do I need to do to get this working?
Hello @AlexGillon,
Here is an example of creating simple router https://www.magestore.com/magento-2-tutorial/magento-2-modules/
Suppose, you have created this type of router http://<magento_url>/hellomagento/index/index
then you have to use http://<magento_url>/hellomagento/index/index?product=101 URL instend of /checkout/cart/add/?product=101.
If you've found one of my answers useful, please give Kudos and Accept as Solution
Thank you @gelanivishal, that link enabled me to get a module up and running perfectly!
However, the original code that you send me seems to be throwing an error. I've enabled developer mode and found the below:
Exception #0 (Exception): Notice: Undefined variable: customerSession in /home/*********/public_html/app/code/myModule/addtobasket/Controller/Product/Index.php on line 22
Any ideas how to fix this?
Hello AlexGillon
Please remove "$this->_customerSession = $customerSession;" and after run php bin/magento setup:di:compile
<?php
namespace VendorName\ModuleName\Controller\Atc;
class Index extends \Magento\Framework\App\Action\Action {
/**
* @var \Magento\Checkout\Model\Cart
*/
protected $cart;
/**
* @var \Magento\Catalog\Model\Product
*/
protected $product;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Catalog\Model\Product $product,
\Magento\Checkout\Model\Cart $cart
) {
$this->resultPageFactory = $resultPageFactory;
$this->cart = $cart;
$this->product = $product;
parent::__construct($context);
}
public function execute()
{
try {
$params = array();
$params['qty'] = '1';//product quantity
/*get product id*/
$pId = '1';//productId
$_product = $this->product->load($pId);
if ($_product) {
$this->cart->addProduct($_product, $params);
$this->cart->save();
}
$this->messageManager->addSuccess(__('Add to cart successfully.'));
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addException(
$e,
__('%1', $e->getMessage())
);
} catch (\Exception $e) {
$this->messageManager->addException($e, __('error.'));
}
/*cart page*/
//$this->getResponse()->setRedirect('/checkout/cart/index');
}
}
If the issue is solved, Click Kudos & Accept as Solution
Perfect, that did the trick, thank you! My final code is below:
<?php
namespace moduleName\addtobasket\Controller\Product;
class Index extends \Magento\Framework\App\Action\Action {
/**
* @var \Magento\Checkout\Model\Cart
*/
protected $cart;
/**
* @var \Magento\Catalog\Model\Product
*/
protected $product;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Catalog\Model\Product $product,
\Magento\Checkout\Model\Cart $cart
) {
$this->resultPageFactory = $resultPageFactory;
$this->cart = $cart;
$this->product = $product;
parent::__construct($context);
}
public function execute()
{
try {
$params = array();
$params['qty'] = '1';//product quantity
/* Get product id from a URL like /addtobasket/product?id=1,2,3 */
$pIds = explode(',',$_GET['id']);
foreach($pIds as $value) {
$_product = $this->product->load($value);
if ($_product) {
$this->cart->addProduct($_product, $params);
$this->cart->save();
}
}
$this->messageManager->addSuccess(__('Add to cart successfully.'));
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addException(
$e,
__('%1', $e->getMessage())
);
} catch (\Exception $e) {
$this->messageManager->addException($e, __('error.'));
}
/*cart page*/
$this->getResponse()->setRedirect('/checkout/cart/index');
}
}