WordPress.org

Forums

WooCommerce - excelling eCommerce
[resolved] Stock qty on catalog pages -- variations show 0 in stock (3 posts)

  1. hpwtexas
    Member
    Posted 2 years ago #

    K so I got one for you php / hook experts here.
    I've written a function to show sku numbers and stock quantities for my catalog pages. Works great, except that variable products show 0 in stock. Eh. So, I rewrote it:

    // ----------------------DISPLAY SKUs AND QTY ON CATALOG PAGES
    function shop_inventory(){
    	global $product;
    	if($product->is_variation()) { echo
    	'<div itemprop="productID" id="multi-sku">Part No. <br/><span>' . $product->sku . '</span></div>';
    	}
    	else { echo
    	'<div itemprop="productID" id="multi-sku">Part No. <br/><span>' . $product->sku . '</span></div>
    	 <div class="stock"><span>' . $product->stock . '</span> in stock<small>(backorders allowed)</small></div>';
            }
    };
    add_action( 'woocommerce_after_shop_loop_item_title', 'shop_inventory' );

    But you can probably tell, that doesn't work. I know the culprit is line 4:
    if($product->is_variation()) {
    ..I just can't seem to work out how to check to see if the product is a variable product type.. I've also tried:
    if($product_type = variation) {
    .. I'm at a loss. So close too!! Any ideas?
    (Heres my original function, which doesn't check for variable products:)

    add_action( 'woocommerce_after_shop_loop_item_title', 'shop_inventory' );
    function shop_inventory(){
    global $product;
    echo '<div itemprop="productID" id="multi-sku">Part No. <br/><span>' . $product->sku . '</span><div class="stock"><span>' . $product->stock . '</span> in stock<small>(backorders allowed)</small></div></div>';
    }

    http://wordpress.org/extend/plugins/woocommerce/

  2. hpwtexas
    Member
    Posted 2 years ago #

    HUZZAH.
    I was VERY close! found woocommerce/templates/single-product/meta.php, and borrowed some code. Changed that pesky line 4 to:
    if ( $product->is_type( 'variable' )) {
    Works GREAT. Here's the final function (add to functions.php):

    // ----------------------DISPLAY SKUs AND QTY ON CATALOG PAGES
    function shop_inventory(){
    	global $product;
    	if ( $product->is_type( 'variable' )) { echo
    	'<div itemprop="productID" id="multi-sku">Part No. <br/><span>' . $product->sku . '</span></div>';
    	}
    	else { echo
    	'<div itemprop="productID" id="multi-sku">Part No. <br/><span>' . $product->sku . '</span></div>
    	 <div class="stock" ><span>' . $product->stock . '</span> in stock<small>(backorders allowed)</small></div>';
            }
    };
    add_action( 'woocommerce_after_shop_loop_item_title', 'shop_inventory' );

    Hope this helps somebody else too!

  3. queenb123
    Member
    Posted 1 year ago #

    Hi,

    I am new to all this, so please bear with me but I have a similar problem:

    I have managed to figure out a solution to the variation stock problem:

    in this example:

    http://www.brazilianweave.com/?product=indian-remy-deep-wave-2

    you can see that 12 inches is out of stock

    but 14 inches is in stock

    how ever 14 inches says: "instock (backorders allowed)"

    this will be confusing for my customers, where do i edit the text "instock (backorders allowed)" please?

    Been driving me crazy for weeks!

    Many thanks.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WooCommerce - excelling eCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic