Question

I created a custom button. What I want is for it to trigger the Add to cart function. Aside from the default add to cart button I want a new add to cart button that will do the same thing. How can I achieve this in Magento 2?

Edit: The reason I want to do this is because my custom button is inside a separate form but what I want is when I click the submit button I want to submit the values of options from other form. I have custom options and have it's value so when I click my submit button I want to submit the value of that other form since the other form containing the custom button only contains the button and no other fields.

Était-ce utile?

La solution

You can place following code to achieve your desired result.

<button class="button" type="button" id="custom_addtocart_button">Add to Cart</button>

<script>
    require(['jquery'], function($) {
        $(document).on('click', '#custom_addtocart_button', function() {
            $('#product-addtocart-button').trigger('click');
        });
    })
</script>
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top