3

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

app/code/Moo/Custom/view/product.phtml

<?php 
$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>
</button>

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

data-post="{"action":null,"data":{"product":"7","uenc":"aHR0cDovLzUyLjE4My4yNi45OC9hcmR6YW5fc3VwZXJtYXJrZXQvaW5kZXgvbG9hZHByb2R1Y3Qv"}}"

Please help me, why it not work?

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

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>
</button>
</form>
  • 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
0

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.