2017-08-19 19 views
0

私のウェブサイトには2つの製品があり、異なる商品をカートに追加する際に、異なるメッセージ(HTMLを使用したいというメッセージで)を表示します。今すぐ表示されますProduct successfully added to cart. 私の子供のfunction.phpでこのコードを使用していますが、私が正確に望むものを教えてくれません。それが今は非推奨だとWooCommerce 3 wc_add_to_cart_messagewc_add_to_cart_message_htmlに置き換えられているのでWooCommerce 3の製品IDに基づいてカートに追加メッセージをカスタマイズする

add_filter ('wc_add_to_cart_message', 'wc_add_to_cart_message_filter', 10, 2); 
function wc_add_to_cart_message_filter($message, $product_id = null) { 
    $titles[] = get_the_title($product_id); 

$titles = array_filter($titles); 
$added_text = sprintf(_n('%s has been successfully added to your Basket.', '%s have been added to your Basket.', sizeof($titles), 'woocommerce'), wc_format_list_of_items($titles)); 

$message = sprintf('%s <a href="%s" class="button">%s</a>', 
       esc_html($added_text), 
       esc_url(wc_get_page_permalink('cart')), 
       esc_html__('View Cart', 'woocommerce')); 

return $message; 
} 
+0

商品のいずれかがカートに追加された場合、「あなたの商品がカートに追加され、「カートを見る」」というメッセージが表示されます。カートにp1を追加すると「message1」が表示され、カートにp2を追加すると「message2」が表示されるような、別の製品を追加するためのさまざまなメッセージを表示したい私は今、それが意味をなさないことを願っています。 –

答えて

0

add_filter ('wc_add_to_cart_message_html', 'wc_add_to_cart_message_html_filter', 10, 2); 
function wc_add_to_cart_message_html_filter($message, $products) { 

    foreach($products as $product_id => $quantity){ 

     // (If needed) get the WC_Product object 
     $product = wc_get_product($product_id); 
     // The product title 
     $product_title = $product->get_title(); 

     // Set Here a product category Id, name or slug (for example, if needed) 
     $product_category = "Clothing"; 
     if(has_term('clothing', 'product_cat', $product_id)){ 
      return __("My custom message for product category \"$product_category\" for $product_title ($product_id)", "woocommerce"); 
     } 

     // Set HERE your first Product ID 
     if($product_id == 37){ 
      return __("My custom message 1 for $product_title ($product_id)", "woocommerce"); 
     } 
     // Set HERE your Second Product ID 
     elseif($product_id == 40){ 
      return __("My custom message 2 for $product_title ($product_id)", "woocommerce"); 
     } 
    } 
    return $message; 
} 

コードは、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイルになったりも中:正しい方法は、製品IDに基づいてカートのメッセージ(あるいは製品カテゴリ)に追加カスタマイズこの作業を取得するには任意のプラグインファイル。

WooCommerce 3+でテストされ、動作します。