1

I have the following functions in my WooCommerce 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?

<?php
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 Warehouse Handling Fee
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, '' );
}

?>
Andrew
  • 15
  • 2
  • I think that you are not using the correct way from start and you should give more context in your question about `$_SESSION["kilo_rate"]` and `$_SESSION["cart_items_count"]`… – LoicTheAztec May 15 '19 at 01:57
  • Have a look at this question: https://stackoverflow.com/questions/49493085/which-hook-is-running-after-woocommerce-update-cart-button-action – Sjors May 15 '19 at 09:29

1 Answers1

0

thanks for you comments, very much appreciated.

$_SESSION["kilo_rate"] come from another function that calculates the shipping weight rate based on actual volume taken up by the products loaded onto a pallet. It does not relate to the actual weight of products in the shopping cart.

I am using weight based shipping plugin https://wordpress.org/plugins/weight-based-shipping-for-woocommerce/ which appears to calculate shipping rate using all items in cart (not the total cart weight) .

The only way I can work around this is to then change the weight of each item in the cart and that is why I divide $_SESSION["kilo_rate"] by the number or items in the cart $_SESSION["cart_items_count"]

I hope this makes sense.

Andrew
  • 35
  • 4