2017-06-01 82 views
0

簡單的問題如何在購物車woocomerce中顯示產品屬性:例如顏色:紅色,不確定是否有一些代碼需要像hook或代碼一樣添加到fundctions.php,或者它可以是通過woocomerce設置完成,沒有在網上找到任何有用的信息,任何幫助表示讚賞。在購物車中顯示屬性值和名稱woocomerce

回答

2

剛做一件簡單的事情如下,你會得到你所有的cart_item -

add_filter('woocommerce_cart_item_name', 'add_variations_in_cart', 10, 3); 
function add_variations_in_cart($name, $cart_item, $item_key){ 
    $product_variation = ''; 
    if(!empty($cart_item['variation_id']) && $cart_item['variation_id'] != 0){ 
     if(is_array($cart_item['variation']) && !empty($cart_item['variation'])){ 
      foreach ($cart_item['variation'] as $key => $value) { 
      $product_variation .= '<br>'.ucfirst(str_replace('attribute_pa_', '', $key)).' : '.ucfirst($value); 
     } 
    } 
} 

echo $name.$product_variation; 

}

就這麼簡單。謝謝。

+0

如何增加自定義樣式的跨度?謝謝 – Zygimantas

+0

你可以根據你的需要在$ product_variation中傳遞你的html結構。根據你的情況,像$ product_variation。='
'。ucfirst(str_replace('attribute_pa_','',$ key))。' :'.ucfirst($ value)。''; – itzmekhokan

0

酪氨酸這個插件https://wordpress.org/plugins/woocommerce-show-attributes/

OR

WooCommerce:我的購物車頁面上的每個項目下面列出的所有產品屬性

在function.php添加此下面的代碼

/** 
    * WooCommerce: show all product attributes listed below each item on Cart page 
    */ 
    function sm_woo_cart_attributes($cart_item, $cart_item_key){ 

     $item_data = $cart_item_key['data']; 
     $attributes = $item_data->get_attributes(); 


     if (! $attributes) { 
      return $cart_item; 
     } 

     $out = $cart_item . '<br />'; 

     foreach ($attributes as $attribute) { 

      if ($attribute['is_taxonomy']) { 

      // skip variations 
       if ($attribute['is_variation']) { 
        continue; 
       } 

       // backwards compatibility for attributes which are registered as taxonomies 

       $product_id = $item_data->id; 
       $terms = wp_get_post_terms($product_id, $attribute['name'], 'all'); 

       // get the taxonomy 
       $tax = $terms[0]->taxonomy; 

       // get the tax object 
       $tax_object = get_taxonomy($tax); 

       // get tax label 
       if (isset ($tax_object->labels->name)) { 
        $tax_label = $tax_object->labels->name; 
       } elseif (isset($tax_object->label)) { 
        $tax_label = $tax_object->label; 
       } 

       foreach ($terms as $term) { 
        $out .= $tax_label . ': '; 
        $out .= $term->name . '<br />'; 
       } 

      } else { 

       // not a taxonomy 

       $out .= $attribute['name'] . ': '; 
       $out .= $attribute['value'] . '<br />'; 
      } 
     } 
     echo $out; 
    } 

    add_filter('woocommerce_cart_item_name', sm_woo_cart_attributes, 10, 2); 
+0

不工作,woocomerce 3.0版 – Zygimantas

+0

試試這個插件https://wordpress.org/plugins/woocommerce-show-attributes/ –

相關問題