2016-09-22 92 views
0

我正嘗試在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

所以,你可以幫助我如何對變化做產品設置屬性,我的代碼是完美的或者其他的東西。

+0

LoicTheAztec,試圖插入變體產品,我已經設置屬性書的類型(Adobe PDF和epub),但是當我運行我的代碼並檢查變體產品時,它給出了選擇選項(請參見屏幕截圖1)插入變體,然後我不必選擇書籍類型,它在選擇字段中設置自動屬性值,它應該顯示如screenshot-2 –

回答

1

我在進入錯誤的屬性名稱,所以我已經糾正了象下面這樣:

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

這裏書本型是將要傳遞我的屬性名稱。