index.php

WooCommerce Dry Ice Shipping Fee Function

This file contains a function for WooCommerce that calculates additional fees for cart items belonging to a specific shipping class, particularly for items requiring dry ice shipping. It adds a base fee for a defined initial quantity and calculates additional fees for each step quantity beyond that.

<?php add_action( 'woocommerce_cart_calculate_fees', 'cwpai_shipping_class_and_item_quantity_fee', 10, 1 ); function cwpai_shipping_class_and_item_quantity_fee( $cart ) { ## -------------- YOUR SETTINGS BELOW ------------ ## $shipping_class = 'dry-ice-shipping'; // Targeted Shipping class slug $initial_quantity = 3; // Quantity for the initial fee $step_quantity = 3; // Quantity step for additional fees $initial_fee = 30; // Fee for the initial quantity $additional_fee = 30; // Additional fee per step quantity ## ----------------------------------------------- ## $total_quantity = 0; // Initializing // Loop through cart items foreach( $cart->get_cart() as $cart_item ) { // Get the instance of the WC_Product Object $product = $cart_item['data']; // Check for product shipping class if( $product->get_shipping_class() == $shipping_class ) { $total_quantity += $cart_item['quantity']; // Add item quantity } } if ( $total_quantity > 0 ) { $fee_text = __('Dry Ice Shipping Fee', 'codewp'); $fee_amount = $initial_fee; // Start with the initial fee // Calculate additional fees based on the step quantity if ($total_quantity > $initial_quantity) { $additional_steps = ceil(($total_quantity - $initial_quantity) / $step_quantity); $fee_amount += $additional_steps * $additional_fee; } // Add the fee $cart->add_fee( $fee_text, $fee_amount ); } }

Frequently Asked Questions

The code adds a custom fee to the WooCommerce cart based on the quantity of items with a specific shipping class.