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?