Plugin for changing shipping class doesn’t work
-
Hello,
I wrote this piece of code that is supposed to change cart’s shipping class to more expensive one when there are two or more shippable items in the cart.
<?php /* Plugin Name: Change Shipping Class Description: Modifies the shipping class of products in the cart based on certain conditions. Version: 1.0 Author: XXX */ // Hook into the WooCommerce cart add_action('woocommerce_before_calculate_totals', 'set_shipping_class_for_first_product'); function set_shipping_class_for_first_product($cart) { // Check if the cart is empty if ($cart->is_empty()) return; // Initialize a variable to track if there's more than one shippable product $shippable_products_count = 0; // Loop through each cart item foreach ($cart->get_cart() as $cart_item_key => $cart_item) { // Check if the product can be shipped (you may adjust this condition as per your requirement) if ($cart_item['data']->needs_shipping()) { $shippable_products_count += $cart_item['quantity']; } // If this is the first shippable product encountered if ($shippable_products_count >= 2) { // Set the shipping class to 25 for the first shippable product $cart_item['data']->set_shipping_class_id(25); } } } ?>
And I’m testing it thoroughly and it works. But then people order my items and even though they order two items – they pay the lower amount for shipping. Like this script doesn’t work for other people.
I’ve asked a friend and he said that he sees higher price. I’ve tested in 2 phones and one PC and it also works.
What can those people do differently that it doesn’t work for them as supposed to?
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Plugin for changing shipping class doesn’t work’ is closed to new replies.