В моем файле functions.php дочерних тем WooCommerce есть следующие функции.Но каждый раз, когда корзина обновляется, мне нужно перезагрузить страницу корзины для запуска функций.
Как я могу автоматически выполнять функции при обновлении корзины?
<?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, '' );
}
?>