문제

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.

도움이 되었습니까?

해결책

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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top