My question is basically nearly similar to this question, but my case is on cart page (not checkout page): Update fee dynamically based on radio buttons in Woocommerce checkout
Everytime user update cart, I will calculate the surcharge based on the number of cart items (the calculation is done via an external API)
After I calculate the surcharge successfully via API, I save the surcharge in woocommerce session but I don't know how to update the surcharge in the total cart table
I can add the surcharge to WooCommerce cart with woocommerce_cart_calculate_fee
hook but for some reason, it always pick up the previous value of WC()->session->get( 'custom_charge_amount', 0);
instead of the current value.
The flow:
- User add items => go to cart => calculate the surcharge based on number of items => update woo surcharge
- User update cart (add item, ..) => calculate the surcharge based on number of items => update woo surcharge
Javascript / jQuery code:
jQuery(document).ready(function( $ ) {
const customCharge = {
init: function() {
$( document.body ).on( 'updated_cart_totals', this.get_custom_charge_amount)
this.get_custom_charge_amount()
},
get_custom_charge_amount: function() {
$.ajax({
url: admin_obj.ajax_url,
type : 'get',
data : {
action : 'get_custom_charge_amount'
},
success: function( htmlResponse ) {
$('#custom-fee').replaceWith( htmlResponse )
$('body').trigger('wc_fragment_refresh') // Not work
}
})
}
}
customCharge.init()
})
PHP code:
add_action( 'woocommerce_cart_collaterals','display_custom_cart' );
function display_custom_cart() {
require_once plugin_dir_path( dirname( __FILE__ ) ) .
'templates/custom-fee.php';
}
add_action( 'woocommerce_cart_calculate_fees', 'custom_surcharge', 20, 1 );
function custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$custom_amount = WC()->session->get( 'custom_charge_amount', 0 ); // get from session
$woocommerce->cart->add_fee( __('Custom charge'), $custom_amount, true, '' );
}
// WP AJAX HANDLE
add_action('wp_ajax_get_compensate_amount', 'get_custom_charge_amount');
add_action('wp_ajax_nopriv_get_custom_amount','get_custom_charge_amount');
public function get_custom_charge_amount() {
// calculate amount here
$custom_amount = 10 * WC()->cart->get_cart_contents_count(); // This is based on the cart item, should calculate on external API
WC()->session->set( 'custom_charge_amount', $custom_amount ); //set session
global $woocommerce;
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'templates/custom-fee.php';
wp_die();
}
PHP / HTML code (in "custom-fee" template):
<?php $custom_amount = WC()->session->get( 'custom_charge_amount', 0); ?>
<div id="custom-fee" class="custom-cart">
<p>This is custom cart</p>
<input id="custom-checkbox" type="checkbox">
<span class="custom-cart__amount"><?php echo $custom_amount; ?></span>
</div>
Any help will be highly appreciated.
I add the surcharge via woocommerce_cart_calculate_fees
add_action( 'woocommerce_cart_calculate_fees', 'custom_surcharge', 20, 1 );
function custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$custom_amount = WC()->session->get( 'custom_charge_amount', 0 ); // get from session
$woocommerce->cart->add_fee( __('Custom charge'), $custom_amount, true, '' );
}