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.

Was it helpful?

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top