2017-04-20 7 views
0

すべての製品の属性を簡単な説明に挿入したいので、クイックビューを開いてこの属性をチェックすることができます。WooCommerce 3.0以降の短い説明に属性を追加する

私はすでにこの回答を試してみました:Display specific product attribute values on archives category pages

また、この1:Woocommerce - Display single product attribute(s) with shortcodes in Frontend

をそして私はそれを動作させることができませんでした。私はWooCommerceがバージョン3.0以上に更新されたためだと思う。

誰もそれを作る方法を知っていますか?

おかげ

答えて

1

アップデート3(オートメーションシンプルな製品、WCの互換性のため)WooCommerceバージョンについては

// 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>'; 
} 

更新 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+になりました。この回答コードに関連


動作するはずです:LoicTheAztecの答えのオフに構築するDisplay specific product attribute values on archives category pages

+0

私はPHPで初心者ですので、私は属性を表示するプラグインをussed:「WooCommerce表示属性」 、それがうまく働きました – danieltakeshi

1

を:

彼のコードあなたが事前に定義されている場合にのみ機能しますプロダクト - >属性のWPバックエンドの属性。製品ページで設定した個々の(カスタム)製品属性で作業する場合、wc_get_product_terms()は何も返しません。事前定義された属性は "pa_"接頭辞で認識でき、 "woocommerce_attribute_taxonomies"テーブルに格納されます。私はこのコードの属性ラベルを表示することができますどのように

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

:LoicTheAztecが示唆したように、このコードを使用し、同じ方法でこれらの個々の属性を示すために

// WC 3+と自動化の互換性のための互換性 add_action( 'woocommerce_shop_loop_item_title'、 'custom_attributes_display'、20); function custom_attributes_display(){ グローバル$ 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>'; 

}

関連する問題