2017-04-20 81 views
0

我想將所有產品的屬性插入到其簡短描述中,以便客戶端可以打開快速查看並檢查此屬性。在WooCommerce中添加屬性以簡短描述3.0+

我已經嘗試過這樣的回答:Display specific product attribute values on archives category pages

而且這一個:Woocommerce - Display single product attribute(s) with shortcodes in Frontend

我無法使它工作。我認爲這應該是因爲WooCommerce更新到版本3.0以上

有沒有人知道一種方法來做到這一點?

感謝

回答

1

更新3(自動化簡單的產品,WC兼容性)

// Compatibility for WC 3+ and automation enhancements 
add_action('woocommerce_shop_loop_item_title', 'custom_attributes_display', 20); 
function custom_attributes_display(){ 
    global $product; 

    // Just for simple products 
    if(! $product->is_type('simple')) return; 

    $loop_count = 0; 

    echo '<div>'; 

    // Get the attributes taxonomy slugs (Updated and dynamic now) 
    $attributes_taxonomy = $product->get_attributes(); 
    // OR set an indexed array of taxonomy slug (key) and name (value) to chose which ones, like: 
    // $attributes_taxonomy = array('pa_nopeus' => 'Nopeus', 'pa_liito' => 'Liito, 'pa_vakaus' => 'Vaukaus'); 

    foreach($attributes_taxonomy as $taxonomy => $attribute) { 

     // Getting the term names of an attribute (set in a coma separated string if many values) 
     $attribute_terms = wp_get_post_terms(get_the_id(), $taxonomy, array('fields' => 'names')); 
     $terms_string = implode(',', $attribute_terms); 

     // Displays only if attribute exist for the product 
     if(count($attribute_terms) > 0){ // Updated 
      echo $terms_string; 

      // Separating each number by a " | " (Updated and dynamic now) 
      $attr_count = count($attributes_taxonomy); 
      $loop_count++; 
      if($loop_count < $attr_count && $attr_count > 1) echo ' | '; 
     } 
    } 

    echo '</div>'; 
} 

更新對於WooCommerce 3.0以上版本而已。

// For WooCommerce Version 3.0+ (only) 
add_action('woocommerce_shop_loop_item_title', 'custom_attributes_display', 20); 
function custom_attributes_display(){ 

    // Just for product category archives pages 
    if(is_product_category()){ 
     global $product; 

     // the array of attributes names 
     $attribute_names = array('pa_nopeus', 'pa_liito', 'pa_vakaus', 'pa_feidi'); 
     foreach($attribute_names as $key => $attribute_name) { 

      // For WooCommerce version 3.0+ 
      $product_id = $product->get_id(); // WC 3.0+ 

      // Getting the value of an attribute (OK for WC 3.0+) 
      $wc_term = wc_get_product_terms($product_id, $attribute_name); 
      $attribute_value = array_shift($wc_term); 

      // Displays only if attribute exist for the product 
      if(!empty($attribute_value) || '0' == $attribute_value){ // Updated 
       echo $attribute_value; 

       // Separating each number by a "/" 
       if($key < 3) echo '/'; 
      } 
     } 
    } 
} 

代碼會出現在您的活動子主題(或主題)的function.php文件中,或者也存在於任何插件文件中。

應該在WC現在的工作3.0+


與此相關的答案代碼:Display specific product attribute values on archives category pages

+0

因爲我在PHP中的新手,我ussed一個插件來顯示屬性: 「WooCommerce顯示屬性」,效果不錯 – danieltakeshi

1

打造關LoicTheAztec的回答:

他的代碼只能如果您已預定義了產品 - >屬性下WP後端的屬性。如果您使用在產品頁面上設置的個人(自定義)產品屬性,則wc_get_product_terms()不會返回任何內容。您可以通過「pa_」前綴識別預定義的屬性,它們存儲在「woocommerce_attribute_taxonomies」表中。

爲了以同樣的方式顯示這些個人的屬性LoicTheAztec建議,使用此代碼:

add_action('woocommerce_shop_loop_item_title', 'custom_attributes_display', 20); 
function custom_attributes_display() 
{ 
    // Just for product category archive pages 
    if(is_product_category()) 
    { 
     global $product; 

     // get all product attributes 
     $attributes = $product->get_attributes(); 
     // the array of attributes you want to display (shown in same order) 
     $show_attributes = array('My Attribute A', 'Another Attribute B'); 
     foreach($show_attributes as $key => $show_attribute) 
     { 
      foreach($attributes as $attribute) 
      { 
       // check if current attribute is among the ones to be shown 
       if ($attribute->get_name() == $show_attribute) 
       { 
        echo $attribute->get_options()[0]; 
        // seperate attributes by "/" 
        if (count($show_attributes) > 1) 
         echo '/'; 
        unset($show_attributes[$key]); 
        break; 
       } 
      } 
     } 
    } 
} 
1

我怎麼能顯示與此代碼屬性標籤?

爲WC //兼容性3+和自動化增強 ADD_ACTION( 'woocommerce_shop_loop_item_title', 'custom_attributes_display',20); function custom_attributes_display(){ global $ product;

// Just for simple products 
if(! $product->is_type('simple')) return; 

$loop_count = 0; 

echo '<div>'; 

// Get the attributes taxonomy slugs (Updated and dynamic now) 
$attributes_taxonomy = $product->get_attributes(); 
// OR set an indexed array of taxonomy slug (key) and name (value) to chose which ones, like: 
// $attributes_taxonomy = array('pa_nopeus' => 'Nopeus', 'pa_liito' => 'Liito, 'pa_vakaus' => 'Vaukaus'); 

foreach($attributes_taxonomy as $taxonomy => $attribute) { 

    // Getting the term names of an attribute (set in a coma separated string if many values) 
    $attribute_terms = wp_get_post_terms(get_the_id(), $taxonomy, array('fields' => 'names')); 
    $terms_string = implode(',', $attribute_terms); 

    // Displays only if attribute exist for the product 
    if(count($attribute_terms) > 0){ // Updated 
     echo $terms_string; 

     // Separating each number by a " | " (Updated and dynamic now) 
     $attr_count = count($attributes_taxonomy); 
     $loop_count++; 
     if($loop_count < $attr_count && $attr_count > 1) echo ' | '; 
    } 
} 

echo '</div>'; 

}

相關問題