我的商店銷售乙烯貼紙。每個產品(貼紙)有144個變化(24種顏色,3種尺寸和2種方向)。每個變化都是分配唯一SKU所必需的。WooCommerce:通過代碼創建產品
手動填充目錄是不現實的。我將製作一個表單,用戶可以在其中指定產品的名稱,說明和主要圖像,以及可能的尺寸和顏色。處理表單時,我需要創建一個產品及其所有變體。
如何創建一個產品及其變化?
我的商店銷售乙烯貼紙。每個產品(貼紙)有144個變化(24種顏色,3種尺寸和2種方向)。每個變化都是分配唯一SKU所必需的。WooCommerce:通過代碼創建產品
手動填充目錄是不現實的。我將製作一個表單,用戶可以在其中指定產品的名稱,說明和主要圖像,以及可能的尺寸和顏色。處理表單時,我需要創建一個產品及其所有變體。
如何創建一個產品及其變化?
我也有類似的情況,這裏是我發現了什麼。
產品實際上是一個custom post type(非常明顯!:P),因此您可以使用wp_insert_post
來插入新產品。插入之後,你得到新的產品類型後的ID,使用update_post_meta
分別設置Meta鍵和meta值_visibility
和visible
。如果您未設置可見性,則新添加的產品將永遠不會在您的店鋪中可見。或者,您也可以從後端設置可視性。對於各種尺寸的產品,請使用該產品的變體。您可以爲每個變體設置不同的類型,價格,SKU等。所有這些都是後期元,因此您可以使用PHP代碼來添加變體和內容。研究postmeta
表以查看密鑰名稱。
傑森在his comment寫道,REST API是去這裏的路。即使沒有HTTP REST請求,也可以完成此操作,即使在禁用其REST接口的Wordpress安裝中也可以工作。
下面是一個簡單的函數,我對爲此作出:
$products_controler = new WC_REST_Products_Controller();
function create_item($rest_request) {
global $products_controler;
if (!isset($rest_request['status']))
$rest_request['status'] = 'publish';
$wp_rest_request = new WP_REST_Request('POST');
$wp_rest_request->set_body_params($rest_request);
return $products_controler->create_item($wp_rest_request);
}
這裏,$rest_request
是一個數組,你通常是通過REST發送(見文檔here)。因爲我需要多次調用這個函數,我不想每次都重新創建對象
的$products_controler
變量是全球性的。隨意使它成爲本地。
這適用於所有類型的產品(簡單,分組,變量,......),它應該是WooCommerce的比手動通過wp_insert_post
和update_post_meta
加入產品內部變化的能力更強。
編輯:鑑於這個答案仍然得到偶爾給予好評,這裏是一個WooCommerce 3.0+更新。變化是變化不再自動添加,所以我們必須自己做。
這是當前版本的函數:
protected function create_item($rest_request) {
if (! isset($rest_request['status'])) {
$rest_request['status'] = $this->plugin->get_option('default_published_status');
}
if (! isset($this->products_controler)) {
$this->products_controler = new WC_REST_Products_Controller();
}
$wp_rest_request = new WP_REST_Request('POST');
$wp_rest_request->set_body_params($rest_request);
$res = $this->products_controler->create_item($wp_rest_request);
$res = $res->data;
// The created product must have variations
// If it doesn't, it's the new WC3+ API which forces us to build those manually
if (! isset($res['variations']))
$res['variations'] = array();
if (count($res['variations']) == 0 && count($rest_request['variations']) > 0) {
if (! isset($this->variations_controler)) {
$this->variations_controler = new WC_REST_Product_Variations_Controller();
}
foreach ($rest_request['variations'] as $variation) {
$wp_rest_request = new WP_REST_Request('POST');
$variation_rest = array(
'product_id' => $res['id'],
'regular_price' => $variation['regular_price'],
'image' => array('id' => $variation['image'][0]['id'],),
'attributes' => $variation['attributes'],
);
$wp_rest_request->set_body_params($variation_rest);
$new_variation = $this->variations_controler->create_item($wp_rest_request);
$res['variations'][] = $new_variation->data;
}
}
return $res;
}
這在Kite Print and Dropshipping on Demand插件使用的,從1(即將公開)版本開始。1,在文件api/publish_products.php
。
還有一個更長的「快速」版本,名爲create_products_fast
,它直接寫入數據庫,使其對未來WP/WC更改的彈性可能降低,但速度更快(對於我們的34產品範圍在我的測試計算機上)。
您好,版本'create_products_fast'是WC的一部分還是用於風箏沖印插件? – 2017-11-23 13:48:10
@ JeansK.Real它是Kite(現在已停用)插件中的一個函數,其中通常通過許多WP函數完成的所有SQL請求(在我們的例子中爲幾百個調用)僅分組爲5或6個批量SQL調用,使其非常快速,但對WP/WC的變化也不太靈活。作爲這種情況下的備份,我們有一個設置可以打開或關閉該功能(在後一種情況下,使用上述功能)。 – 2017-11-24 12:36:52
我用這個代碼:
$sku = 21333;
$size = 'S';
$stock = 2;
$price_a = 60;
$price_b = 30;
$product_parent = get_product_by_sku($sku);
$product = new WC_Product_Variable($product_parent->id);
$variations = $product->get_available_variations();
// First off all delete all variations
foreach($variations as $prod_variation) {
$metaid=mysql_query("SELECT meta_id FROM wp_postmeta WHERE post_id = ".$prod_variation['variation_id']);
while ($row = mysql_fetch_assoc($metaid)) {
mysql_query("DELETE FROM wp_postmeta WHERE meta_id = ".$row['meta_id']);
}
mysql_query("DELETE FROM wp_posts WHERE ID = ".$prod_variation['variation_id']);
}
// Now add new variation
$thevariation = array(
'post_title'=> '',
'post_name' => 'product-' . $product_parent->id . '-variation',
'post_status' => 'publish',
'post_parent' => $product_parent->id,
'post_type' => 'product_variation',
'guid'=>home_url() . '/?product_variation=product-' . $product_parent->id . '-variation'
);
$variation_id = wp_insert_post($thevariation);
update_post_meta($variation_id, 'post_title', 'Variation #' . $variation_id . ' of '. $product_parent->id);
wp_set_object_terms($variation_id, $size, 'pa_size');
update_post_meta($variation_id, 'attribute_pa_size', $size);
update_post_meta($variation_id, '_regular_price', $price_a);
update_post_meta($variation_id, '_downloadable_files', '');
update_post_meta($variation_id, '_download_expiry', '');
update_post_meta($variation_id, '_download_limit', '');
update_post_meta($variation_id, '_sale_price_dates_to', '');
update_post_meta($variation_id, '_sale_price_dates_from', '');
update_post_meta($variation_id, '_backorders', 'no');
update_post_meta($variation_id, '_stock_status', 'instock');
update_post_meta($variation_id, '_height', '');
update_post_meta($variation_id, '_manage_stock', 'yes');
update_post_meta($variation_id, '_width', '');
update_post_meta($variation_id, '_sale_price_dates_from', '');
update_post_meta($variation_id, '_backorders', 'no');
update_post_meta($variation_id, '_stock_status', 'instock');
update_post_meta($variation_id, '_manage_stock', 'yes');
update_post_meta($variation_id, '_height', '');
update_post_meta($variation_id, '_width', '');
update_post_meta($variation_id, '_length', '');
update_post_meta($variation_id, '_weight', '');
update_post_meta($variation_id, '_downloadable', 'no');
update_post_meta($variation_id, '_virtual', 'no');
update_post_meta($variation_id, '_thumbnail_id', '0');
update_post_meta($variation_id, '_sku', '');
update_post_meta($variation_id, '_sale_price', $price_b);
update_post_meta($variation_id, '_price', $price_b);
update_post_meta($product_parent->id, '_min_variation_price', $price_b);
update_post_meta($product_parent->id, '_max_variation_price', $price_b);
update_post_meta($product_parent->id, '_min_price_variation_id', $variation_id);
update_post_meta($product_parent->id, '_max_price_variation_id', $variation_id);
update_post_meta($product_parent->id, '_min_variation_regular_price', $price_a);
update_post_meta($product_parent->id, '_max_variation_regular_price', $price_a);
update_post_meta($product_parent->id, '_min_regular_price_variation_id', $variation_id);
update_post_meta($product_parent->id, '_max_regular_price_variation_id', $variation_id);
update_post_meta($product_parent->id, '_min_variation_sale_price', $price_b);
update_post_meta($product_parent->id, '_max_variation_sale_price', $price_b);
update_post_meta($product_parent->id, '_min_sale_price_variation_id', $variation_id);
update_post_meta($product_parent->id, '_max_sale_price_variation_id', $variation_id);
update_post_meta($product_parent->id, '_price', $price_b);
update_post_meta($variation_id, '_stock', $stock);
update_post_meta($product_parent->id, 'post_status', 'publish');
基於Vedran's answer,這裏的最少的代碼爲通過PHP張貼WooCommerce產品:
$data = [
'name' => 'Test product',
'description' => 'Lorem ipsum',
];
$request = new WP_REST_Request('POST');
$request->set_body_params($data);
$products_controller = new WC_REST_Products_Controller;
$response = $products_controller->create_item($request);
謝謝!這種方式是絕對正確的,因爲它使用每次修改WooCommerce API。我測試了這個代碼,並在meta和其他表格中創建了所有的nessesary產品數據。 – realmag777 2017-12-12 16:37:43
你需要創建一個圖像或只是打印出來可能選擇? – Peon 2012-07-16 11:50:48
現在有一個V2 REST API,我會推薦使用它來創建產品。 API可以以不同的用戶身份登錄,並且可以滿足手動構建產品時可能忽略的所有細節。它還涉及到將一個單一的大數據結構放在一起,並將其發送到一個動作中。 – Jason 2015-02-28 23:41:47