如何为WooCommerce产品的变体指定特定属性?

时间:2017-02-26 作者:Donatas

我正在创建一个具有多种变体的产品:

//创建产品

        $post = array(
         \'post_title\'   => $title,
         \'post_content\' => $description,
         \'post_status\'  => "publish",
         \'post_excerpt\' => $excerpt,
         \'post_name\'    => $slug, //name/slug
         \'post_type\'    => "product"
 );


$new_post_id = wp_insert_post( $post);
//创建所需的属性

$product_attributes = array();

$product_attributes[\'print_size\'] = array(
    \'name\' => \'print_size\',
    \'value\' => implode(\'|\', $availableSizes),
    \'position\' => 0,
    \'is_visible\' => 0,
    \'is_variation\' => 1,
    \'is_taxonomy\' => 0
);

$product_attributes[\'paper_type\'] = array(
    \'name\' => \'paper_type\',
    \'value\' => implode(\'|\', $availableMaterial),
    \'position\' => 0,
    \'is_visible\' => 0,
    \'is_variation\' => 1,
    \'is_taxonomy\' => 0
);


update_post_meta($new_post_id, \'_product_attributes\', $product_attributes);
//创建变体

$my_post1 = array(
    \'post_title\' => \'SizeType122\',
    \'post_name\' => \'post_name222\',
    \'post_status\' => \'publish\',
    \'post_parent\' => $new_post_id,
    \'post_type\' => \'product_variation\',
    \'guid\' => home_url() . \'/?product_variation=1\' 
);

$attID1 = wp_insert_post($my_post1);

update_post_meta($attID1, \'_virtual\', \'no\');
update_post_meta($attID1, \'_downloadable\', \'no\');
update_post_meta($attID1, \'_manage_stock\', \'no\');
update_post_meta($attID1, \'_stock_status\', \'instock\');
update_post_meta($attID1, \'_sku\', \'300\');
update_post_meta($attID1, \'_price\', \'300\');
它以应有的属性创建了一个变体,但如果我检查产品变体,它会给我带来如下结果:

Array
(
    [0] => Array
        (
            [variation_id] => 1542
            [variation_is_visible] => 1
            [variation_is_active] => 1
            [is_purchasable] => 1
            [display_price] => 300
            [display_regular_price] => 300
            [attributes] => Array
                (
                    [attribute_print_size] => 
                    [attribute_paper_type] => 
                )
  [image_src] => 
        [image_link] => 
        [image_title] => 
        [image_alt] => 
        [image_caption] => 
        [image_srcset] => 
        [image_sizes] => 
        [price_html] => 
        [availability_html] => 
        [sku] => 300
        [weight] =>  kg
        [dimensions] => 
        [min_qty] => 1
        [max_qty] => 
        [backorders_allowed] => 
        [is_in_stock] => 1
        [is_downloadable] => 
        [is_virtual] => 
        [is_sold_individually] => no
        [variation_description] => 
    )
()

所以属性是空的,如果我转到管理端,我可以看到“任何打印大小”和“任何纸张类型”作为变体。如果我选择特定属性并保存在管理中,我可以在我共享的数组中看到这些值:

  [attributes] => Array
                (
                    [attribute_print_size] => 8x10
                    [attribute_paper_type] => Enhanced Matte Paper
                )
那么,我如何通过编程实现这一点呢?

1 个回复
SO网友:Donatas

对于所有将要为此而挣扎的人,你只需要使用以下方法:

      update_post_meta($attID1, \'attribute_print_size\', \'8x10\');
 update_post_meta($attID1, \'attribute_paper_type\', \'Premium Luster Photo\');
这将设置精确的变化。

相关推荐