• mdczaplicki

    (@mdczaplicki)


    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)
  • mdshak

    (@mdshak)

    I would suggested to use this given below of code. It will work as you desire.

    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 flag to track if the shipping class has been set for the first shippable product
        $shipping_class_set = false;
    
        // 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()) {
                // If the shipping class has not been set yet, set it for this product
                if (!$shipping_class_set) {
                    $cart_item['data']->set_shipping_class_id(25);
                    $shipping_class_set = true; // Set the flag to true to prevent setting the shipping class for subsequent shippable products
                    break; // Exit the loop since we have set the shipping class for the first shippable product
                }
            }
        }
    }
    
    Thread Starter mdczaplicki

    (@mdczaplicki)

    Sorry, but this code doesn’t make sense. What’s the point of

    $shipping_class_set

    ? It’s being set to true and then it leave the loop. So it’s pointless.

    Looks like you copy pasted it from ChatGPT.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Plugin for changing shipping class doesn’t work’ is closed to new replies.