> /** * Plugin Name: WooCommerce Advanced Excel/CSV Product Importer & Exporter * Plugin URI: https://yourstore.com * Description: إضافة مخصصة لاستيراد وتصدير وتعديل المنتجات عبر ملف CSV مع دعم فلترة التصنيفات وحالة النشر، WooCommerce، Google Shopping، و Yoast SEO. * Version: 2.1.0 * Author: مبرمج المتجر * Text Domain: wc-advanced-importer */ if ( ! defined( 'ABSPATH' ) ) { exit; } // 1. إضافة صفحة الاستيراد والتصدير إلى قائمة المنتجات في لوحة التحكم function wc_adv_importer_menu() { add_submenu_page( 'edit.php?post_type=product', 'استيراد وتصدير المنتجات المتقدم', 'استيراد وتصدير المنتجات', 'manage_woocommerce', 'wc-adv-product-importer', 'wc_adv_importer_page_html' ); } add_action( 'admin_menu', 'wc_adv_importer_menu' ); // دالة مشتركة لتحديد رؤوس الأعمدة المطلوبة للاستيراد والتصدير function wc_adv_get_csv_headers() { return array( 'post_title', 'post_content', 'regular_price', 'sale_price', 'sku', 'post_name', 'short_description', 'categories', 'brand', 'mpn', 'google_brand', 'product_condition', 'meta_desc', 'social_title', 'social_desc', 'x_title', 'x_desc', 'post_status' ); } // 2. معالجة تحميل نموذج الـ CSV الفارغ أو تصدير المنتجات الحالية function wc_adv_handle_template_download() { if ( isset( $_GET['action'] ) && current_user_can( 'manage_woocommerce' ) ) { // أ. تحميل النموذج الفارغ if ( $_GET['action'] == 'download_product_template' ) { header( 'Content-Type: text/csv; charset=UTF-8' ); header( 'Content-Disposition: attachment; filename="product_import_template.csv"' ); $output = fopen( 'php://output', 'w' ); fprintf( $output, chr(0xEF).chr(0xBB).chr(0xBF) ); // BOM للغة العربية $headers = wc_adv_get_csv_headers(); fputcsv( $output, $headers ); // صف تجريبي توضيحي $sample_row = array( 'جهاز عناية شخصية متطور', 'وصف تفصيلي للمنتج هنا...', '25.500', '19.990', 'SKU-1025', 'personal-care-device', 'وصف قصير للمنتج', 'أجهزة التجميل والعناية الشخصية', 'إبليمو', 'MPN-9988', 'إبليمو', 'new', 'بيانات الميتا وصفية تظهر في محركات البحث', 'العنوان الاجتماعي للمنتج', 'الوصف الاجتماعي للمنتج', 'عنوان منصة X', 'وصف منصة X', 'publish' ); fputcsv( $output, $sample_row ); fclose( $output ); exit; } // ب. تصدير المنتجات الحالية مع الفلترة if ( $_GET['action'] == 'export_products_csv' ) { header( 'Content-Type: text/csv; charset=UTF-8' ); header( 'Content-Disposition: attachment; filename="products_export_' . date('Y-m-d') . '.csv"' ); $output = fopen( 'php://output', 'w' ); fprintf( $output, chr(0xEF).chr(0xBB).chr(0xBF) ); // BOM للغة العربية fputcsv( $output, wc_adv_get_csv_headers() ); // جلب الفلاتر إن وجدت من رابط الـ GET $filter_cat = isset( $_GET['export_cat'] ) ? sanitize_text_field( $_GET['export_cat'] ) : ''; $filter_status = isset( $_GET['export_status'] ) ? sanitize_text_field( $_GET['export_status'] ) : ''; $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'post_status' => ! empty( $filter_status ) ? array( $filter_status ) : array( 'publish', 'draft', 'pending', 'private' ), ); if ( ! empty( $filter_cat ) ) { $args['tax_query'] = array( array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $filter_cat, ), ); } $products = get_posts( $args ); foreach ( $products as $p ) { $product_obj = wc_get_product( $p->ID ); if ( ! $product_obj ) continue; // استخراج التصنيفات $cat_terms = get_the_terms( $p->ID, 'product_cat' ); $cat_names = array(); if ( $cat_terms && ! is_wp_error( $cat_terms ) ) { foreach ( $cat_terms as $ct ) { $cat_names[] = $ct->name; } } $categories_str = implode( ', ', $cat_names ); // استخراج الماركة (Brand) $brand_val = ''; if ( taxonomy_exists( 'product_brand' ) ) { $brand_terms = get_the_terms( $p->ID, 'product_brand' ); if ( $brand_terms && ! is_wp_error( $brand_terms ) ) { $brand_names = array(); foreach ( $brand_terms as $bt ) { $brand_names[] = $bt->name; } $brand_val = implode( ', ', $brand_names ); } } if ( empty( $brand_val ) ) { $brand_val = get_post_meta( $p->ID, '_product_brand', true ); } $row = array( $p->post_title, $p->post_content, $product_obj->get_regular_price(), $product_obj->get_sale_price(), $product_obj->get_sku(), $p->post_name, $p->post_excerpt, $categories_str, $brand_val, get_post_meta( $p->ID, '_gpf_mpn', true ) ?: get_post_meta( $p->ID, 'gpls_woo_gpf_data_mpn', true ), get_post_meta( $p->ID, '_wc_gla_brand', true ), get_post_meta( $p->ID, '_wc_gla_condition', true ) ?: 'new', get_post_meta( $p->ID, '_yoast_wpseo_metadesc', true ), get_post_meta( $p->ID, '_yoast_wpseo_opengraph-title', true ), get_post_meta( $p->ID, '_yoast_wpseo_opengraph-description', true ), get_post_meta( $p->ID, '_yoast_wpseo_twitter-title', true ), get_post_meta( $p->ID, '_yoast_wpseo_twitter-description', true ), $p->post_status ); fputcsv( $output, $row ); } fclose( $output ); exit; } } } add_action( 'admin_init', 'wc_adv_handle_template_download' ); // 3. تصميم صفحة الاستيراد، التصدير، والرفع function wc_adv_importer_page_html() { if ( ! current_user_can( 'manage_woocommerce' ) ) { return; } $message = ''; if ( isset( $_POST['wc_adv_import_nonce'] ) && wp_verify_nonce( $_POST['wc_adv_import_nonce'], 'wc_adv_import_action' ) ) { if ( isset( $_FILES['excel_file'] ) && ! empty( $_FILES['excel_file']['tmp_name'] ) ) { $file = $_FILES['excel_file']['tmp_name']; $file_name = $_FILES['excel_file']['name']; $ext = pathinfo( $file_name, PATHINFO_EXTENSION ); if ( in_array( strtolower( $ext ), array( 'csv', 'txt' ) ) ) { $handle = fopen( $file, 'r' ); if ( $handle !== FALSE ) { // تخطي البايت BOM إن وجد $bom = fread( $handle, 3 ); if ( $bom !== "\xEF\xBB\xBF" ) { rewind( $handle ); } $header = fgetcsv( $handle, 2000, ',' ); $imported_count = 0; while ( ( $row = fgetcsv( $handle, 2000, ',' ) ) !== FALSE ) { if ( count( $header ) == count( $row ) ) { $data = array_combine( $header, $row ); if ( $data ) { wc_adv_process_product_row( $data ); $imported_count++; } } } fclose( $handle ); $message = '
تم استيراد ومعالجة وتحديث بيانات ' . $imported_count . ' منتج بنجاح!
صيغة الملف غير مدعومة. يرجى رفع ملف بصيغة CSV.
الرجاء اختيار ملف صالح للرفع.
قم بتصدير منتجاتك الحالية للتعديل عليها، أو استيراد منتجات جديدة دفعة واحدة مع مزامنة بيانات WooCommerce و Google و Yoast SEO.
تصدير ملف CSV يحتوي على كافة بيانات المنتجات الحالية مع إمكانية الفلترة:
إذا كنت تريد إضافة منتجات جديدة تماماً من البداية، يمكنك تحميل قالب الأعمدة الفارغ:
قم برفع ملف الـ CSV (سواء كان ملف منتجات تم تصديره وتعديله أو ملف جديد) لتحديث المنتجات الموجودة بناءً على الـ SKU أو إضافة الجديدة: