Displaying the 'out of stock' string on sh

2019-08-13 23:03发布

I'm using this piece of code in my functions.php file of my WordPress/WooCommerce site:

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

This code displays the 'out of stock' notice on the shop page where all products are displayed like this:

enter image description here


The issue is that it also causes of the available inventory number of the products next to product titles like this:

enter image description here

I don't want this.

My question:
How I can change my code to still display the 'out of stock' notice (when product is out of stock) and not the inventory product availability on shop page (in left side of add-to-cart button)?

Thanks in advance!

1条回答
Bombasti
2楼-- · 2019-08-13 23:31

Explanations about your code:

  1. In first part (if) was displaying stock quantity as long as product was in stock (even using the working tricks of this answer and even for shop page)
  2. Part two (else) was displaying 'out of stock' string and removing add-to-cart button (as the product was out stock).

With the code below, now we have:

  • an additional (if) condition with !is_shop() which displays stock quantity as long as product is is stock and is not in shop page.
  • an additional (else) for shop page, that get out of the function without displaying stock quantity.
  • and your untouched final else condition (just as before).

So here is the fully functional solution, as desired:

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

If you just want to display stock quantity on single product page, and 'out of stock' only in shop page, this will be your code:

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

Now if you just don't want to display any stock quantity, your code will be like this:

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

And in this case you could use all the working tricks from this answer:
Woocommerce - Remove the available product inventory number from the shop page


All code is tested and works perfectly.

The code goes on function.php file of your active child theme or theme…

查看更多
登录 后发表回答