I have updated the functions.php file in my child theme with custom code to calculate the weight of items in the shopping cart based on what pallet they can fit on for delivery via truck.
Now I just need to update/override the cart weight with the $kilo_rate which is the value created by my script?
How do I set the shopping cart weight to $kilo_rate ??
<?php
add_filter( 'woocommerce_cart_item_name', 'showing_sku_in_cart_items', 99, 3 );
$item_count = 0;
$volume = 0;
$max_length = 0;
$max_width = 0;
$max_length_cart = 0;
$max_width_cart = 0;
$sample_count = 0;
$sample_count = 0;
$sample_count = "Short";
function showing_sku_in_cart_items( $item_name, $cart_item, $cart_item_key ) {
global $volume;
global $max_length;
global $max_width;
global $max_length_cart;
global $max_width_cart;
global $item_count ;
global $pallet_size;
global $cubic_rate;
global $kilo_rate;
// The WC_Product object
$product = $cart_item['data'];
// Get the SKU
$sku = $product->get_sku();
//IGNORE samples from this list
$sku_pref = substr($sku,0,2);
// When sku doesn't exist
if(empty($sku)) return $item_name;
// Add the sku
$item_name .= '<br><small class="product-sku">' . __( "SKU: ", "woocommerce") . $sku . '</small></br>';
//if ($sku_pref != 'SM') $sku_num = substr($sku,-3);
if ($sku_pref != 'SM'){
$sku_num = substr($sku,-3);
}else{
$sample_count = $sample_count + 1;
}
//get max pallet vol by product SKU
if ($sku_num == '001'){ $max_length = 2.6; $max_width = 0.7; $pallet_size = "Long";}
if ($sku_num == '002'){ $max_length = 2.6; $max_width = 0.8; $pallet_size = "Long";}
if ($sku_num == '003'){ $max_length = 2.6; $max_width = 1.0; $pallet_size = "Long";}
if ($sku_num == '004'){ $max_length = 3.2; $max_width = 0.7; $pallet_size = "Long";}
if ($sku_num == '005'){ $max_length = 3.2; $max_width = 0.9; $pallet_size = "Long";}
if ($sku_num == '006'){ $max_length = 3.2; $max_width = 1.0; $pallet_size = "Long";}
if ($sku_num == '007'){ $max_length = 2.6; $max_width = 0.7; $pallet_size = "Long";}
if ($sku_num == '008'){ $max_length = 3.2; $max_width = 0.7; $pallet_size = "Long";}
if ($sku_num == '009'){ $max_length = 3.2; $max_width = 1.0; $pallet_size = "Long";}
if ($sku_num == '010'){ $max_length = 1.2; $max_width = 0.7; }
if ($sku_num == '011'){ $max_length = 1.2; $max_width = 0.9; }
if ($sku_num == '012'){ $max_length = 1.2; $max_width = 1.0; }
if ($sku_num == '013'){ $max_length = 2.6; $max_width = 1.0; $pallet_size = "Long";}
if ($pallet_size != "Long") $max_width = 1.2;
//SET Max Volume to largest pallet based on largest piece selected
if ($max_length_cart < $max_length) $max_length_cart = $max_length;
if ($max_width_cart < $max_width) $max_width_cart = $max_width;
$pallet_area = ($max_length_cart * $max_width_cart);
global $woocommerce;
$cart_items = $woocommerce->cart->cart_contents_count;
$cart_less_samples = $cart_items - $sample_count;
$cubic_rate = ($pallet_area *(($cart_less_samples * 0.06)+ 0.14));
$kilo_rate = 250 * $cubic_rate;
return $item_name."<strong> Cubic Rate: ".$cubic_rate." - Kilo Rate: ".$kilo_rate."</br>Pallet Size W: ".$max_width_cart."m L: ".$max_length_cart."m</strong>";
}
?>
</div>