0

I have a Magento 2.2.9 based website and a cms page created that called in a phtml file.

In that phtml file, I am wanting to have an add to cart button for a specific product. However, clicking the button does not seem to add the item to my cart. I am wondering if anyone could suggest changes tome code get it to work.

        <?php 
            $sku ='KACI01';
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
            $productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
            $product = $productRepository->get($sku);
            $productid = $product->getId();
            $cartHelper = $this->helper('Magento\Checkout\Helper\Cart');
            $addToCartUrl = $cartHelper->getAddUrl($product);

            $postDataHelper = $this->helper('Magento\Framework\Data\Helper\PostHelper');
            $postData = $postDataHelper->getPostData($block->getAddToCartUrl($product), ['product' => $productid])
        ?>

    <form data-role="tocart-form" action="<?php echo $addToCartUrl; ?>" method="post"> 
       <?php echo $block->getBlockHtml('formkey')?>
       <button class="action tocart primary" data-post='<?php echo $postData; ?>' type="button" title="Add to Bag">
          <span>Add to Bag</span>
       </button>
    </form>

1 Answer 1

2

I've updated your code. Please give a try with below code in your phtml file.

<?php 
    $sku ='KACI01';
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
    $productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
    $product = $productRepository->get($sku);
    $productid = $product->getId();
    //$cartHelper = $this->helper('Magento\Checkout\Helper\Cart');
    //$addToCartUrl = $cartHelper->getAddUrl($product);
    $listBlock = $objectManager->get('\Magento\Catalog\Block\Product\ListProduct');
    $addToCartUrl =  $listBlock->getAddToCartUrl($product);

    $postDataHelper = $this->helper('Magento\Framework\Data\Helper\PostHelper');
    $postData = $postDataHelper->getPostData($block->getAddToCartUrl($product), ['product' => $productid]);
?>

<form data-role="tocart-form" action="<?php echo $addToCartUrl; ?>" method="post"> 
    <?php echo $block->getBlockHtml('formkey')?>
    <div class="btn">
        <button type="submit" title="Add to Cart" class="action tocart primary">
            <span>Add to Cart</span>
        </button>
    </div>   
</form>

Reference: https://www.offset101.com/magento-2-get-add-cart-button-custom-templete-file/

Hope it helps!!!

3
  • That works. I noticed it only works on our staging and live server not on my local MAMP environment which is odd.
    – Jayreis
    Commented Sep 10, 2019 at 18:43
  • 1
    thank you @sumit
    – Jayreis
    Commented Sep 11, 2019 at 19:42
  • how can use instead SKU, product ID from configuration page?
    – Lerry
    Commented Aug 8, 2021 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.