cancel
Showing results for 
Search instead for 
Did you mean: 

Getting the add to cart button to redirect/post

SOLVED

Getting the add to cart button to redirect/post

I've got a 3rd party extension that implements the following:

 

For certain products the customer is sent from the product details page to a special page as an extra step. This works perfectly from the category and list views, but not from the product details page. The correct custom URL is posted to, but it uses AJAX instead of a real POST, so it doesn't actually display the page. I need to fix or workaround that.


According to docs and forums this can be achieved by passing bindSubmit=false to the init of validate-product.js.

Here's what I tried. It doesn't seem to have any effect at all.

 

  • I've confirmed that my version of addtocart.phtml is being used.
  • I've confirmed that bindSubmit=false is being passed into the js (using debugger)
  • Developer mode with full page, layout and block cache disabled
  • false + false is deliberate to see if it works at all.
  • This is 2.3.3. In 2.3.2. Magento_Catalog/product/view/validation was used instead of Magento_Catalog/js/validate-product
  • I don't see this x-magento-init appear in the page specific [hash].js file. Am I right to assume that it should be?

 

# addtocart.phtml

<?php $postOrAjaxBool = $_product->getSpecificSomething() ? 'false' : 'false'; ?>
<script type="text/x-magento-init"> { "#product_addtocart_form": { "Magento_Catalog/js/validate-product": { "bindSubmit": <?=$postOrAjaxBool?> } } } </script>
# Rendered HTML

<div class="product-add-form">
    <form data-product-sku="some product" action="https://dev.someurl/modulename/cart/edit/product/4311/" method="post" id="product_addtocart_form" novalidate="novalidate">
        <input type="hidden" name="product" value="4311">
        <input type="hidden" name="selected_configurable_option" value="">
        <input type="hidden" name="related_product" id="related-products-field" value="">
        <input type="hidden" name="item" value="4311">
        <input name="form_key" type="hidden" value="yakkitysmack">                            <!-- thisoneisused addtocart.phtml bindsubmit = true -->

    <div class="box-tocart">
         <div class="field qty">
                    SNIP
        </div>
     </div>
     <div class="actions">
                    <button type="submit" title="Bewerken" class="action primary tocart" id="product-addtocart-button">
                        <span>Bewerken</span>
                    </button>
                    
SNIP

                </div>
            </div>
   
        
  </form>
</div>

No for the actual questions:

Why doesn't this work?
Is validate-product indeed the right way to go at it or am I missing something?

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Getting the add to cart button to redirect/post

I worked around this by not running the x-magento-init at all for the applicable products.

Apparently the bindSubmit works differently than expected:

bindSubmit = true means the page reloads and shows any validation or success messages.

bindSubmit = false does not reload the page.

But it's always an AJAX request.

 

I probably bypassed the validation that way, but I'll try to reanable that by adding the init for Magento_Catalog/product/view/validation.

View solution in original post

1 REPLY 1

Re: Getting the add to cart button to redirect/post

I worked around this by not running the x-magento-init at all for the applicable products.

Apparently the bindSubmit works differently than expected:

bindSubmit = true means the page reloads and shows any validation or success messages.

bindSubmit = false does not reload the page.

But it's always an AJAX request.

 

I probably bypassed the validation that way, but I'll try to reanable that by adding the init for Magento_Catalog/product/view/validation.