dte49889 2018-10-12 03:18
浏览 30

Woocommerce以编程方式将“简单”产品更改为具有下载和硬拷贝变体的“可变”产品

I have a woocommerce (wordpress) up to date, with over 10k products which I need to create variations for, there are four kinds of variations, two are hardcopy with inventory control, the other two variation of the products does not need inventory control.

I have check out so many various ways people have created programmatically variations and attempted their scripts on a dev site, that I am starting to get bug eyed.

<?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 );
        }
}

I've read, tried, so many of the script suggestions within stack, some which have helped a lot with simple products that remain simple... but this one has me stumped.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算
    • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
    • ¥20 有人知道这种图怎么画吗?
    • ¥15 pyqt6如何引用qrc文件加载里面的的资源
    • ¥15 安卓JNI项目使用lua上的问题
    • ¥20 RL+GNN解决人员排班问题时梯度消失
    • ¥60 要数控稳压电源测试数据
    • ¥15 能帮我写下这个编程吗
    • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路