php - Woocommerce 添加具有外部/附属类型的产品

标签 php wordpress woocommerce

我正在尝试以编程方式(使用 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/

相关文章:

php - Wordpress 安装 - php 和 mysql 没有互相交谈来访问数据库?

php - 如何在wordpress主题中包含styles.css?

html - 为什么我网站的页脚会卡在页面顶部?

mysql - 使用 JOIN 在 Wordpress 中进行高级 MySQL 查询

php - 在 WooCommerce 中通过其别名获取产品属性标签名称

wordpress - 店铺页面先展示特色商品,最后展示缺货商品

php if else 语句在选择框中

PHP偶数或奇数脚本

php - Bootstrap jquery twitter 本地 Bootstrap

php - 有条件地为特定产品 ID 和数量自动应用优惠券