簡體   English   中英

以編程方式批量創建 WooCommerce 產品

[英]Bulk create WooCommerce products programmatically

可以在 WooCommerce 上批量創建產品嗎? 我用的是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']}");
}

這需要很多時間,因為它將對每個產品進行查詢,更糟糕的是,這將是一個定期運行的 cron 作業。 我還必須檢查產品是否存在,在這種情況下,更新它而不是創建新產品。

所以查詢的數量將乘以 2。

產品存在嗎? 更新它,否則創建它

有一個更好的方法嗎? 也許我應該直接查詢數據庫,但它看起來很臟。

是否有任何 WP 功能可以查詢數據庫而無需創建自己的數據庫連接?

假設您有一個這樣的數組,並且您有唯一的SKU來標識產品。

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

通過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;
}

這將使您對如何創建/更新產品有一個簡要的了解; 如果您需要任何進一步的幫助,那么您必須共享您的 4-5 個數組元素,以便我可以了解您要創建的產品類型以及它將具有的字段/元。

希望這可以幫助!

暫無
暫無

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

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