I have a website based on Magento2.2.9 and a cms page created that called in a phtml file.
In that phtml file, I want to have a button to add to the cart for a specific product. However, clicking on the button does not seem to add the item to my cart. I wonder if anyone could suggest changes in the code to make it work.
get('MagentoCatalogModelProductRepository'); $product = $productRepository->get($sku); $productid = $product->getId(); $cartHelper = $this->helper('MagentoCheckoutHelperCart'); $addToCartUrl = $cartHelper->getAddUrl($product); $postDataHelper = $this->helper('MagentoFrameworkDataHelperPostHelper'); $postData = $postDataHelper->getPostData($block->getAddToCartUrl($product), ('product' => $productid)) ?>