Skip to content

Instantly share code, notes, and snippets.

@RadGH
Last active June 5, 2020 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RadGH/7c8f0ef77e242ce011932d41e9c06b8e to your computer and use it in GitHub Desktop.
Save RadGH/7c8f0ef77e242ce011932d41e9c06b8e to your computer and use it in GitHub Desktop.
[WooCommerce UPS Shipping] This script allows you to specify products as "already packed". They will not be added to separate boxes, and will not be combined with other products.
<?php
// This script allows you to specify products as "already packed". They will not be added to separate boxes, and will not be combined with other products.
// This is useful if you have products that are already packed and ready to ship that should not be put into another box.
//
// IMPORTANT:
// The plugin 'WooCommerce UPS Shipping' does not currently support the filter "woocommerce_shipping_ups_skip_packing".
// For now, you must manually edit the UPS plugin directly. The two edits are located below (for version 3.2.18):
// 1. https://gist.github.com/RadGH/ad8d8e599366a27c81bc84dd1f66aaa9
// 2. https://gist.github.com/RadGH/b7b69f65bbeb7212083058d1d644b0c9
//
// The rest of this script goes in your functions.php or similar location.
/**
* When the WooCommerce UPS Shipping Method is packing cart items into boxes, examine each eligible product and see if "Packed Separately" is enabled.
* When "Packed Separately" is enabled, the product's dimensions are used as a custom package size and the product is not combined in another package.
* If multiple quantity is specified, more than one package will be added.
*
* @param bool $skip
* @param string $cart_item_key
* @param array $cart_item_data
* @param string $dim_unit
* @param string $weight_unit
* @param WC_Boxpack $WC_Boxpack
* @param WC_Shipping_UPS $WC_Shipping_UPS
*
* @return mixed
*/
function tt_ups_maybe_use_separate_package_for_product( $skip, $cart_item_key, $cart_item_data, $dim_unit, $weight_unit, $WC_Boxpack, $WC_Shipping_UPS ) {
if ( $skip ) return $skip;
// Get some cart item data
$product_id = $cart_item_data['product_id'];
$variation_id = $cart_item_data['variation_id'];
$quantity = max( 1, $cart_item_data['quantity'] );
// Abort unless "_packed_separately" is enabled for the product (for variations, checks the parent product)
if ( ! get_post_meta( $product_id, '_packed_separately', true ) ) return $skip;
// Get dimensions and value for packing
$length = number_format( wc_get_dimension( $cart_item_data['data']->get_length(), strtolower( $dim_unit ) ), 2, '.', '' );
$width = number_format( wc_get_dimension( $cart_item_data['data']->get_width(), strtolower( $dim_unit ) ), 2, '.', '' );
$height = number_format( wc_get_dimension( $cart_item_data['data']->get_height(), strtolower( $dim_unit ) ), 2, '.', '' );
$weight = number_format( wc_get_weight( $cart_item_data['data']->get_weight(), strtolower( $weight_unit ) ), 2, '.', '' );
$value = $cart_item_data['data']->get_price();
// Add a box (or multiple boxes if qty more than 1)
for ( $i = 1; $i <= $quantity; $i++ ) {
$WC_Boxpack->add_package( $length, $width, $height, $weight, $value );
// Report some debugging info
$WC_Shipping_UPS->debug( '['. $i .'] Self-packed item added:<br><pre>' . print_r(array('product_id' => $product_id, 'variation_id' => $variation_id, 'length' => $length, 'width' => $width, 'height' => $height, 'weight' => $weight, 'value' => $value), true) );
}
return true;
}
add_filter( 'woocommerce_shipping_ups_skip_packing', 'tt_ups_maybe_use_separate_package_for_product', 10, 7 );
/**
* Add a checkbox below Dimensions on shipping tab of products to allow enabling the "Packed Separately" option.
*/
function tt_packed_separately_field() {
global $post;
// Display checkbox below dimensions field
woocommerce_wp_checkbox(
array(
'id' => '_packed_separately',
'wrapper_class' => 'show_if_simple show_if_variable hide_if_external hide_if_grouped hide_if_virtual',
'label' => __( 'Packed Separately', 'terratech' ),
'description' => __( 'Product is already boxed and should not be packed with other products (UPS Only).', 'terratech' )
)
);
}
add_action( 'woocommerce_product_options_dimensions', 'tt_packed_separately_field' );
/**
* Save "_packed_separately" metadata to the product
*
* @param $post_id
*/
function tt_save_packed_separately_field( $post_id ) {
$checked = isset( $_POST['_packed_separately'] ) ? 'yes': '';
update_post_meta( $post_id, '_packed_separately', $checked );
}
add_action( 'woocommerce_process_product_meta', 'tt_save_packed_separately_field' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment