Skip to content

Instantly share code, notes, and snippets.

@mcurren
Last active November 29, 2017 22:18
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 mcurren/78fef5a6115dfc1177fad160737549e5 to your computer and use it in GitHub Desktop.
Save mcurren/78fef5a6115dfc1177fad160737549e5 to your computer and use it in GitHub Desktop.
Add 3D Printing weights and dims to WooCommerce cart objects
/**
* Add 3DPrint weight and dimensions to WooCommerce cart item objects
* @author mike@curren.me
*
* @requires WooCommerce plugin - https://woocommerce.com/
* @requires Wordpress 3D Printing plugin - http://www.wp3dprinting.com/
*/
function add_3dp_stats_to_wc_cart_objects( $cart_object ) {
if ( (is_admin() && ! defined( 'DOING_AJAX' ) ) || $cart_object->is_empty() )
return;
foreach ( $cart_object->get_cart() as $cart_item ) {
/**
* Set cart item weight
* NOTE: must multiply integer by 1000 to move decimal over 3 places
* maybe raw dump is incorrect because of float() wrap from plugin ???
*/
$cart_item['data']->set_weight( $cart_item['3dp_options']['weight'] * 1000 );
/**
* Set cart item dimensions
*/
$cart_item['data']->set_width( $cart_item['3dp_options']['width'] );
$cart_item['data']->set_height( $cart_item['3dp_options']['height'] );
$cart_item['data']->set_length( $cart_item['3dp_options']['length'] );
}
/**
* Debugging info
* NOTE: un-comment below to show total cart weight on WooCommerce Cart page
*/
// echo '<pre>Cart weight: '; print_r( $cart_object->get_cart_contents_weight() ); echo '</pre><br>';
}
add_action( 'woocommerce_before_calculate_totals', 'add_3dp_stats_to_wc_cart_objects', 10, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment