[英]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.