[英]Custom woocommerce Cart AJAX Update
我正在嘗試AJAX化我的Woocommerce購物車,但我很難,我真的找不到任何幫助或文檔(我搜索了幾天)。 所以任何幫助都是非常感激的。
我嘗試了兩種方法:
自定義AJAX函數更改數量時(在購物車頁面上),會觸發一個AJAX調用( - > admin-ajax.php),它會觸發我的functions.php中的一個函數:
function setQty() { global $woocommerce; WC()->cart->set_quantity( $_POST['itemKey'], $_POST['quantity'], true); echo json_encode(array('totalCount'=>WC()->cart->get_cart_contents_count(), 'total'=>$woocommerce->cart->total)); }
回顯的JSON字符串包含購物車中正確數量的商品,但總金額為0.當我重新加載頁面時,不會保存更改的數量。 與...相同
WC()->cart->add_to_cart($product_id, $quantity) or adding WC()->cart->calculate_totals()
我試過這種方式,所以我可以格式化我需要更新我的購物車我想要的值。 為了讓購物車更新保存,我在這里缺少什么?
非常感謝你提前。
它是您試圖創建的迷你購物車/櫃台還是整頁以查看/刪除產品?
如果你想要一個小型車/車櫃台,我創建了一個回購協議https://github.com/samisonline/ajax_cart為您在烤的基本功能。這可能不是你確切的格式”重新尋找,但它顯示了工作中的woocommerce功能,以及AJAX工作所需的標記!
我不確定你想要更新什么,但我想你想更新一攬子產品的總數量。
此示例替換並更新HTML
假設您有一個div,您的總金額在:
<div class="cart-totals"><?php echo WC()->cart->get_cart_contents_count(); ?></div>
將其添加到您的functions.php:
// Mini Cart update with AJAX
add_filter( 'woocommerce_add_to_cart_fragments', 'custom_cart_count_fragments', 10, 1 );
function custom_cart_count_fragments( $fragments ) {
$fragments['div.cart-totals'] = '<div class="cart-totals">' . WC()->cart->get_cart_contents_count() . '</div>';
return $fragments;
}
注意:您正在替換而不是添加
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.