簡體   English   中英

使用WPDB方法在Woocommerce中批量插入簡單產品

[英]Bulk insert simple products in Woocommerce using WPDB methods

在WooCommerce中,我必須從json文件中插入25000種產品!

我當時在考慮使用WPDB() insert()方法,因為使用WC_Product()方法時,這是一個比較WC_Product()的過程,需要花費更多時間並在服務器上資源化。

因此,在下面的代碼中,我嘗試使用WPDB() insert()方法:

for( $i = 0; $i < count($data->DataList); $i++ ) {
    $DiamondData = array(
        'Shape'   => $Shape,
        'Size'    => $Size,
        'Color'   => $Color,
        'Clarity' => $Clarity,
        'Cut'     => $Cut
    );

    $wpdb->insert($table,$DiamondData);    

    $my_id = $wpdb->insert_id;
}

任何幫助和指導將不勝感激。

在您的代碼示例中,您似乎正在嘗試向每個產品添加一些產品屬性。 產品屬性有些復雜 ,需要檢查它們是否存在以及這些術語是否也退出。 如果沒有,則需要創建它們。完成后,可以在產品中進行設置。

在foreach循環中使用WPDB()insert()方法,意味着您WPDB()產品插入數據產品,並且在產品數量和需要檢查的內容上也將非常繁重。

但是您沒有義務使用WC_Product()類和方法。 您也可以使用舊的方式使用Wordpress函數,並且在Stack Overflow上有很多示例。

因此,您可以使用如下所示的方法,該方法將插入具有其產品屬性的簡單產品, 但是例如, 每次將過程限制為500個產品

如果由於某種原因停止了該進程,則可以從原處重新啟動它。

代碼(可以嵌入到函數中)

$limit = 500; // Number of products to be processed (here 500 by 500 products)

$index = (int) get_option( 'custom_product_insertion_index' ); // Get the index of processed products
$insertion_count = 0; // Initializing

// Loop through data array to be inserted in each product
for( $i = $index; $i < count($data->DataList); $i++ ) {
    // First insert the new product to get the post ID
    $post_id = wp_insert_post(
      'post_title' => $product_name,
      'post_content' => $product_description,
      'post_type' => 'product',
      'post_status' => 'publish' 
    );

    // Set the product type (here a simple product)
    wp_add_object_terms( $post_id, 'simple', 'product_type' );

    $attributes_data = [];

    $attributes = array(
        'Shape'   => $shape,
        'Size'    => $size,
        'Color'   => $color,
        'Clarity' => $clarity,
        'Cut'     => $cut
    );

    $count = 0;

    // Check if attributes and terms exist, if not we create them
    foreach ($attributes_raw as $attribute => $term ){
        $taxonomy = 'pa_'.sanitize_title($attribute_name); // The attribute taxonomy

        // If taxonomy doesn't exists we create it (Thanks to Carl F. Corneil)
        if( ! taxonomy_exists( $taxonomy ) ){
            register_taxonomy(
                $taxonomy,
               'product_variation',
                array(
                    'hierarchical' => false,
                    'label' => ucfirst($taxonomy),
                    'query_var' => true,
                    'rewrite' => array( 'slug' => sanitize_title($attribute_name)), // The base slug
                ),
            );
        }

        $term_name = ucfirst($term);

        // Add the product attribute term if it doesn't exist.
        if( ! term_exists( $term_name, $taxonomy ) )
            wp_insert_term( $term_name, $taxonomy ); // Create the term

        // Set the term in the product
        wp_set_post_terms( $post_id, $term_name, $taxonomy );

        $attributes_data[$taxonomy] = array(
            'name'         => $taxonomy,
            'value'        => '',
            'position'     => $count,
            'is_visible'   => true,
            'is_variation' => false,
            'is_taxonomy'  => true,
        );
        $count++;
    }

    // Add the product attribute data in the product
    update_post_meta($post_id, '_product_attributes', $attributes_data );

    // Add the product price
    update_post_meta($post_id, '_price', $price );

    // and so on…

    $insertion_count++; 

    // Saving the number of processed products
    update_option( 'custom_product_insertion_index', $index++ );

    if( $insertion_count >= 500 ) break; // stop the loop after 500 products inserted
}

進行數據庫備份…

您可能需要為需要在每個產品中設置的其他數據完成代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM