我正在尝试以编程方式(使用 PHP)将 Woocommerce 产品插入我的网站。我所有的产品都是外部/附属产品类型。我已经使用以下代码成功创建了一个产品,但它默认为“简单产品”产品类型。我的问题是如何使用我的 PHP 帖子创建或元数据更新将产品类型更改为外部/附属?
我试过这个命令但没有成功:
update_post_meta($post_id, 'product-type' , 'external' );
这是用于创建产品的代码,其中分配了基本属性:
require_once("../wp-load.php");
$new_post = array(
'post_title' => "Title of My Product",
'post_content' => 'Full description of My Product',
'post_status' => 'publish',
'post_type' => 'product',
'is_visible' => '1'
);
$post_id = wp_insert_post($new_post);
update_post_meta($post_id, '_sku', '5000' );
update_post_meta($post_id, '_regular_price' , '99.95');
update_post_meta($post_id, '_product_url' , 'http://www.myaffiliatesURL.com');
update_post_meta($post_id, '_button_text' , 'Buy from My Affiliate' );
update_post_meta($post_id, '_aioseop_description' , 'Short description of My Product' );
update_post_meta($post_id, '_visibility' , 'visible' );
最佳答案
可以试试这个。
wp_set_object_terms ($post_id, 'external', 'product_type');
你还需要设置'parent_id'
关于php - Woocommerce 添加具有外部/附属类型的产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21647733/