Code Snippet for Splitting the Cart Based on the Dry Ice Products

The following code snippet will help you to split the cart based on the dry ice products and show different shipping rates in the cart/checkout pages.

For this, add the following code in the EasyPost functions.php plugin file and save the changes for implementing the requirement.

new USPS_Easypost_WooCommerce_Shipping();

add_filter( 'woocommerce_cart_shipping_packages', 'wf_split_cart_by_shipping_class_group' );

function wf_split_cart_by_shipping_class_group($packages){

global $wpdb;

//Reset packages

     $packages               = array();

    //Init splitted package

    $splitted_packages      =   array();

    $dry_ice_multiple_check =''; 

    foreach ( WC()->cart->get_cart() as $item_key => $item ) {

     $dry_ice_check = get_post_meta($item['product_id'],'_wf_dry_ice_code',true);

        if ( $item['data']->needs_shipping() ) {        

      $belongs_to_class_group =   'none';

                if($dry_ice_check == 'yes'){             

                 $dry_ice_multiple_check = $dry_ice_check;

                 $belongs_to_class_group = 'dry_ice';              

                }                    

            $splitted_packages[$belongs_to_class_group][$item_key]  =   $item;

        }

    }

    // Add grouped items as packages 

    if($dry_ice_multiple_check){

    if(is_array($splitted_packages)){

        foreach($splitted_packages as $key => $splitted_package_items){

            $packages[] = array(

                'contents'        => $splitted_package_items,

                'contents_cost'   => array_sum( wp_list_pluck( $splitted_package_items, 'line_total' ) ),

                'applied_coupons' => WC()->cart->get_applied_coupons(),

                'dry_ice'         => $key == 'dry_ice' ? $key : '',

                '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()

),

            );

        }

    }

}

    return $packages;

}

}

After implementing the above code snippet you need to make some changes in the plugin and Enable the Dry ice product for splitting the cart and showing the dry ice shipping rates in the cart –

  • You need to select the “Dry Ice” shipping option in the individual product page for the particular dry ice product so that code snippet will recognize between the dry ice and non-dry ice products and then split the cart based on dry ice product.

Please refer to the link for further details on how to Add Dry Ice for Shipping ProductsThe above code snippet will work for particular packaging options in our EasyPost shipping plugin, which are as follows:

  • Pack Items Individually

In this option, each item in the cart is packed separately. Total shipping cost is calculated by adding the shipping cost for each item individually. This option is selected by default.

For example, the shipping cost of item X is $10. If the customer adds two quantities of item X to the cart, the total shipping cost is $10 x 2, which is $20.

  • Pack into boxes with weight and dimensions: 

If packing items individually does not suit your business, you can define custom box sizes under the Box Dimensions section. All the cart items are packed into the defined custom boxes in the Box Dimensions settings. The best-fit box is automatically chosen from the defined boxes. This is the recommended option for cost-effective packing.

Code snippets work for these packaging options mentioned above. Also in the backend, we have provided an option to manually select the dry ice box as per your requirement to generate the dry ice shipping label.

You can read through setting up box dimensions to understand the box packing algorithm in detail.

Note – The above code snippet for Dry ice feature will not work for weight-based packaging option.

After the implementation of all the above changes and defining the code snippet, our plugin will send the dry ice rate request to the EasyPost team and will get the shipping rates in the cart as shown in the below screenshot.

EasyPost Rate Request :

Splitting of the cart:  

The cart will look like this after splitting cart with dry ice product.

Note – FedEx, UPS and USPS all three shipping carriers are eligible for dry ice products.

Note – Above code snippet will only work in the frontend or cart page so it will not generate the Dry Ice shipping labels in the order page. But no worries, if you want to generate Dry ice shipping labels, you can contact the support team and our team will do custom code work and enable the option for generating the dry ice shipping labels. 

Let us take a look at how to do the same.

How to generate Dry Ice shipping labels in the backend

After placing the order with a Dry Ice product and a non-dry ice product, you will find the option for generating the packages in the order page so you can manually add the dry ice box and also enable the dry ice option for generating a normal label as well as dry ice shipping label. Follow the below steps for generating and adding extra dry ice boxes manually.

  1. Generate the packages in the order page

2. Enable Dry Ice option for generating the dry ice label

3. Add a new package for non-dry ice product

4. Create shipment and generate the shipping label for dry ice and non-dry ice product. Please find below the copy of dry ice shipping label

If it is still not working, feel free to contact the support team. We will help you to troubleshoot any issue.

Next WooCommerce – Sort shipping options / methods / services by shipment cost

    Leave a Reply

    WordPress Image Lightbox Plugin