Skip to content
Advertisement

Save WooCommerce order total volume as a custom field for Shipstation

I’ve been coding a WP plugin for Calculating the square inch total volume for an order and send it via custom field to Shipstation:

<?php
   

  global $woocommerce;
  $items = $woocommerce->cart->get_cart();

  //Debugging function
  function console_log( $data ){
    echo '<script>';
    echo 'console.log('. json_encode( $data ) .')';
    echo '</script>';
  }
  console_log('$items');
  //Custoom field for shipstation
  add_filter( 'woocommerce_shipstation_export_custom_field_2', 'shipstation_custom_field_2' );

  //Function for processing volume
  function shipstation_custom_field_2() {
    $cart_prods_in3 = array();
    foreach($items as $item => $values) { 
        $_product =  wc_get_product( $values['data']->get_id());
        //GET GET PRODUCT IN3 
        $prod_in3 = $_product->get_length() * 
                   $_product->get_width() * 
                   $_product->get_height();
        $prod_in3 = $prod_m3 * $values['quantity'];
        //PUSH RESULT TO ARRAY
        array_push($cart_prods_in3, $prod_in3);
    } 
    return $cart_prods_in3; // Key for your custom field
}

console_log('shipstation_custom_field_2');

?>

I uploaded it via SFTP to the sites files and it shows up on WP but when I click activate I get a fatal error:

Fatal error: Uncaught Error: Call to a member function get_cart() on null in /nas/content/live/sfmstaging/wp-content/plugins/boxCalc/boxCalc.php:10 Stack trace: #0 /nas/content/live/sfmstaging/wp-admin/includes/plugin.php(2299): include() #1 /nas/content/live/sfmstaging/wp-admin/plugins.php(191): plugin_sandbox_scrape(‘boxCalc/boxCalc…’) #2 {main} thrown in /nas/content/live/sfmstaging/wp-content/plugins/boxCalc/boxCalc.php on line 10

Am I pulling the cart data right? Is there an issue with the Woocommerce global?

Advertisement

Answer

Note that cart is live customer Object that you can’t get as you are actually doing.

Now in ShipStation plugin documentation the woocommerce_shipstation_export_custom_field_2 filter hook is just to return the meta key of an existing order custom field.

That mean that you need to save the cart total volume as custom order meta data when customer place an order.

Try the following instead:

// Custom 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_total_volume');
function save_order_total_volume( $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 variabl product)
        if( ! $item_volume ) {
            $total_volume += get_product_volume( wc_get_product( $item->get_product_id() ) ) * $item->get_quantity();
        } else {
            $total_volume += $item_volume;
        }
    }
    $order->update_meta_data( '_total_volume', $total_volume ); // Save total volume as custom order meta data
}

// 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 '_total_volume';
}

Code goes in functions.php file of the active child theme (or active theme) or in a plugin file. Tested and works.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement