以编程方式创建产品变量?

时间:2016-09-22 07:07:09

标签: php wordpress woocommerce

我正在尝试在woocommerce中创建产品变体,并使用以下代码插入变体后期类型。

foreach ($book_tag as $bookTag) {
    $post_name = 'ABE-'.$post_id.'book-type'.$bookTag;
    $my_post = array(
        'post_title' => 'BookType ' . $bookTag . ' for #' . $post_id,
        'post_name' => $post_name,
        'post_status' => 'publish',
        'post_parent' => $post_id,
        'post_type' => 'product_variation',
        'guid' => home_url() . '/?product_variation=' . $post_name
    );
    $attID = wp_insert_post($my_post);
    update_post_meta($attID, 'book_type', $bookTag);
    update_post_meta($attID, '_price', $bookPrice);
    update_post_meta($attID, '_regular_price', $bookPrice);
    update_post_meta($attID, '_sale_price', $salePrice );
    update_post_meta($attID, '_sku', $post_name);
    update_post_meta($attID, '_virtual', 'no');
    update_post_meta($attID, '_downloadable', 'yes');
    update_post_meta($attID, '_manage_stock', 'no');
    update_post_meta($attID, '_stock_status', 'instock');
}

这工作正常,但它没有设置自动书类型,我必须从下拉选项中选择属性,但我希望它应该根据属性值自动选择属性。 以下屏幕截图显示给我:

enter image description here

但我想要关注截图

enter image description here

那么你能帮助我如何为变异产品设置属性,我的代码是完美的还是其他的东西。

1 个答案:

答案 0 :(得分:1)

我输入了错误的属性名称,所以我更正了如下所示:

update_post_meta($attID, 'attribute_book-type', $bookTag);

这里book-type是我的属性名称,将被传递。

相关问题