0

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.

0

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>
1
  • This seems a lot cleaner than my solution. Will use this. Thanks Jun 12 '19 at 7:41

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.