localhost - Fastest way to create product variations in WooCommerce?

I am looking into the fastest way for WooCommerce to generate 750+ product variations. My host with 450 variations took

I am looking into the fastest way for WooCommerce to generate 750+ product variations. My host with 450 variations took about 10 minutes, whereas currently, i'm running locally though it's now 18 minutes generating 1000. At the same time, I looked at my CPU speed and noticed there wasn't an as high demand for my CPU and was wondering why. I would figure if I hosted it locally I would be able to utilize my computer specifications. Is there any faster way to do this or am I doing something when hosting it locally?

I am looking into the fastest way for WooCommerce to generate 750+ product variations. My host with 450 variations took about 10 minutes, whereas currently, i'm running locally though it's now 18 minutes generating 1000. At the same time, I looked at my CPU speed and noticed there wasn't an as high demand for my CPU and was wondering why. I would figure if I hosted it locally I would be able to utilize my computer specifications. Is there any faster way to do this or am I doing something when hosting it locally?

Share Improve this question asked May 14, 2019 at 6:07 W__W__ 91 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default -2

this link will give a solution to your problem : https://stackoverflow/questions/47518280/create-programmatically-a-woocommerce-product-variation-with-new-attribute-value

the above solution is based on creating product variation programmatically which may not be safe so I would recommend to follow tool provided by WooCommerce and create a duplicate copy of existing and similar products and just change as you needed.

From a defined variable product ID You will find bellow, a custom function that will add (create) a Product variation. The variable parent product needs to have set for it the needed attributes.

You will need to provide some information as:

the array of attributes/values the Sku, prices and stock…. This data has to be stored in a formatted multi dimensional array (see an example at the end).

This function will check if the attributes values (term name) already exist and if not: - it create it for the product attribute - set it in the parent variable product.

The custom function code:

/**
 * Create a product variation for a defined variable product ID.
 *
 * @since 3.0.0
 * @param int   $product_id | Post ID of the product parent variable product.
 * @param array $variation_data | The data to insert in the product.
 */

function create_product_variation( $product_id, $variation_data ){
    // Get the Variable product object (parent)
    $product = wc_get_product($product_id);

    $variation_post = array(
        'post_title'  => $product->get_title(),
        'post_name'   => 'product-'.$product_id.'-variation',
        'post_status' => 'publish',
        'post_parent' => $product_id,
        'post_type'   => 'product_variation',
        'guid'        => $product->get_permalink()
    );

    // Creating the product variation
    $variation_id = wp_insert_post( $variation_post );

    // Get an instance of the WC_Product_Variation object
    $variation = new WC_Product_Variation( $variation_id );

    // Iterating through the variations attributes
    foreach ($variation_data['attributes'] as $attribute => $term_name )
    {
        $taxonomy = 'pa_'.$attribute; // 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( $attribute ),
                    'query_var' => true,
                    'rewrite' => array( 'slug' => sanitize_title($attribute) ), // The base slug
                ),
            );
        }

        // Check if the Term name exist and if not we create it.
        if( ! term_exists( $term_name, $taxonomy ) )
            wp_insert_term( $term_name, $taxonomy ); // Create the term

        $term_slug = get_term_by('name', $term_name, $taxonomy )->slug; // Get the term slug

        // Get the post Terms names from the parent variable product.
        $post_term_names =  wp_get_post_terms( $product_id, $taxonomy, array('fields' => 'names') );

        // Check if the post term exist and if not we set it in the parent variable product.
        if( ! in_array( $term_name, $post_term_names ) )
            wp_set_post_terms( $product_id, $term_name, $taxonomy, true );

        // Set/save the attribute data in the product variation
        update_post_meta( $variation_id, 'attribute_'.$taxonomy, $term_slug );
    }

    ## Set/save all other data

    // SKU
    if( ! empty( $variation_data['sku'] ) )
        $variation->set_sku( $variation_data['sku'] );

    // Prices
    if( empty( $variation_data['sale_price'] ) ){
        $variation->set_price( $variation_data['regular_price'] );
    } else {
        $variation->set_price( $variation_data['sale_price'] );
        $variation->set_sale_price( $variation_data['sale_price'] );
    }
    $variation->set_regular_price( $variation_data['regular_price'] );

    // Stock
    if( ! empty($variation_data['stock_qty']) ){
        $variation->set_stock_quantity( $variation_data['stock_qty'] );
        $variation->set_manage_stock(true);
        $variation->set_stock_status('');
    } else {
        $variation->set_manage_stock(false);
    }

    $variation->set_weight(''); // weight (reseting)

    $variation->save(); // Save the data
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745165956a4614631.html

相关推荐

  • localhost - Fastest way to create product variations in WooCommerce?

    I am looking into the fastest way for WooCommerce to generate 750+ product variations. My host with 450 variations took

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信