Woocommerce通过下载和印刷版本的变化,以编程方式将“简单”产品更改为“可变”产品

时间:2018-10-12 03:18:03

标签: php wordpress woocommerce

我有一个最新的woocommerce(wordpress),我需要为超过1万种产品创建变体,有四种  的变体中,两个是带有库存控制的硬拷贝,其他两个变种的产品则不需要库存控制。

我已经检查了人们通过多种方式创建程序化变体并在开发站点上尝试其脚本的多种方法,以至于我开始发现bug。

<?php



require_once('wp-load.php'); // load the wordpress core
require_once('functions.php') ; // custom functions to grab related data from the wpdb

$import_id = GetTheNextProduct(); // non-wordpress database
$is_variable_product = CheckIfVariable($import_id) ;
 $post_id = GetRelatedWPDI($import_id); // returns related word press post `ID` of the product 
 // ... other variables are set in the same way... ie: $sku = GetSKU($import_id) ;
 // the problem I am having is getting the current variation array into woocommerce variations, 
 // I've been able to do everything else.
 if($is_variable_product == 1) {
 $variations = GetVariations($import_id) ;
 // woocommerce proper way to create variations....????
 } 
 /* out example of $variations...

 $variations = array(
 "0" => array(
         "ProductType" => "Small PDF",
         "filepath" => "/path/to/pdf/file.pdf",
         "filename" => "file.pdf",
         "price" => "5.00",
         "_visibility" => "visible",
         "_stock_status" => "instock",
         "_downloadable" => "yes",
         "_weight" => "",
         "_length" => "",
         "_width" => "",
         "_height" => "",
         "_width" => "", 
         "inventory" => "unlimited",
         "_sku" => "parent",
         ),
 "1" => array(
         "ProductType" => "Large PDF",
         "filepath" => "/path/to/pdf/file-large.pdf",
         "filename" => "file-large.pdf",
         "price" => "10.00",
         "_visibility" => "visible",
         "_stock_status" => "instock",
         "_downloadable" => "yes",
         "_weight" => "",
         "_length" => "",
         "_width" => "",
         "_height" => "", 
         "_width" => "",
         "inventory" => "unlimited",
         "_sku" => "parent",
         ),
 "2" => array(
         "ProductType" => "Small Hardcopy",
         "filepath" => "",
         "filename" => "",
         "price" => "15.00",
         "_visibility" => "visible",
         "_stock_status" => "instock",
         "_downloadable" => "yes",
         "_weight" => "0.5",
         "_length" => "",
         "_width" => "8",
         "_height" => "11",
         "_width" => "", 
         "inventory" => "123",
         "_sku" => "parent",
         ),
 "4" => array(
         "ProductType" => "Large Hardcopy",
         "filepath" => "",
         "filename" => "",
         "price" => "20.00",
         "_visibility" => "visible",
         "_stock_status" => "instock",
         "_downloadable" => "yes",
         "_weight" => "0.5",
         "_length" => "",
         "_width" => "8",
         "_height" => "11",
         "_width" => "", 
         "inventory" => "456",
         "_sku" => "parent",
         ),
 )


 */

update_post_meta( $post_id, '_sku', $sku); 
update_post_meta( $post_id,'_visibility','visible');
wp_set_object_terms($post_id, 'variable', 'product_type');

// do something here to the current $variations array variable to...

$variation_id = wp_insert_post( $variation_post );
$variation = new WC_Product_Variation( $variation_id );

foreach($variations as $vars_sub){
    foreach($vars_sub as $key => $value) {
        update_post_meta( $variation_id, 'attribute_'.$key, $value );
        }
}

我已经阅读,尝试过很多堆栈中的脚本建议,其中一些对保持简单的简单产品很有帮助...但是这个让我很困惑。

1 个答案:

答案 0 :(得分:0)

我通过利用 WC 的 REST API 和 Python 解决了这个问题。我的脚本在这里:https://gist.github.com/Antebios/f53807040247b536e1674758a400dc45

将产品类型从“简单”转换为变量很容易,但添加变体却很棘手。我正在执行迭代循环并从数组创建变体,我还检查变体是否已经存在,因此没有重复的变体。看看。