Erläuterungen über Ihren Code:
- 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)
- 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 ...
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. –