2016-07-14 22 views
1

Ich verwende dieses Stück Code in meiner functions.php Datei meiner Wordpress/WooCommerce Website:Anzeigen der 'Out-of-stock'-Zeichenfolge auf der Shop-Seite, aber nicht der Bestandsmenge.

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'); 

Dieser Code zeigt die ‚ausverkauft‘ Mitteilung auf der Shop-Seite, wo alle Produkte wie angezeigt dies:

enter image description here


Das Problem ist, dass es verursacht auch die verfügbaren Inventarnummer der Produkte neben pr oduct Titel wie folgt aus:

enter image description here

Ich will das nicht.

Meine Frage:
Wie ich meinen Code noch die 'out of stock' Vorankündigung geändert werden können angezeigt werden (wenn das Produkt ausverkauft ist) und nicht die Inventar Produktverfügbarkeit auf Shop-Seite (in der linken Seite von Add-to -Kartentaste)?

Vielen Dank im Voraus!

+0

Ich habe das erste Bild nur geändert, in dem ersten Bild sehen Sie, ich habe in blau eingekreist die ' vergriffenes "tag", das der Code implementiert. Ohne den Funktionscode wird dies nicht angezeigt. –

Antwort

2

Erläuterungen über Ihren Code:

  1. Im ersten Teil (wenn) anzeigte Menge Lager solange Produkt auf Lager war (auch using the working tricks of this answer und sogar für Shop-Seite)
  2. Teil zwei (sonst) wurden Anzeige 'ausverkauft' Zeichenkette und Entfernen Add-to-Cart-Taste (als das Produkt ausverkauft war).

Mit dem folgenden Code, jetzt haben wir:

  • einen zusätzlichen (if) Zustand mit !is_shop() der so langer Bestandsmenge zeigt als Produkt ist auf Lager und ist nicht im Shop Seite.
  • ein zusätzliches (else) für Shop-Seite, die aus der Funktion ohne Anzeige der Lagerbestandsmenge herauskommen.
  • und Ihre unberührte letzte else Zustand (wie vorher).

So, hier ist die voll funktionsfähige Lösung, je nach Wunsch:

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'); 
    } 
} 

Wenn Sie nur Bestandsmenge auf einzelne Produktseite anzuzeigen, und 'out of stock' nur in-Shop-Seite , dies wird Ihr Code sein:

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; 
} 

Nun, wenn Sie nur don‘ t wollen jede Lagermenge angezeigt werden, wird der Code wie folgt sein:

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'); 
    } 
} 

Und in diesem Fall, dass Sie alle Arbeits Tricks aus dieser Antwort verwenden:
Woocommerce - Remove the available product inventory number from the shop page


Alle Code ist getestet und funktioniert perfekt.

Der Code geht auf function.php Datei Ihres aktiven Kind Thema oder ein Thema ...