function custom_shipping_class_fee($rates, $package) { // Define the shipping class that triggers the additional fee. $target_shipping_class = 'engineered-flooring'; // Replace with your actual shipping class. $target_shipping_class1 = 'reclaimed-flooring'; // Initialize the additional fee amount. $additional_fee = 590.05; // Change this to the desired additional fee amount. // Check if any products in the package belong to the specified shipping class. $cart_contains_target_shipping_class = false; foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { $product = $cart_item['data']; if ($product->get_shipping_class() === $target_shipping_class || $product->get_shipping_class() === $target_shipping_class1) { $cart_contains_target_shipping_class = true; break; } } // Loop through the shipping rates and add the additional fee if the condition is met. if ($cart_contains_target_shipping_class) { foreach ($rates as $rate_key => $rate) { // Modify the shipping rate you want to add the fee to. if ($rate->method_id === 'flat_rate') { //$rates[$rate_key]->cost += $additional_fee; $rates[$rate_key]->cost = $additional_fee; break; // Stop looping after modifying one rate. } } } return $rates; } add_filter('woocommerce_package_rates', 'custom_shipping_class_fee', 10, 2);
Jagdish Sarma Asked question September 12, 2023