[英]How to use timber.RightDrawer.open(); in custom add to cart success to open drawer?
我正在使用添加到购物车的自定义ajax代码和它的工作正常,问题是我需要通过使用timber.RightDrawer.open();打开成功功能上的购物车抽屉。 现在我在表单中使用“js-drawer-open-right”类,但是在点击“添加到购物车”按钮的同时打开抽屉。 我需要打开抽屉成功添加到购物车。
我的Ajax代码是:
function addItem(button) {
var postData = $(button).closest('.add-to-cart').serialize();
$.ajax({
type: 'POST',
url: '/cart/add.js',
dataType: 'json',
data: postData,
success: addToCartOk,
error: addToCartFail
});
}
function addToCartOk(product) {
//Want to open drawer here on success
timber.RightDrawer.open();
}
function addToCartFail(obj, status) {
}
我的表格是:
在这里,您可以查看添加到购物车https://soft-theme.myshopify.com/collections/all
与timber.RightDrawer.open()相比,我找到了不同且非常简单的解决方案。
我已经使用jQuery成功函数点击了“js-drawer-open-right”,并从我之前放置的表单中删除了该类。
现在的成功功能是:
function addToCartOk(product) {
//drawer open here by click on that class on success
jQuery('.js-drawer-open-right').click();
}
它工作得非常好。
@barjinder解决方案,但更清洁:
$(document).on("click", ".add-to-cart-button", function(event) {
event.preventDefault();
var form = $(this).closest('form');
jQuery.ajax({
type: 'POST',
url: '/cart/add.js',
data: form.serialize(),
dataType: 'json',
success: function(cart) {
jQuery('.js-drawer-open-right').click();
},
error: function(XMLHttpRequest, textStatus) {
alert("An error occurred: can't reach server");
},
});
});
您可以使用以下表格
<form action="/cart/add" method="POST">
<input type="hidden" name="quantity" value="1">
<input type="hidden" name="id" value="{{ product.variants.first.id }}">
<button type="submit" class="add-to-cart-button">
Add to cart
</button>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.