if (is_array($rates) && !empty($rates)) { $rate = reset($rates); if (isset($rate->cost)) { $cost = floatval($rate->cost); } } } catch (Exception $e) {} } $shipping_methods[] = [ 'id' => $zone_id . ':' . $method->id, 'title' => $method->get_method_title(), 'cost' => wc_price($cost) ]; } } } // إذا لم توجد طرق شحن للمنطقة، جرب المنطقة الافتراضية (Rest of the World) if (empty($shipping_methods) && $zone_id != 0) { $default_zone = WC_Shipping_Zones::get_zone_by('zone_id', 0); if ($default_zone) { // تجهيز مصفوفة الشحن مع القيم الافتراضية $package = array( 'destination' => array( 'country' => 'DZ', 'state' => $zone_id, 'postcode' => '00000', 'city' => '', 'address' => '', 'address_2' => '', ), 'contents_cost' => $product_price, ); foreach ($default_zone->get_shipping_methods(true) as $method) { if ($method->enabled === 'yes') { $cost = isset($method->cost) ? floatval($method->cost) : 0; if ($method->id === 'free_shipping') { $cost = 0; } if (method_exists($method, 'calculate_shipping')) { $rate = null; try { $rates = $method->calculate_shipping($package); if (is_array($rates) && !empty($rates)) { $rate = reset($rates); if (isset($rate->cost)) { $cost = floatval($rate->cost); } } } catch (Exception $e) {} } $shipping_methods[] = [ 'id' => '0:' . $method->id, 'title' => $method->get_method_title(), 'cost' => wc_price($cost) ]; } } } } } // اختر أول طريقة شحن كسعر افتراضي if (!empty($shipping_methods)) { $shipping_cost = floatval(strip_tags($shipping_methods[0]['cost'])); } $total = $product_price + $shipping_cost; wp_send_json_success([ 'shipping_cost' => wc_price($shipping_cost), 'total' => wc_price($total), 'shipping_methods' => $shipping_methods // استبدال زر السلة بفورم الطلب السريع add_action('woocommerce_single_product_summary', function() { // لا تنفذ الكود في لوحة التحكم أو خارج الواجهة الأمامية أو خارج صفحة المنتج أو إذا لم تكن WooCommerce معرفة if (is_admin() || !function_exists('is_product') || !is_product() || !class_exists('WooCommerce')) return; // كل كود النموذج والمتغيرات هنا فقط if (get_option('hozicod_enabled', 'yes') !== 'yes') return; global $product; $selected = get_option('hozicod_selected_products', []); if (!empty($selected) && !in_array($product->get_id(), (array)$selected)) return; // إخفاء زر السلة الافتراضي remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30); // جلب ولايات الجزائر من WooCommerce States $dz_states = []; if (class_exists('WC_Countries')) { $dz_states = WC()->countries->get_states('DZ'); } $product_price = $product->get_price(); $shipping_cost = 0; $total = $product_price + $shipping_cost; // تعريف متغيرات المنتج والأسعار بقيم افتراضية $product = $product ?? false; $product_price = $product_price ?? 0; $shipping_cost = $shipping_cost ?? 0; $total = $total ?? 0; // إعداد متغيرات التنسيق من الإعدادات $field_bg = get_option('hozicod_field_bg', '#fff'); $field_color = get_option('hozicod_field_color', '#222'); $field_fontsize = get_option('hozicod_field_fontsize', '16'); $btn_fontsize = get_option('hozicod_btn_fontsize', '18'); $btn_color = get_option('hozicod_button_color', '#007cba'); $btn_text_color = get_option('hozicod_text_color', '#fff'); $form_bg = get_option('hozicod_bg_color', '#f9f9f9'); $summary_bg = get_option('hozicod_summary_bg', '#f1f3f6'); $summary_color = get_option('hozicod_summary_color', '#222'); $summary_border = get_option('hozicod_summary_border', '#e0e5ee'); $btn_animation = get_option('hozicod_btn_animation', 'none'); $form_title = get_option('hozicod_form_title', 'أضف معلوماتك في الأسفل للطلب'); $btn_text = get_option('hozicod_btn_text', 'اطلب الآن'); $font_family = get_option('hozicod_font_family', 'Cairo'); $font_size = get_option('hozicod_font_size', '16'); // حماية شاملة: تعريف جميع المتغيرات بقيم افتراضية إذا لم تكن معرفة أو فارغة (مرة واحدة فقط) $field_bg = isset($field_bg) && $field_bg ? $field_bg : '#fff'; $field_color = isset($field_color) && $field_color ? $field_color : '#222'; $field_fontsize = isset($field_fontsize) && $field_fontsize ? $field_fontsize : '15'; $btn_fontsize = isset($btn_fontsize) && $btn_fontsize ? $btn_fontsize : '17'; $btn_color = isset($btn_color) && $btn_color ? $btn_color : '#1e88e5'; $btn_text_color = isset($btn_text_color) && $btn_text_color ? $btn_text_color : '#fff'; $form_bg = isset($form_bg) && $form_bg ? $form_bg : '#f9f9f9'; $summary_bg = isset($summary_bg) && $summary_bg ? $summary_bg : '#f1f3f6'; $summary_color = isset($summary_color) && $summary_color ? $summary_color : '#222'; $summary_border = isset($summary_border) && $summary_border ? $summary_border : '#e0e5ee'; $form_title = isset($form_title) && $form_title ? $form_title : 'أضف معلوماتك في الأسفل للطلب'; $font_family = isset($font_family) && $font_family ? $font_family : 'Cairo'; $font_size = isset($font_size) && $font_size ? $font_size : '16'; $btn_animation = isset($btn_animation) && $btn_animation ? $btn_animation : 'none'; $btn_text = isset($btn_text) && $btn_text ? $btn_text : 'اطلب الآن'; $dz_states = isset($dz_states) && is_array($dz_states) ? $dz_states : array(); $product = isset($product) && $product ? $product : false; $product_price = isset($product_price) && $product_price ? $product_price : 0; $shipping_cost = isset($shipping_cost) && $shipping_cost ? $shipping_cost : 0; $total = isset($total) && $total ? $total : 0; ?>// تأكد من وجود المنتج if (!isset($product) || !$product) { $form_title = $form_title ?? 'أضف معلوماتك في الأسفل للطلب'; $font_family = $font_family ?? 'Cairo'; $font_size = $font_size ?? '16'; $btn_animation = $btn_animation ?? 'none'; $btn_text = $btn_text ?? 'اطلب الآن'; $dz_states = $dz_states ?? array(); $product = $product ?? false; $product_price = $product_price ?? 0; $shipping_cost = $shipping_cost ?? 0; $total = $total ?? 0; ?>
📝
💰 سعر المنتج: دج
🚚 سعر التوصيل: دج
🧾 الإجمالي الكلي: دج
}, 29); // تحميل خطوط Google Fonts المطلوبة add_action('wp_head', function() { $font_family = get_option('hozicod_font_family', 'Cairo'); $fonts = [ 'Cairo' => 'https://fonts.googleapis.com/css?family=Cairo:400,700&display=swap', 'Tajawal' => 'https://fonts.googleapis.com/css?family=Tajawal:400,700&display=swap', 'Amiri' => 'https://fonts.googleapis.com/css?family=Amiri:400,700&display=swap' ]; if(isset($fonts[$font_family])) { echo ''; } }); // معالجة الطلب وإنشاء طلب WooCommerce حقيقي add_action('wp', function() { if (isset($_POST['hozicod_product_id']) && isset($_POST['hozicod_name'])) { $product_id = intval($_POST['hozicod_product_id']); $qty = max(1, intval($_POST['hozicod_qty'] ?? 1)); $name = sanitize_text_field($_POST['hozicod_name']); $phone = sanitize_text_field($_POST['hozicod_phone']); $address = sanitize_text_field($_POST['hozicod_address']); $state = sanitize_text_field($_POST['hozicod_state']); $shipping_method = sanitize_text_field($_POST['hozicod_shipping_method'] ?? ''); $order = wc_create_order(); $order->add_product(wc_get_product($product_id), $qty); $order->set_address([ 'first_name' => $name, 'phone' => $phone, 'address_1' => $address, 'state' => $state, 'country' => get_option('woocommerce_default_country'), ], 'shipping'); $order->set_address([ 'first_name' => $name, 'phone' => $phone, 'address_1' => $address, 'state' => $state, 'country' => get_option('woocommerce_default_country'), ], 'billing'); if ($shipping_method) { $parts = explode(':', $shipping_method); $zone_id = $parts[0] ?? ''; $method_id = $parts[1] ?? ''; $order->add_shipping( new WC_Shipping_Rate($shipping_method, '', 0, [], $method_id) ); } $order->calculate_totals(); $order->update_status('processing'); // إعادة التوجيه لصفحة الشكر $thankyou = wc_get_endpoint_url('order-received', $order->get_id(), wc_get_checkout_url()); wp_redirect($thankyou); exit; } }); // --- نهاية ملف HoziCOD ---