Displaying the 'out of stock' string on shop page but not the stock quantity.

T.Doe

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!

LoicTheAztec

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…

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get dates when quantity were out of stock

From Dev

Get dates when quantity were out of stock

From Dev

Hide out of stock - "pre_get_post" taxanomy for visibility on shop page

From Dev

Rails 4.0: Displaying stock

From Dev

Magento Product pag - displaying related products that are out of stock

From Dev

Magento Product pag - displaying related products that are out of stock

From Dev

Query for update stock quantity in transaction

From Java

Shopify, displaying price of first in stock product variant on Collection page

From Dev

Order out of stock products

From Dev

Caching plugins will affect the wordpress shop stock control?

From Dev

Stock system - SQL not showing out of stock items

From Dev

Change Woocommerce In Stock/Out Stock Text

From Dev

WooCommerce product variations: Auto enable Manage Stock and set stock quantity

From Dev

Use of "FOR UPDATE" during Magento stock quantity update

From Dev

Magento - get item stock/quantity per site

From Dev

Prevent 'Add to cart' If quantity Exceeds than stock

From Dev

echo woocommerce stock quantity of a particular product id

From Dev

Stock Quantity not Updated after Placing Order in magento

From Dev

How to Update Stock Quantity Manually in Magento

From Dev

How to get the stock quantity of an article from woocommerce?

From Dev

How get variation's stock quantity woocommerce

From Dev

Prevent 'Add to cart' If quantity Exceeds than stock

From Dev

cannot update quantity in stock on database php/mysql

From Dev

Prestashop - Cancelled order product quantity not increased in stock quantity

From Dev

Updating in stock quantity based on shopping cart quantity in rails app?

From Dev

Prestashop - Cancelled order product quantity not increased in stock quantity

From Java

Extracting specific string matches from a Stock Website page

From Dev

Hide 'out of stock' products in Woocommerce

From Dev

Google AdWords out of stock script

Related Related

  1. 1

    Get dates when quantity were out of stock

  2. 2

    Get dates when quantity were out of stock

  3. 3

    Hide out of stock - "pre_get_post" taxanomy for visibility on shop page

  4. 4

    Rails 4.0: Displaying stock

  5. 5

    Magento Product pag - displaying related products that are out of stock

  6. 6

    Magento Product pag - displaying related products that are out of stock

  7. 7

    Query for update stock quantity in transaction

  8. 8

    Shopify, displaying price of first in stock product variant on Collection page

  9. 9

    Order out of stock products

  10. 10

    Caching plugins will affect the wordpress shop stock control?

  11. 11

    Stock system - SQL not showing out of stock items

  12. 12

    Change Woocommerce In Stock/Out Stock Text

  13. 13

    WooCommerce product variations: Auto enable Manage Stock and set stock quantity

  14. 14

    Use of "FOR UPDATE" during Magento stock quantity update

  15. 15

    Magento - get item stock/quantity per site

  16. 16

    Prevent 'Add to cart' If quantity Exceeds than stock

  17. 17

    echo woocommerce stock quantity of a particular product id

  18. 18

    Stock Quantity not Updated after Placing Order in magento

  19. 19

    How to Update Stock Quantity Manually in Magento

  20. 20

    How to get the stock quantity of an article from woocommerce?

  21. 21

    How get variation's stock quantity woocommerce

  22. 22

    Prevent 'Add to cart' If quantity Exceeds than stock

  23. 23

    cannot update quantity in stock on database php/mysql

  24. 24

    Prestashop - Cancelled order product quantity not increased in stock quantity

  25. 25

    Updating in stock quantity based on shopping cart quantity in rails app?

  26. 26

    Prestashop - Cancelled order product quantity not increased in stock quantity

  27. 27

    Extracting specific string matches from a Stock Website page

  28. 28

    Hide 'out of stock' products in Woocommerce

  29. 29

    Google AdWords out of stock script

HotTag

Archive