2017-10-13 178 views
2

我正在努力尋找一個簡單的解決方案來解決這個問題,我正在使用Woocommerce的一頁結賬插件。將產品描述添加到Woocommerce的購物車項目

我的客戶希望在購物車商品的商品標題旁添加商品說明。
有關如何操作代碼以顯示說明的任何想法?

這是我反倒:

enter image description here

我認爲這個插件也只是躲在某處的描述,但我不能在代碼的任何地方找到它。

+0

您正在使用什麼版本的WooCommerce? –

+0

我使用的是版本4.8.2 –

+0

這是最新的WordPress版本,但如果您更新了插件,則可能是WooCommerce版本3.2。 –

回答

0

你可以在你的主題文件夾的根目錄下的functions.php中試試這段代碼。不知道它是否仍然有效,因爲我不再活躍於Wordpress開發。 [未經測試]

更新:可能應該爲WooCommerce 3+

工作中使用woocommerce_cart_item_name鉤:

add_filter('woocommerce_cart_item_name', 'cart_description', 20, 3); 
function cart_description($name, $cart_item, $cart_item_key) { 
    // Get the corresponding WC_Product 
    $product_item = $cart_item['data']; 

    if(!empty($product_item)) { 
     // WC 3+ compatibility 
     $description = $product_item->get_description(); 
     $result = __('Description: ', 'woocommerce') . $description; 
     return $name . '<br>' . $result; 
    } else 
     return $name; 
    } 
} 
+0

,似乎沒有工作..謝謝嘗試,雖然! –

+0

更新的答案可能適用。 –

0

有2種方式來做到這一點(做工作產品和產品變體):

1)自定義功能掛在woocommerce_get_item_data行動掛鉤(最佳的方法)

add_filter('woocommerce_get_item_data', 'customizing_cart_item_data', 10, 2); 
function customizing_cart_item_data($cart_data, $cart_item) { 

    $custom_items = array(); 
    $label = __('Description', 'woocommerce'); 

    // Get the product description 
    $description = $cart_item['data']->get_description(); 

    // For product variations when description is empty 
    if($cart_item['data']->is_type('variation') && empty($description)){ 
     // Get the parent variable product object 
     $product = wc_get_product($cart_item['data']->get_parent_id()); 
     // Get the variable product description 
     $description = $product->get_description(); 
    } 

    // If product or variation description exists we display it 
    if(! empty($description)){ 
     $custom_items[] = array(
      'key'  => $label, 
      'display' => $description, 
     ); 
    } 

    // Merging description and product variation attributes + values 
    if(! empty($cart_data)) $custom_items = array_merge($custom_items, $cart_data); 

    return $custom_items; 
} 

代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。

......或者......

2)隨着woocommerce_cart_item_name過濾鉤子鉤住自定義函數:

add_filter('woocommerce_cart_item_name', 'customizing_cart_item_data', 10, 3); 
function customizing_cart_item_data($item_name, $cart_item, $cart_item_key) { 
    // The label 
    $label = __('Description', 'woocommerce'); 

    // Get the product description 
    $description = $cart_item['data']->get_description(); 

    // For product variations when description is empty 
    if($cart_item['data']->is_type('variation') && empty($description)){ 
     // Get the parent variable product object 
     $product = wc_get_product($cart_item['data']->get_parent_id()); 
     // Get the variable product description 
     $description = $product->get_description(); 
    } 

    if(! empty($description)){ 
     $item_name .= '<p class="item-description" style="margin:12px 0 0;"> 
      <strong>'.$label.'</strong>: <br>'.$description.' 
     </p>'; 
    } 
    return $item_name; 
} 

代碼放在您的活動子主題的function.php文件(或主題)或者在任何插件文件中。

代碼在Woocommerce 3+上測試並正常工作。

相關問題