简体   繁体   English

动态更新woocommerce购物车

[英]Update woocommerce cart dynamically

On my checkout page I added an input box for additional plastic bags for the products (the user can select how many plastic bags they want to add), and when the user changes the quantity there I would like to udpate the cart dynamically. 在结帐页面上,我添加了一个用于产品的其他塑料袋的输入框(用户可以选择要添加的塑料袋的数量),并且当用户在其中更改数量时,我想动态地为购物车加油。

I'm using ajax on the theme script.js: 我在主题script.js上使用ajax:

$(document).on('change', '#additionalBagsSize', function(event) {
    event.preventDefault()
    let data = {
        action : 'my_action',
        id : 1
    }
    $.post(ajaxurl, data, function(response) {
        // no response needed here
    });
})

And in wordpress functions.php: 在wordpress functions.php中:

add_action( 'wp_ajax_my_action', 'my_action_callback' );

function my_action_callback() {
    WC()->cart->total = WC()->cart->total + 1000;
    echo WC()->cart->total;
}

I also tried to add a "fee": 我还尝试添加“费用”:

function my_action_callback() {
    global $woocommerce;    
    $woocommerce->cart->add_fee( 'Surcharge', 500, true, '' );
    echo WC()->cart->total;
}

The adding of 1000 is just for testing purposes, and it doesn't update the cart unfortunately. 添加1000只是出于测试目的,不幸的是,它不会更新购物车。 Is there a way to programatically control the cart amount and update it in real time for the user to see? 是否可以通过编程方式控制购物车数量并实时更新以供用户查看?

Add below line in your ajax response which refresh the cart content 在您的ajax响应中添加以下行,以刷新购物车内容

$(document).on('change', '#additionalBagsSize', function(event) {
    event.preventDefault()
    let data = {
        action : 'my_action',
        id : 1
    }
    $.post(ajaxurl, data, function(response) {
        jQuery(document.body).trigger('update_checkout'); //refresh the cart items and totals
    });
})

It seems that the total woocommerce cart amount is an array of added products (and not a string/integer) so it's impossible to just determine a new amount. 似乎woocommerce购物车的总金额是添加产品的数组(而不是字符串/整数),因此不可能仅确定新金额。

A better approach is to add the custom products you're interested in via the woocommerce plugin, and then use woocommerce functions to add or remove them from the cart. 更好的方法是通过woocommerce插件添加您感兴趣的自定义产品,然后使用woocommerce功能在购物车中添加或删除它们。

To add: 加上:

WC()->cart->add_to_cart($id, $count);

To remove: 去除:

WC()->cart->remove_cart_item($cart_item_key);

Hope it can help someone 希望它可以帮助某人

For adding cart fee, please check below code, in that you have to add some kind of session and then you will have to check for that session by this way you can add the fee 要添加购物车费用,请检查以下代码,因为您必须添加某种会话,然后您必须通过这种方式检查该会话,您可以添加费用

add_action( 'woocommerce_cart_calculate_fees', 'custom_function_checkout_fee', 20, 1 );

function custom_function_checkout_fee( $cart ) {
  if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

  $fee = WC()->session->get( 'add_fee' );
  if(isset($fee) && $fee != 0 && is_numeric($fee)){
    $cart->add_fee( __('Option Fee', 'woocommerce'), $fee );
  }
}



add_action( 'wp_ajax_my_action', 'my_action_callback' );

function my_action_callback() {
    WC()->session->set('add_fee', 500 );
}

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

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