0

i found solution here to count flat rate based on specific quantity steps in woocommerce, what i need additionally to add an extra specific amount based on shipping zone. for example : i charged customer 4 dollar for each 3 items based on specific shipping class, so if the customer added 6 items to cart with the same shipping class he will charged 8 dollar, what i need then to add 50 dollar additional based on the customer shipping zone so the total shipping cost get 58 dollar (8 for items number + 50 for shipping zone), also i want to be abble to add many shipping classes and many shipping zones to the code.

Here's the code

add_filter( 'woocommerce_package_rates', 'progressive_shipping_cost_based_shipping_class_quantity_steps', 10, 2 );
function progressive_shipping_cost_based_shipping_class_quantity_steps( $rates, $package )
{
    // HERE Bellow your settings
    $shipping_class = "very-light-weight"; // The shipping class ID
    $qty_step       = 20; // Items qty threshold for a step
    $item_count     = 0; // Initializing

    // Get the shipping class ID
    $class_id = get_term_by('slug', $shipping_class, 'product_shipping_class' )->term_id;


    // Loop through in cart items to get the Tshirts count
    foreach( $package['contents'] as $cart_item ) {
        if ( $cart_item['data']->get_shipping_class_id() == $class_id ){
            $item_count += $cart_item['quantity']; // Count Tshirts
        }
    }

    // The rate operand increase each {$qty_step} depending on {$item_count}
    $rate_operand = ceil( $item_count / $qty_step );

    foreach ( $rates as $rate_key => $rate ){
        // Targetting "Flat rate"
        if( 'flat_rate' === $rate->method_id ) {
            $has_taxes = false;

            // Set the new cost
            $rates[$rate_key]->cost = $rate->cost * $rate_operand;

            // Taxes rate cost (if enabled)
            foreach ($rates[$rate_key]->taxes as $key => $tax){
                if( $tax > 0 ){
                    // New tax calculated cost
                    $taxes[$key] = $tax * $rate_operand;
                    $has_taxes = true;
                }
            }
            // Set new taxes cost
            if( $has_taxes )
                $rates[$rate_key]->taxes = $taxes;
        }
    }
    return $rates;
}

1 Answer 1

0

To adjust the WooCommerce code so it adds an extra specific amount based on the shipping zone, you can modify the existing function to include shipping zone-based costs. Here’s a streamlined version of the code:

add_filter( 'woocommerce_package_rates', 'add_cost_based_on_zone_and_quantity', 10, 2 );

function add_cost_based_on_zone_and_quantity( $rates, $package ) {
    $shipping_class = "very-light-weight";  // Define the shipping class
    $qty_step = 20;  // Quantity threshold for a step
    $item_count = 0;  // Initialize item count

    // Define extra costs by shipping zone ID
    $zone_costs = array(
        'zone_1' => 50,  // Adjust these with actual zone IDs and costs
        'zone_2' => 75,
    );

    // Get the shipping class ID and count items
    $class_id = get_term_by('slug', $shipping_class, 'product_shipping_class')->term_id;
    foreach ($package['contents'] as $cart_item) {
        if ($cart_item['data']->get_shipping_class_id() == $class_id) {
            $item_count += $cart_item['quantity'];
        }
    }

    // Calculate the rate per quantity step
    $rate_operand = ceil($item_count / $qty_step);

    // Determine the shipping zone and retrieve additional costs
    $shipping_zone = WC_Shipping_Zones::get_zone_matching_package($package);
    $zone_id = $shipping_zone->get_id();  // Get zone ID

    foreach ($rates as $rate_key => $rate) {
        if ('flat_rate' === $rate->method_id) {
            $extra_cost = isset($zone_costs['zone_' . $zone_id]) ? $zone_costs['zone_' . $zone_id] : 0;
            $rates[$rate_key]->cost = ($rate->cost * $rate_operand) + $extra_cost;  // Adjust the total cost
        }
    }

    return $rates;
}

Key Adjustments:

  • Zone Costs Array: Contains additional costs linked to specific shipping zones.
  • Shipping Zone Detection: Identifies the customer’s shipping zone to determine extra charges.
  • Cost Adjustment: Modifies the shipping cost based on both item quantity and shipping zone.

Replace 'zone_1', 'zone_2' with your actual shipping zone identifiers and adjust the additional costs accordingly. This code now succinctly adds specific costs based on both the quantity of items and the customer's shipping zone.

1
  • Thank you very much bro, it works very well, i just need one more thing to apply the same conditional concept to quantity steps to be abble to add as many shipping class and quantiy steps for example : if shipping_class = "very-light-weight" qty_step = 20 ... if shipping_class = "heavy-weight" qty_step = 10 Commented Jun 11 at 14:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.