2016-07-14 23 views
1

私は私のワードプレス/ WooCommerceサイトの私のfunctions.phpファイル内のコードのこの部分を使用しています:ショップページに「在庫切れ」の文字列を表示しますが、在庫数は表示しません。

function envy_stock_catalog() { 
    global $product; 
    if ($product->is_in_stock()) { 
     echo $product->get_stock_quantity() ; 
    } else { 
     echo '<div class="out-of-stock" >' . __('out of stock', 'envy') . '</div>'; 
       add_action('init','remove_loop_button'); 
    } 
} 
add_action('woocommerce_after_shop_loop_item_title', 'envy_stock_catalog'); 

このコードは、すべての製品が同じように表示されているお店のページで予告「在庫切れ」表示この:

enter image description here


問題は、それはまた、PRの隣の製品の利用可能在庫数の原因ということですこのようなoductタイトル:

enter image description here

は、私はこれを望んでいません。

私の質問:私はまだアドオンへの左側にショップページで在庫製品の可用性'out of stock'予告(製品が在庫切れの)表示しないように私のコードを変更するにはどうすればよい
-cartボタン)?

ありがとうございます!あなたのコードについて

+0

私は最初の画像で、あなたは「私は青い丸で囲まきた見ることができ、最初の画像を変更しました...あなたのアクティブな子テーマやテーマのfunction.phpファイルに行きますコードが実装している「在庫切れ」タグが含まれています。機能コードがなければ、これは表示されません。 –

答えて

2

説明:

  1. 最初の部分で(場合)は、(他の)パート2
  2. (でもusing the working tricks of this answer、さらにはショップページ用)限り、製品の在庫があったように在庫数をして表示しました。 '在庫切れ'文字列を表示し、カートに追加ボタンを削除します(商品が在庫切れだったため)。以下のコードで

、今、私たちは持っている:

  • 限り製品があるとして、在庫数量を表示する株式で、お店ではありません!is_shop()と追加(IF)条件ページ。
  • ショップページ用の追加の(他の)、在庫量を表示せずに機能を終了します。
  • とあなたの手の届かない最後のelse条件(前と同じ)。だからここ

は、必要に応じて、完全に機能するソリューションです:

add_action('woocommerce_after_shop_loop_item_title', 'envy_stock_catalog'); 
function envy_stock_catalog() { 
    global $product; 
    if ($product->is_in_stock()) { // If product is in stock 

     if (!is_shop()) { // If is NOT Shop page 

      // Displays the stock quantity 
      echo '<span class="qty">' . $product->get_stock_quantity() . '<span>'; 
     } else { // If is shop page (and product is in stock) 
      return; // Don't display stock quantity (and removes nothing). 
     } 
    // If product is NOT in stock 
    } else { 
     // Display 'out of stock' string 
     echo '<div class="out-of-stock" >' . __('out of stock', 'envy') . '</div>'; 

     // Removes "add to cart" button 
     add_action('init','remove_loop_button'); 
    } 
} 

あなただけの唯一のショップページに「在庫切れ」単一の製品ページ上の在庫数を表示する場合、およびあなただけのドンが」

add_action('woocommerce_after_shop_loop_item_title', 'envy_stock_catalog'); 
function envy_stock_catalog() { 
    global $product; 
    // If product is in stock but is not shop page 
    if ($product->is_in_stock() && !is_shop()) { 

     // Displays the stock quantity 
     echo '<span class="qty">' . $product->get_stock_quantity() . '<span>'; 
    // If product is NOT in stock and is shop page 
    } elseif (!$product->is_in_stock() && is_shop()) { 
     // Display 'out of stock' string 
     echo '<div class="out-of-stock" >' . __('out of stock', 'envy') . '</div>'; 

     // Removes "add to cart" button 
     add_action('init','remove_loop_button'); 
    } 
    // other cases goes out the function 
    return; 
} 

:今、これはあなたのコードになります

add_action('woocommerce_after_shop_loop_item_title', 'envy_stock_catalog'); 
function envy_stock_catalog() { 
    global $product; 
    if ($product->is_in_stock()) { // If product is in stock 
     return; // Don't display stock quantity (and removes nothing). 

    // If product is NOT in stock 
    } else { 
     // Display 'out of stock' string 
     echo '<div class="out-of-stock" >' . __('out of stock', 'envy') . '</div>'; 

     // Removes "add to cart" button 
     add_action('init','remove_loop_button'); 
    } 
} 

そして、この場合には、あなたがこの回答からすべての作業のトリックを使用することができます:
Woocommerce - Remove the available product inventory number from the shop page


すべてのコードを、tは任意の在庫数量を表示したい、あなたのコードは次のようになりますテストされ、完全に動作します。

コードが

関連する問題