Got a custom plugin here that takes box sixes and decides the best ones for an order using the volumes of items:
//Initialize box sizes and volumes
$boxes = array("10x6x4"=>240, "10x8x6"=>480, "13x9x6"=>702, "12x12x6"=>864, "15x12x8"=>1440, "22x12x8"=>2112, "24x12x12"=>3456);
// Function to get the volume of a product
function get_product_volume( $product ) {
return $product->get_length() * $product->get_width() * $product->get_height();
}
// Save the cart total volume as custom order meta data
add_action('woocommerce_checkout_create_order', 'save_order_box_size');
function save_order_box_size( $order ) {
$total_volume = 0; // Initializing
// Loop through order items
foreach( $order->get_items() as $item ){
$item_volume = get_product_volume($item->get_product()) * $item->get_quantity();
// For product variations (if volume is not accessible get the volume of the parent variable product)
if( ! $item_volume ) {
$total_volume += get_product_volume( wc_get_product( $item->get_product_id() ) ) * $item->get_quantity();
} else {
$total_volume += $item_volume;
}
}
//Loop through Box sizes and volumes
foreach($boxes as $box_size => $box_volume) {
if($total_volume <= $box_volume){
// Save total volume as custom order meta data
$order->update_meta_data( '_box_size', $box_size );
break;
}
}
}
// Add total volume custom field meta key for export to ship station
add_filter( 'woocommerce_shipstation_export_custom_field_2', 'shipstation_custom_field_2' );
function shipstation_custom_field_2( $custom_field_key ) {
return '_box_size';
}
No errors are thrown in the logs but there is no value in the custom field for Shipstation when the order gets there. I ran the code with set values for $total_volume and it came out right every time. Am I missing something with pulling the item dimensions?
The first code line (the array of boxes) should required to be included in your 2nd function like (as variable $boxes
was not defined):
// Function to get the volume of a product
function get_product_volume( $product ) {
return $product->get_length() * $product->get_width() * $product->get_height();
}
// Save the cart total volume as custom order meta data
add_action('woocommerce_checkout_create_order', 'save_order_box_size');
function save_order_box_size( $order ) {
// Define box sizes and volumes
$boxes = array("10x6x4"=>240, "10x8x6"=>480, "13x9x6"=>702, "12x12x6"=>864, "15x12x8"=>1440, "22x12x8"=>2112, "24x12x12"=>3456);
$total_volume = 0; // Initializing
// Loop through order items
foreach( $order->get_items() as $item ){
$item_volume = get_product_volume($item->get_product()) * $item->get_quantity();
// For product variations (if volume is not accessible get the volume of the parent variable product)
if( ! $item_volume ) {
$total_volume += get_product_volume( wc_get_product( $item->get_product_id() ) ) * $item->get_quantity();
} else {
$total_volume += $item_volume;
}
}
//Loop through Box sizes and volumes
foreach($boxes as $box_size => $box_volume) {
if($total_volume <= $box_volume){
// Save total volume as custom order meta data
$order->update_meta_data( '_box_size', $box_size );
break;
}
}
}
// Add total volume custom field meta key for export to ship station
add_filter( 'woocommerce_shipstation_export_custom_field_2', 'shipstation_custom_field_2' );
function shipstation_custom_field_2( $custom_field_key ) {
return '_box_size';
}
It should better work now.