Skip to content

Instantly share code, notes, and snippets.

@Barnabas2
Last active September 13, 2021 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Barnabas2/b514ff0d43c4393f1fbbfb34cfed1b7b to your computer and use it in GitHub Desktop.
Save Barnabas2/b514ff0d43c4393f1fbbfb34cfed1b7b to your computer and use it in GitHub Desktop.
WooCommerce add box weight at checkout
/* The following method allows to add the weight of the packing at the checkout by dividing
the weight of your packing to the cart items. In this way the
the total cart weight includes the box weight and the shipping
options can reflect the correct prices for the total aclual order weight*/
//Case scenario for selling books
//Adjust code per your needs
add_action( 'woocommerce_before_calculate_totals', 'add_custom_weight', 10, 1 );
function add_custom_weight( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
//boxes weight
$Small_envelope = 0.095;
$Medium_envelope = 0.11;
$Large_envelope = 0.2;
$Small_box = 0.4;
$Medium_box = 0.6;
$DHLno5 = 0.9;
$DHLno6 = 1.5;
// Loop through cart items and get total height and max width
// In my case, max width is needed in case there is a book in the cart larger than the normal A5 size
foreach(WC()->cart->get_cart() as $cart_item ) {
$total_height += $cart_item['data']->get_height() * $cart_item['quantity'];
$temp_width = $cart_item['data']->get_width();
if ($temp_width > $max_width){
$max_width = $temp_width;
}
$cart_items_qty += $cart_item['quantity'];
}
//Check which box should be used based on total order height
if( $total_height <= 2.7) {
$extra_weight = $Small_envelope; // Consider also envelope height and wrapping padding
}
if( $total_height > 2.7 && $total_height <=8) {
$extra_weight = $Medium_envelope;
}
if( $total_height > 8 && $total_height <=16 && $max_width < 19) {
$extra_weight = $Large_envelope;
}
// If a box can fit two columns of books, then the box can fit contents double it's normal height
// I have an extra check here in case there was a larger book in width than 19cm,
// to use rather a small box for the order instead of a Large envelope
if( $total_height > 8 && $total_height <=16 && $max_width > 19) {
$extra_weight = $Small_box;
}
if( $total_height > 16 && $total_height <=22) {
$extra_weight = $Small_box;
}
if( $total_height > 22 && $total_height <=38) {
$extra_weight = $Medium_box;
}
if( $total_height > 38 && $total_height <=60) {
$extra_weight = $DHLno5;
}
if( $total_height > 60 ) {
$extra_weight = $DHLno6;
}
//I divide the $extra_weight (box weight), by the number of cart items
//Then loop through cart items again to add the divided weight to each item.
$xtr_weight_per_item = $extra_weight / $cart_items_qty;
foreach ( $cart->get_cart() as $cart_item ) {
$cart_item['data']->set_weight( $cart_item['data']->get_weight()+$xtr_weight_per_item);
}
}
//show calculated weight at checkout for testing
add_action( 'woocommerce_cart_totals_after_order_total', 'display_wc_cart_total_weight_after_order_total' );
function display_wc_cart_total_weight_after_order_total() {
?>
<tr class="order-total">
<th><?php esc_html_e( 'Total weight', 'woocommerce' ); ?></th>
<td data-title="<?php esc_attr_e( 'Total weight', 'woocommerce' ); ?>"><?php echo wc_format_weight( WC()->cart->get_cart_contents_weight() ); ?></td>
</tr>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment