2016-07-14 20 views
1

我使用這段代碼在我functions.php我的WordPress/WooCommerce網站的文件:在商店頁面上顯示'缺貨'字符串,但不顯示庫存數量。

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

我只是改變了第一形象,第一形象,你可以看到我已經盤旋在藍色的「缺貨「代碼實現的標籤。沒有功能代碼,它不顯示這個。 –

回答

2

你的代碼說明:只要

  1. 在第一部分(如有)的顯示庫存量的產品是在股票(甚至using the working tricks of this answer,甚至店鋪頁面)
  2. 第二部分(別人)的顯示'缺貨'字符串和刪除添加到購物車按鈕(因爲產品缺貨)。

用下面的代碼,現在我們有:

  • !is_shop()一個額外的(如果)條件,其顯示庫存量,只要產品是是股票,而不是在店頁。
  • 附加(其他)爲商店頁面,即在不顯示庫存數量的情況下退出該功能。
  • 和你未觸動的最終其他條件(就像以前一樣)。

因此,這裏是全功能的解決方案,根據需要:

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


的所有代碼經過測試和完美運作。

代碼的推移function.php文件的活躍兒童主題或主題的...

相關問題