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

问题描述 投票:1回答:1

在WooCommerce中,我必须从json文件中插入25000个产品!

我在考虑使用WPDB() insert()方法,因为当使用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;
}

任何帮助和指导将非常感谢。

php wordpress woocommerce product custom-taxonomy
1个回答
2
投票

在您的代码示例中,您似乎尝试为每个产品添加一些产品属性。产品属性很复杂,需要检查它们是否存在以及术语是否也存在。如果他们不这样做,你需要创建它们......一旦完成,你可以在产品中设置它们。

在foreach循环中使用WPDB()insert()方法意味着您要按产品插入数据,并且对于产品数量和需要检查的事物也会非常沉重。

但是你没有义务使用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
}

进行数据库备份......

您可能需要完成需要在每个产品中设置的其他数据的代码。

© www.soinside.com 2019 - 2024. All rights reserved.