I tried with button add to cart and it not working,


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

<button class="action description-button" data-post='<?php echo $postData; ?>'
<type="button" title="<?php echo __('Add to cart') ?>">
    <span><?php echo __('Add to cart') ?></span>

data-post button have action is null ? It look like :


Please help me, why it not work?

  • please share your block code. – kunj Jun 24 '18 at 17:46

Create form in your template file and pass action for the same .Also you need to pass formkey .

Try below code :

<form  data-role="tocart-form" action="<?php echo $block->getAddToCartUrl($_item); ?>" method="post">            
<?php echo $block->getBlockHtml('formkey'); ?>
<button type="submit" title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>" class="action tocart primary" style="float:right;margin-left: 10px;">
<span><?php /* @escapeNotVerified */echo __('Add to Cart'); ?></span>
  • thank, but it not work, action form not have url. – M.Jack Feb 3 '17 at 8:42
  • @M.Jack so what you want to achieve ? you want add to cart button in your template file ? – Manthan Dave Feb 3 '17 at 8:44
  • my issue is product not add to cart. It not work when click add to cart button.Can you hepl me ? – M.Jack Feb 3 '17 at 8:51
  • Yes put my code when you need this button and it will add product to the cart .ultimately you need to take form with the action so paste my code when you need it – Manthan Dave Feb 3 '17 at 9:06
  • Yes, I use code of you but it not show in minicart and check cart page is empty. – M.Jack Feb 3 '17 at 9:27

all data-post attributes has to be with simple quotation marks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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