2017-11-04 2 views
0

私のWooCommerce製品は、必要な方法でJet.comに商品データを提供するように設定されています。それは次のようなものです:WooCommerceでフィルターを使用して関連商品名を変更する

Product Name - A bunch of attributes - A bunch of other descriptors - Etc. 

しかし私は、製品の長いフォームのタイトルがJet.comのやり方と同じようにウェブサイトに表示されることを望みません。

したがって、woocommerce_cart_item_nameフィルタを使用して、商品の最初のメイン名を取得するコードを作成しました。Product Nameとチェックアウト時にカートに表示されます(これはカート/チェックアウトのページです)。

私が理解できないことは、カート/チェックアウトページに表示される関連の製品(アップセルとクロスセル)に関する同じことを行う方法です。 WooCommerceのドキュメントには、woocommerce_related_item_nameのような特定のフィルタはありません。

カートで行うのと同じ方法で、関連する商品のテキストを変更するにはどうすればよいですか?

詳細については、upsell/cross-sellで同じことをしたい作業カート名の切り捨てコードをここに示します。

// Display only short product name on website, leaving long product name for Jet.com to consume 
add_filter('woocommerce_cart_item_name', 'shorten_woo_product_title', 10, 2); 
    function shorten_woo_product_title($title, $cart_item, $cart_item_key) { 
     $_product = $cart_item['data'] ; 
     if (is_checkout() || is_shop()) { 
      $title = $_product->get_title(); 
      // Use as the product name the characters up to but not including the first dash character 
      $n = 1; // 1st dash 
      $pieces = explode(' - ', $title); // Break up the title into an array delimited by the "space dash space" characters 
      $shortname = implode(' - ', array_slice($pieces, 0, $n)); // Grab the short name in front of the first dash character 
      return $shortname; // Return it back 
     } else { 
      return $title; // Give the full product name 
     } 
} 

答えて

0

代わりにWordpressネイティブwp_titleフィルタを使用してください。ウェブサイトのすべてのタイトルに影響を与える可能性があります。つまり、product_type条件を追加するだけで商品に影響を与えることができます。

function change_product_titles($title, $id = null) { 
    $prod=get_post($id); 
    if (!empty($prod->ID) and $prod->post_type=='product') { 
     return $title.'blablabla'; 
    } 
    return $title; 
} 
add_filter('the_title', 'change_product_titles', 10, 2); 
+0

ありがとう@ Elvin85。私たちの場合、wp_titleを使用することはできませんでした。私たちの店は、WooCommerce製品のコンテンツを動的に挿入した製品ページを持つ従来のストア設定をポストとして使用していませんでした。それらは静的です。 WooCommerceの方法で製品名を動的に表示する唯一の場所はチェックアウトです。 get_post($ id)はそこには適用されないので、wp_titleを使うとカートのページは空白になります。 –

関連する問題