简体   繁体   English

自定义woocommerce购物车AJAX更新

[英]Custom woocommerce Cart AJAX Update

I am trying to AJAXify my Woocommerce cart but I am failing hard and I really can't find any help or documentation (I searched for days). 我正在尝试AJAX化我的Woocommerce购物车,但我很难,我真的找不到任何帮助或文档(我搜索了几天)。 So any help is desperately appreciated. 所以任何帮助都是非常感激的。

I tried two approaches: 我尝试了两种方法:

  1. Custom AJAX Function When changing the quantity (on the cart page), a AJAX call is fired (-> admin-ajax.php) which triggers a function in my functions.php: 自定义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)); } 

The echoed JSON String contains the correct number of items in cart but the total amount is 0. When I reload the page the altered quantity is not saved. 回显的JSON字符串包含购物车中正确数量的商品,但总金额为0.当我重新加载页面时,不会保存更改的数量。 Same with 与...相同

WC()->cart->add_to_cart($product_id, $quantity) or adding WC()->cart->calculate_totals()

I tried it this way so i can format the values I need to update my cart how i want. 我试过这种方式,所以我可以格式化我需要更新我的购物车我想要的值。 What am I missing here in order to get the cart update saved? 为了让购物车更新保存,我在这里缺少什么?

  1. The woocommerce built-in AJAX update function doesn't trigger, because I have a custom design and I think it needs a specific HTML-structure in order to work properly but i can't find any documentation or examples. woocommerce内置AJAX更新功能不会触发,因为我有一个自定义设计,我认为它需要一个特定的HTML结构才能正常工作,但我找不到任何文档或示例。 Also I need the updated values (total amount, total count etc.) in a custom format. 另外,我需要以自定义格式更新的值(总金额,总计数等)。 So far it just passes me "updated fragments" as predefined html code. 到目前为止,它只是将“更新的片段”作为预定义的html代码传递给我。 How do i need to build the HTML code of my cart.php that the ajax update will work? 我如何构建我的cart.php的HTML代码,ajax更新将起作用? And how do i alter the returning values? 我如何改变返回值?

Thank you very much in advance. 非常感谢你提前。

Is it a mini cart/counter you are trying to create or a full page to review/remove products? 它是您试图创建的迷你购物车/柜台还是整页以查看/删除产品?

If it is a mini-cart/cart counter you wanted, I've created a repo under https://github.com/samisonline/ajax_cart for you that has the basic functionality baked in. This may not be the exact format you're looking for, but it shows the woocommerce functionality at work, as well as the needed markup for AJAX to work! 如果你想要一个小型车/车柜台,我创建了一个回购协议https://github.com/samisonline/ajax_cart为您在烤的基本功能。这可能不是你确切的格式”重新寻找,但它显示了工作中的woocommerce功能,以及AJAX工作所需的标记!

I'm not sure what you'd like to update, but I guess you want to update the totals amount of products in the basket. 我不确定你想要更新什么,但我想你想更新一揽子产品的总数量。

This example replaces and updates the HTML 此示例替换并更新HTML

Let's say you have a div where your total amount is in: 假设您有一个div,您的总金额在:

<div class="cart-totals"><?php echo WC()->cart->get_cart_contents_count(); ?></div>

Add this to your functions.php: 将其添加到您的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;
}

Note: You are replacing not adding 注意:您正在替换而不是添加

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM