2013-10-22 55 views
12

如何增加價值的一個woocommerce通過代碼屬性? 我創建了一個叫做「調度時間」(分類:pa_dispatch)屬性,現在我想價值添加到特定產品的調度屬性。woocommerce:增加價值的產品屬性

如何做到這一點編程?

enter image description here

回答

14

我找到了答案,你需要使用wp_set_object_terms設置分類的對象而言,

wp_set_object_terms($object_id, $terms, $taxonomy, $append); 

其中,$追加可truefalse,如果屬實,該標籤將被追加到現有標籤,如果爲false,則替換標籤。

在我的例子,

wp_set_object_terms($object_id, '2 Business Days', 'pa_dispatch' , false); 

這裏,pa_dispatch是宇商貿分類。

+1

謝謝。在你的functions.php中,你把這個放在哪裏?什麼是$ object_id? – nicmare

+0

@nicmare把它放在你想要改變值的地方,但是如果你直接把這個語句放在functions.php中,那麼每次加載任何文件時都會運行這個函數!另外,$ object_id只是產品的id,即。,該特定產品的帖子ID。 – Rao

+0

當然,我會用save_post掛鉤來解僱它。我會試一試。謝謝 – nicmare

2

不能增加價值的屬性。您需要使產品變量,創建變體併爲其分配屬性。現在在這個變體中,您可以指定一個值。

Attributes Configuration

Variations Configuration

讀取模式:

  1. http://docs.woothemes.com/document/product-variations/
  2. http://www.youtube.com/watch?v=7PX8MWBOAeo

編輯:

更澄清的問題後,這裏是一個更新的解決方案。

添加下面的函數,你的functions.php。在適當的鉤子上調用它並傳遞產品ID以及屬性值。

function se19519561_set_attributes($post_id, $attributes) { 

    //Type attribute 
    $product_attributes['type'] = array(
     //Make sure the 'name' is same as you have the attribute 
     'name' => htmlspecialchars(stripslashes('Dispatch Time')), 
     'value' => $attributes, 
     'position' => 1, 
     'is_visible' => 1, 
     'is_variation' => 1, 
     'is_taxonomy' => 0 
    ); 

//Add as post meta 
update_post_meta($post_id, '_product_attributes', $product_attributes); 

} 

希望這有助於!

+0

不,這不是我想要實現的,我可以通過GUI添加值,但我想在php中以編程方式執行此操作。 – Rao

+0

即,我想通過php函數將'3個工作日'的值添加到特定產品的'Dispatch time'屬性中。 – Rao

+1

您是否想要在訪問產品頁面時即時添加屬性,或者是否希望一次將其添加到所有產品? –