Last active
November 29, 2017 22:18
-
-
Save mcurren/78fef5a6115dfc1177fad160737549e5 to your computer and use it in GitHub Desktop.
Add 3D Printing weights and dims to WooCommerce cart objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Add 3DPrint weight and dimensions to WooCommerce cart item objects | |
* @author [email protected] | |
* | |
* @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