简体   繁体   English

以编程方式批量创建 WooCommerce 产品

[英]Bulk create WooCommerce products programmatically

It is possible to bulk create products on WooCommerce?可以在 WooCommerce 上批量创建产品吗? I am using the wp-cli Product command but it seems that I have to create one by one.我用的是wp-cli Product命令但是好像要一一创建。

<?php

$products = array(
    array('title' => 'My product 1'),
    array('title' => 'My product 2'),
    // ...
    // 10.000 more products
);

foreach ($products as $product) {
    $cliProduct = new WC_CLI_Product();
    $cliProduct->create(array(), $product);
    WP_CLI::success("Added Product #{$product['title']}");
}

This takes a lot of time since it is going to make a query for each product, even worse, it is going to be a cron job that will be run regularly.这需要很多时间,因为它将对每个产品进行查询,更糟糕的是,这将是一个定期运行的 cron 作业。 I also have to check if the product exist, in that case, update it instead of create a new product.我还必须检查产品是否存在,在这种情况下,更新它而不是创建新产品。

So the number of queries will be multiplied by 2.所以查询的数量将乘以 2。

Product Exists?产品存在吗? Update it, else Create it更新它,否则创建它

Is there a better way to do this?有一个更好的方法吗? Maybe I should query the database directly, but it looks dirty.也许我应该直接查询数据库,但它看起来很脏。

Is there any WP function to query the database without having to create my own DB connection?是否有任何 WP 功能可以查询数据库而无需创建自己的数据库连接?

Assuming you have an array like this and you have Unique SKU to identify product.假设您有一个这样的数组,并且您有唯一的SKU来标识产品。

$products = [
    0 => [
        'title' => 'My Simple product 1',
        'sku' => 'sku1',
        'product_cat' => 'My cat'
    //...
    //...
    ],
    1 => [
        'title' => 'My Simple product 1',
        'sku' => 'sku1'
    //...
    //...
    ]
];

Pass the above array through myCustomProduct() method.通过myCustomProduct()方法传递上述数组。

function myCustomProduct($product_array)
{
    if (!empty($product_array)):
        foreach ($product_array as $product):
            $product_id = wc_get_product_id_by_sku($product['sku']);
            //no product exist with the given SKU so create one
            if (!$product_id):
                $post = [
                    'post_author' => '',
                    'post_content' => $product['content'],
                    'post_status' => "publish",
                    'post_title' => wp_strip_all_tags($product['title']),
                    'post_name' => $product['title'],
                    'post_parent' => '',
                    'post_type' => "product",
                ];
                //Create Post
                $product_id = wp_insert_post($post, $wp_error);

                //set Product Category
                wp_set_object_terms($product_id, $product['product_cat'], 'product_cat');

                //set product type
                wp_set_object_terms($product_id, 'simple', 'product_type');

                update_post_meta($product_id, '_sku', $product['sku']);
                update_post_meta($product_id, 'total_sales', '0');

            //product found
            else:
                $post = [
                    'ID' => $product_id,
                    'post_title' => $product['title'],
                    'post_content' => $product['content'],
                ];
                $post_id = wp_update_post($post, true);
//              if (is_wp_error($post_id))
//              {
//                  $errors = $post_id->get_error_messages();
//                  foreach ($errors as $error)
//                  {
//                      echo $error;
//                  }
//              }
            endif;

            update_post_meta($product_id, '_visibility', 'visible');
            update_post_meta($product_id, '_stock_status', 'instock');
            update_post_meta($product_id, '_product_attributes', array());
            update_post_meta($product_id, '_manage_stock', "yes");
            update_post_meta($product_id, '_backorders', "no");
            update_post_meta($product_id, '_stock', $product['qty']);
            update_post_meta($product_id, '_price', $product['price']);
            //update_post_meta($product_id, '_downloadable', 'yes');
            //update_post_meta($product_id, '_virtual', 'yes');
            //update_post_meta($product_id, '_regular_price', "1");
            //update_post_meta($product_id, '_sale_price', "1");
            //update_post_meta($product_id, '_purchase_note', "");
            //update_post_meta($product_id, '_featured', "no");
            //update_post_meta($product_id, '_weight', "");
            //update_post_meta($product_id, '_length', "");
            //update_post_meta($product_id, '_width', "");
            //update_post_meta($product_id, '_height', "");
            //update_post_meta($product_id, '_sale_price_dates_from', "");
            //update_post_meta($product_id, '_sale_price_dates_to', "");
            //update_post_meta($product_id, '_price', "1");
            //update_post_meta($product_id, '_sold_individually', "");
        endforeach;
    endif;
}

This'll give you a brief idea as how to create/update product;这将使您对如何创建/更新产品有一个简要的了解; if you need any further assistance then you have to share your 4-5 array elements, so that I can get to know what type of product you want to create and what field/meta it will be having.如果您需要任何进一步的帮助,那么您必须共享您的 4-5 个数组元素,以便我可以了解您要创建的产品类型以及它将具有的字段/元。

Hope this helps!希望这可以帮助!

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

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