I added your code like this:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<!--define id for frontend route is “standard”-->
<router id="standard">
<!--Route name-->
<route id="Post" frontName="Post">
<!--Module name-->
<module name="MassiveArt_ShoppingCart" />
</route>
</router>
</config>But I am not sure what i need to add in the orute id and frontName.
This is the Code of the Controller if you need it:This is my file structure<?php
namespace MassiveArt\ShoppingCart\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Checkout\Model\Cart;
use Magento\Catalog\Model\Product;
class Post extends Action
{
protected $formKey;
protected $cart;
protected $product;
public function __construct(
Context $context,
JsonFactory $resultJsonFactory,
FormKey $formKey,
Cart $cart,
Product $product) {
$this->formKey = $formKey;
$this->resultJsonFactory = $resultJsonFactory;
$this->cart = $cart;
$this->product = $product;
parent::__construct($context);
}
public function execute()
{
$result = $this->resultJsonFactory->create();
$productId = $this->getRequest()->getParam('productId');
try {
$params = array(
'form_key' => $this->formKey->getFormKey(),
'product' => $productId,
'qty' => 1
);
$product = $this->product->load($productId);
$this->cart->addProduct($product, $params);
$this->cart->save();
$result->setData(['message' => __("Product is added in cart")]);
return $result;
} catch(\Exception $e) {
$result->setData(['error' => __($e->getMessage())]);
return $result;
}
}
}
Please change Post as "post" (small letter) and change your button_getcart.phtml as
jQuery("#getCart").on("click", function(){
jQuery.ajax({
url:"http://192.168.10.106:8080/api/cart/",
type:"GET", //First change type to method here
headers: {
'X-Requested-With': 'XMLHttpRequest',
'Accept': '*/*',
'Cache-Control':'no-cache',
},
contentType: "application/json; charset=utf-8",
data:
{
"_customerID": 1, //for tests
}
success:function(response) {
jQuery.ajax({
type: "post",
url: <?= $block->getUrl('post/index/post') ?>,
data: {
productId: response.Product_ID
}
success: function (data) {
console.log(data);
}
});
},
error:function(){
alert("An Error occurred. Try again later");
}
});
}
});Note: I hope you have created the folder in MassiveArt\ShoppingCart\Controller\Index folder. Hope this will work for you. If still you face any issue please let me know.
Regards
I will test this tomorrow thank you very much
Good Morning, I now tested if it works but it still has error 404 not found, if i try to get to the url, you posted [$block->getUrl('post/index/post'), which is http://localhost/post/index/post/ in my case, manually I also get the error 404.
Good Morning,
Please try to use $block->getBaseUrl().'/post/index/post'
and then check the url you are getting by $block->getBaseUrl().'/post/index/post'
If still you face any issue, please let me know.
Regards
Now I get this URL: http://localhost//post/index/post, but still I get the error 404
As I cant post a image, please follow this link: https://imgur.com/a/XkfgJU8
My base URL is localhost, the two links, you sended didn't work. I tried: http://localhost/magento/post/index/post as well as http://localhost/post/index/post