add_action('template_redirect','ccs_handle_direct_order',1); function ccs_handle_direct_order(){ if(($_SERVER['REQUEST_METHOD']??'')!=='POST' || empty($_POST['ccs_direct_order'])) return; if(!function_exists('wc_create_order') || !class_exists('WooCommerce')) wp_die('WooCommerce غير مفعل.'); if(!isset($_POST['ccs_order_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['ccs_order_nonce'])),'ccs_direct_order')) wp_die('انتهت صلاحية النموذج. أعد تحميل الصفحة وحاول مجدداً.'); $pid=absint($_POST['ccs_product_id']??0); $product=wc_get_product($pid); if(!$product || !$product->exists() || $product->get_status()!=='publish') wp_die('المنتج غير متاح حالياً.'); $name=sanitize_text_field(wp_unslash($_POST['ccs_customer_name']??'')); $phone=preg_replace('/[^0-9+]/','',(string)wp_unslash($_POST['ccs_customer_phone']??'')); $wilaya=sanitize_text_field(wp_unslash($_POST['ccs_wilaya']??'')); $city=sanitize_text_field(wp_unslash($_POST['ccs_city']??'')); $address=sanitize_text_field(wp_unslash($_POST['ccs_address']??'')); $mode=sanitize_key(wp_unslash($_POST['ccs_delivery_mode']??'home')); if(!in_array($mode,array('home','stopdesk'),true)) $mode='home'; $qty=max(1,min(20,absint($_POST['ccs_qty']??1))); if(!$name || strlen(preg_replace('/\D/','',$phone))<8 || !in_array($wilaya,ccs_algerian_wilayas(),true) || !$city) wp_die('يرجى إدخال الاسم ورقم هاتف صحيح والولاية والبلدية.'); $delivery=ccs_get_delivery_rate($wilaya,$mode); if($delivery===null) wp_die('سعر التوصيل لهذه الولاية غير متوفر حالياً.'); $offers=ccs_get_product_offers($product); $oi=absint($_POST['ccs_offer']??0); if(!isset($offers[$oi])) $oi=0; $offer=$offers[$oi]; try { // Create a real WooCommerce order and persist all order data through WooCommerce CRUD. $order=wc_create_order(array('status'=>'on-hold','customer_id'=>0,'created_via'=>'ccs_direct_order')); if(is_wp_error($order) || !$order instanceof WC_Order) throw new Exception('wc_create_order did not return an order object.'); $item_id=$order->add_product($product,$qty,array( 'subtotal'=>wc_format_decimal($offer['price']*$qty), 'total'=>wc_format_decimal($offer['price']*$qty), )); if(!$item_id) throw new Exception('Could not add product to order.'); $order_item=$order->get_item($item_id); if($order_item) { $order_item->add_meta_data('العرض المختار',$offer['label'],true); $order_item->save(); } $order->set_billing_first_name($name); $order->set_billing_phone($phone); $order->set_billing_country('DZ'); $order->set_billing_state($wilaya); $order->set_billing_city($city); $order->set_billing_address_1($address); $order->set_shipping_first_name($name); $order->set_shipping_country('DZ'); $order->set_shipping_state($wilaya); $order->set_shipping_city($city); $order->set_shipping_address_1($address); $order->update_meta_data('ولاية الزبون',$wilaya); $order->update_meta_data('بلدية الزبون',$city); $order->update_meta_data('طريقة التوصيل',$mode==='home'?'التوصيل إلى المنزل':'التوصيل إلى المكتب'); $order->update_meta_data('سعر الإرجاع',300); if($delivery>0) { $ship=new WC_Order_Item_Shipping(); $ship->set_method_title($mode==='home'?'التوصيل إلى المنزل':'التوصيل إلى المكتب'); $ship->set_method_id('ccs_wilaya_delivery'); $ship->set_total(wc_format_decimal($delivery)); $order->add_item($ship); } $order->set_payment_method('cod'); $order->set_payment_method_title('الدفع عند الاستلام'); $order->set_customer_note('طلب عبر نموذج الطلب المباشر.'); $order->calculate_totals(false); $order->add_order_note('طلب مباشر عبر نموذج المتجر. الولاية: '.$wilaya.'، البلدية: '.$city.'، العرض: '.$offer['label'].'، التوصيل: '.$delivery.' دج.'); $order->save(); if(!$order->get_id() || get_post_status($order->get_id())===false) throw new Exception('Order was not persisted.'); wp_safe_redirect($order->get_checkout_order_received_url()); exit; } catch(Throwable $e) { if(function_exists('wc_get_logger')) { wc_get_logger()->error('CCS direct order failed: '.$e->getMessage(),array('source'=>'ccs-direct-order','product_id'=>$pid)); } wp_die('ما قدرناش نسجلو الطلب بسبب خطأ تقني. حاول مرة أخرى، وإذا تكرر المشكل تواصل مع إدارة المتجر.'); } }
أطلب الآن! وأدفع عند الإستلام 🤝🏻 توصيل سريع لجميع الولايات 🚚 | 0777835679

Cart

Your cart is currently empty!

Browse store


New in store