I will send you my files again:
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"> <!--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>
Index.php:
<?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; } } }
button_getcart.phtml:
<div class="primary"> <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $customerSession = $objectManager->create("Magento\Customer\Model\Session"); function testtest(){ $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $productId = 1; $product = $objectManager->create('\Magento\Catalog\Model\Product')->load($productId); $cart = $objectManager->create('Magento\Checkout\Model\Cart'); $formKey = $objectManager->create('\Magento\Framework\Data\Form\FormKey')->getFormKey(); $option = array('469'=>459); $params = array( 'form_key' => $formKey, 'product' => $productId, //product Id 'qty' =>10, //quantity of product 'options' => $option ); $cart->addProduct($product, $params); $cart->save(); } if($customerSession->isLoggedIn()) { echo '<button id="getcart" style="width:100%;" type="button" class="action primary checkout">Return your saved cart</button>'; } ?> </div> <script type="text/javascript"> require(["jquery","domReady!"], function (jQuery) { jQuery("#getcart").on("click", function(){ var myurl = <?php echo json_encode($block->getBaseUrl().'/post/index/post') ?>; console.log(myurl); var postelement = { _customerID: <?php echo $customerSession->getCustomerId(); ?>, _smartContractAddress: <?php echo json_encode($block->getContractAddress()); ?>, }; jQuery.ajax({ url:"http://192.168.10.105:8080/api/getcart/", type:"POST", //First change type to method here headers: { 'X-Requested-With': 'XMLHttpRequest', 'Accept': '*/*', 'Cache-Control':'no-cache', "Authorization": "Basic " + btoa(<?php echo json_encode($block->getContractAddress()); ?> + ":" + <?php echo json_encode($block->getPassword()); ?>), }, contentType: "application/json; charset=utf-8", data: JSON.stringify(postelement), success:function(response) { jQuery.ajax({ type: "POST", url: myurl, data: { productId: response._productId[1] }, success: function (data){ console.log(data); }, error: function(data, data1){ console.log(data); }, }); }, error:function(data, data1, data2){ alert("An Error occurred. Try again later"); } }); require([ 'Magento_Customer/js/customer-data' ], function (customerData) { var sections = ['cart']; customerData.invalidate(sections); customerData.reload(sections, true); }); }); }); </script>
Please pass static url in your javascript's var myurl
That is something like var myurl = "http://localhost/magento/post/index/post"
Change http://localhost/magento with you magento base url.
Regards
As I already told, this tatic url is also not working for me, I am now passing http://localhost/post/index/post, but it didnt work. Somehow i just don't find the url to the controller and I don't know why. I already tried a lot of links, but i always get the error 404.
Hello, do you have any solution for this? Otherwise i will try to make something completely else, as i got another reply in a other forum.
I have no homepageurl for you, as I am running Magento local on my virtual maschine.
You can see it here: