Andrew Economedes
asked on
Run functions automatically when the shopping cart is updated in WooCommerce?
I have the following functions in my Woocomerce child themes functions.php file.
But every time the cart is updated I need to reload the cart page for the functions to run.
How can I execute the functions automatically when the shopping cart is updated?
But every time the cart is updated I need to reload the cart page for the functions to run.
How can I execute the functions automatically when the shopping cart is updated?
add_action( 'woocommerce_before_calculate_totals', 'add_custom_weight', 10, 1 );
function add_custom_weight( $cart ) {
if (($_SESSION["kilo_rate"]) && ($_SESSION["cart_items_count"]) > 0 ){
$each_cart_item_weight = ($_SESSION["kilo_rate"]/$_SESSION["cart_items_count"]);
foreach ( WC()->cart->get_cart() as $cart_item ) {
//very simplified example - every item in cart will be 100 kg
$cart_item['data']->set_weight( $each_cart_item_weight );
}
}
// print_r( $cart->get_cart_contents_weight() ); // Only for testing (not on checkout page)
}
add_action('woocommerce_cart_contents_weight', 'set_cart_contents_weight');
function set_cart_contents_weight( $weight ) {
$weight = $_SESSION["kilo_rate"];
return $weight;
}
add_action('woocommerce_cart_collaterals', 'myprefix_cart_extra_info');
function myprefix_cart_extra_info() {
echo '<div class="cart-extra-info">';
echo '<h2>Delivery Information</h2>';
echo '<p>Weight: '. $_SESSION["kilo_rate"].'Kg (Pallet plus Products)';
echo '</br> Items in Cart: '.$_SESSION["cart_items_count"];
echo '</br> Pallet Size - Length '.$_SESSION["max_length_cart"].'m Width '.$_SESSION["max_width_cart"].'m</p>';
echo '</div>';
}
/**
* Add a 1% surcharge to your cart / checkout based on delivery country
* Taxes, shipping costs and order subtotal are all included in the surcharge amount
*/
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$county = array('AU');
$percentage = 0.15;
if ( in_array( $woocommerce->customer->get_shipping_country(), $county ) ) :
// $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$surcharge = ($woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Fuel Surcharge', $surcharge, true, '' );
endif;
}
/**
* Add a 1% surcharge to your cart / checkout based on delivery country
* Taxes, shipping costs and order subtotal are all included in the surcharge amount
*/
add_action( 'woocommerce_cart_calculate_fees','woocommerce_warehouse_picking_charge' );
function woocommerce_warehouse_picking_charge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$handling_fee = $_SESSION["cart_less_samples"] * 20;
$woocommerce->cart->add_fee( 'Warehouse Handling Fee', $handling_fee, true, '' );
}
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.