Skip to content

Instantly share code, notes, and snippets.

@rajeshsingh520
Last active September 10, 2025 04:51
Show Gist options
  • Save rajeshsingh520/9b77f96f9c5326f9641675cb459270de to your computer and use it in GitHub Desktop.
Save rajeshsingh520/9b77f96f9c5326f9641675cb459270de to your computer and use it in GitHub Desktop.
split printify package
if ( ! class_exists( 'Pisol_Split_Shipping_20250910' ) ) {
class Pisol_Split_Shipping_20250910 {
private static $instance = null;
private $shipping_class_id = 280; // Replace with your shipping class ID
private function __construct() {
add_filter( 'woocommerce_cart_shipping_packages', [ $this, 'split_packages' ], PHP_INT_MAX );
}
public static function get_instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
public function split_packages( $packages ) {
$subtotal_of_free_shipping_class_product = $this->get_free_shipping_class_subtotal();
$country = WC()->customer->get_shipping_country();
if($country == 'US' && $subtotal_of_free_shipping_class_product < 99){
return $packages;
}
if($country == 'CA' && $subtotal_of_free_shipping_class_product < 135){
return $packages;
}
$new_package = array(
'contents' => array(),
'contents_cost' => 0,
'applied_coupons' => WC()->cart->get_applied_coupons(),
'user' => array(
'ID' => get_current_user_id(),
),
'destination' => array(
'country' => WC()->customer->get_shipping_country(),
'state' => WC()->customer->get_shipping_state(),
'postcode' => WC()->customer->get_shipping_postcode(),
'city' => WC()->customer->get_shipping_city(),
'address' => WC()->customer->get_shipping_address(),
'address_2' => WC()->customer->get_shipping_address_2(),
),
);
foreach ( $packages as $p_index => &$package ) {
foreach ( $package['contents'] as $item_key => $item ) {
$product = $item['data'];
if ( $product->get_shipping_class_id() == $this->shipping_class_id ) {
// Move this item to the new package
$new_package['contents'][ $item_key ] = $item;
$new_package['contents_cost'] += $item['line_total'];
// Remove from current package
unset( $package['contents'][ $item_key ] );
$package['contents_cost'] -= $item['line_total'];
}
}
}
// Remove empty packages
$packages = array_filter( $packages, function( $pkg ) {
return ! empty( $pkg['contents'] );
});
// Reindex
$packages = array_values( $packages );
// Add new package if needed
if ( ! empty( $new_package['contents'] ) ) {
$packages[] = $new_package;
}
return $packages;
}
/**
* Get subtotal of products with the defined shipping class in the cart
*/
public function get_free_shipping_class_subtotal() {
$subtotal = 0;
if ( ! WC()->cart ) {
return $subtotal;
}
foreach ( WC()->cart->get_cart() as $cart_item ) {
$product = $cart_item['data'];
if ( $product && $product->get_shipping_class_id() == $this->shipping_class_id ) {
$subtotal += $cart_item['line_subtotal']; // before discounts
// Or use $cart_item['line_total'] if you want after discounts
}
}
return $subtotal;
}
}
Pisol_Split_Shipping_20250910::get_instance();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment