简体   繁体   English

使用WPDB方法在Woocommerce中批量插入简单产品

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

In WooCommerce, I have to insert 25 000 product from a json file! 在WooCommerce中,我必须从json文件中插入25000种产品!

I was thinking using the WPDB() insert() method, because When using WC_Product() methods, it's a heavier process that takes more time and ressources on the server. 我当时在考虑使用WPDB() insert()方法,因为使用WC_Product()方法时,这是一个比较WC_Product()的过程,需要花费更多时间并在服务器上资源化。

So in the below code I am trying to use WPDB() insert() method: 因此,在下面的代码中,我尝试使用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;
}

Any help and guidance will be really appreciated. 任何帮助和指导将不胜感激。

In your code example, it seems that you are trying to add some product attributes to each product. 在您的代码示例中,您似乎正在尝试向每个产品添加一些产品属性。 Product attributes are something complicated that requires to check if they exists and if the terms exits too. 产品属性有些复杂 ,需要检查它们是否存在以及这些术语是否也退出。 If they don't , you need to create them… Once done you can set them in the product. 如果没有,则需要创建它们。完成后,可以在产品中进行设置。

Using WPDB() class insert() method in a foreach loop, means that you are inserting data product by product, and it's going also to be very heavy too regarding the number of products and the things that need to be checked. 在foreach循环中使用WPDB()insert()方法,意味着您WPDB()产品插入数据产品,并且在产品数量和需要检查的内容上也将非常繁重。

But you are not obliged to use WC_Product() Class and methods. 但是您没有义务使用WC_Product()类和方法。 You can also use the old way using Wordpress functions and there is many examples on Stack Overflow. 您也可以使用旧的方式使用Wordpress函数,并且在Stack Overflow上有很多示例。

So you could use something like below, that will insert simple products with their product attributes, but limiting the process to 500 products each time , for example. 因此,您可以使用如下所示的方法,该方法将插入具有其产品属性的简单产品, 但是例如, 每次将过程限制为500个产品

If the process is stopped for any reason, you can restart it where it was… 如果由于某种原因停止了该进程,则可以从原处重新启动它。

The code (that you can embed in a function) : 代码(可以嵌入到函数中)

$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
}

Make a database backup… 进行数据库备份…

You might need to complete the code for other data that need to be set in each product. 您可能需要为需要在每个产品中设置的其他数据完成代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM