add_action('woocommerce_cart_calculate_fees', 'custom_shipping_based_on_state_code', 20, 1); function custom_shipping_based_on_state_code($cart) { if (is_admin() && !defined('DOING_AJAX')) return; // التحقق من وجود شحن مجاني $free_shipping = false; foreach (WC()->session->get('chosen_shipping_methods') as $method) { if (strpos($method, 'free_shipping') !== false) { $free_shipping = true; break; } } if ($free_shipping) return; // إجمالي سعر المنتجات $order_total = $cart->subtotal; // عدد الكراتين $boxes = ceil($order_total / 6000); // استخراج رمز المحافظة (state) $state_code = WC()->customer->get_shipping_state(); // قائمة الرموز لمحافظات التكلفة الأعلى $high_cost_state_codes = ['EG-MN', 'EG-BH', 'EG-SH', 'EG-KN', 'EG-LX', 'EG-AS', 'EG-BA', 'EG-SIN', 'EG-JS']; // التحقق من المحافظة $is_high_cost = in_array($state_code, $high_cost_state_codes); // تعيين تكلفة الكرتونة $shipping_per_box = $is_high_cost ? 120 : 100; $shipping_cost = $boxes * $shipping_per_box; // عرض التفاصيل $label = "عدد كراتين الشحن: {$boxes} - التكلفة: {$shipping_cost} جنيه"; $cart->add_fee($label, $shipping_cost, true); }