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