/*
Theme Name: Hello Elementor Child
Template: hello-elementor
Version: 1.0
*/
add_filter('woocommerce_package_rates', function ($rates, $package) {

    $base_shipping = 150000;
    $extra_per_product = 5000;

    $total_quantity = 0;

    foreach (WC()->cart->get_cart() as $cart_item) {
        $total_quantity += (int) $cart_item['quantity'];
    }

    // محصول اول شامل هزینه پایه است
    // از محصول دوم به بعد، 5000 تومان اضافه می‌شود
    $shipping_cost = $base_shipping;

    if ($total_quantity > 1) {
        $shipping_cost += ($total_quantity - 1) * $extra_per_product;
    }

    foreach ($rates as $rate_key => $rate) {
        $rates[$rate_key]->cost = $shipping_cost;
        $rates[$rate_key]->taxes = array();
    }

    return $rates;

}, 100, 2);