0

We have a second add to cart button on our product page, using the following code, see below.

But when a related product is checked and this second add to cart button is clicked, the related product is not added to the cart. When we use the default add to cart button on the product page, the related product is added successfully to the cart.

I guess it has something to do with the duplicate of the data-role="tocart-form", but I can not find the related code and solution.

Any idea how we can solve this issue?

        <div class="box-tocart">
        <form data-role="tocart-form" id="product_addtocart_form_sticky" class="product_addtocart_form_sticky" action="<?php echo $block->getAddToCartUrl($product); ?>" method="post">
        <?php echo $block->getBlockHtml('formkey')?>
            <button type="submit" title="Add to Cart" class="action tocart primary small-button">
                <span><i class="fad fa-shopping-cart"></i><?= /* @escapeNotVerified */ __('Add to Cart') ?></span>
            </button>  
        </form>
        </div>
1

I'd check the core-code for id="product_addtocart_form".

Most likely, there's a submit function, and you have to apply it to id="product_addtocart_form_sticky" as well.

EDIT

If you look into vendor/magento/module-catalog/view/frontend/templates/product/view/form.phtml, you see that there's a hidden input field <input type="hidden" name="related_product" id="related-products-field" value="" />

I'd search the codebase where this field is populated, and go on from there.

4
  • Thanks! Any idea where to add this part to the submit function?
    – JGeer
    Jun 15 '21 at 13:59
  • See my edited answer Jun 15 '21 at 15:43
  • Thanks! Seems that we indeed missed the <input type="hidden" name="related_product" id="related-products-field" value="" /> in the second form. Added this part and now it works perfect.
    – JGeer
    Jun 15 '21 at 18:30
  • hi @JGeer... you mention second form, can i ask which second form?
    – Ali Raza
    Aug 9 '21 at 13:25

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.