2017-10-13 11 views
2

私はWoocommerceのOne-page-checkoutプラグインでこの問題の簡単な解決策を見つけるのに苦労しています。Woocommerceのカートアイテムに商品の説明を追加

私のクライアントは、商品アイテムの横に商品の説明をカートアイテムに追加します。
説明を表示するためにコードを操作する方法についてのご意見はありますか?

これは私が実際に持っているものです。

enter image description here

が、私はこのプラグインは、どこかの説明を隠すことだろうと思うだろうが、私はどこかのコードでそれを見つけることができません。

+0

現在、どのバージョンのWooCommerceをお使いですか? –

+0

私はバージョン4.8.2を使用しています –

+0

これは最新のWordpressバージョンですが、プラグインを更新した場合はおそらくWooCommerceバージョン3.2にあります。 –

答えて

0

あなたのテーマフォルダのルートにあるfunctions.phpでこのコードを試すことができます。私はまだWordpressの開発ではアクティブではないので、それがまだ動作するかどうかは分かりません。 [未テスト]

更新:それを行うには2通りの方法が(製品のための作業を行うことがあります

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

:これはおそらく、woocommerce_cart_item_nameフックを使用してWooCommerce 3+

のために働く必要があります製品バリエーション):

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ファイルになりますか。

...か... woocommerce_cart_item_nameフィルターフックに引っかけカスタム機能で

2):

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+でテストされ、動作します。

関連する問題