• Hello,
    I noticed that the availability info (in stock/not in stock) in wishlist-view table only works well for single product. If there is a variable product with some variations in stock and others out of stock, the table still says “in stock”. Is it possible to edit the code in order to list all possible variations with their specific stock lever, for example:

    Red T-Shirt
    Size S: in stock
    Size M: in stock
    Size L: not in stock

    That would be sooo great 🙂
    Any help would be really appreciated!

    • This topic was modified 9 years, 4 months ago by newshop.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter newshop

    (@newshop)

    There is this plugin WooCommerce Availability Chart that shows the available stock amount. Maybe this could help to create a code..

    Plugin Author YITHEMES

    (@yithemes)

    Hi newshop!

    You’re right!
    Please, try to follow the following steps to customize plugin

    1. Overwrite default wishlist-view.php template, copying it from wp-content/plugins/yith-woocommerce-wishlist/includes and pasting it under wp-content/themes/<your theme or child folder>/woocommerce/

    2. In your brand new template, replace the following code

    <?php
      if( $stock_status == 'out-of-stock' ) {
        $stock_status = "Out";
        echo '<span class="wishlist-out-of-stock">' . __( 'Out of Stock', 'yith-woocommerce-wishlist' ) . '</span>';
      } else {
        $stock_status = "In";
        echo '<span class="wishlist-in-stock">' . __( 'In Stock', 'yith-woocommerce-wishlist' ) . '</span>';
      }
    ?>

    (lines 189-197) with this code

    <?php
      if( $product->is_type( 'variable' ) ){
        $variations = $product->get_available_variations();
        if( ! empty( $variations ) ){
          foreach ( $variations as $variation ){
            $variation = $product->get_child( $variation['variation_id'] );
            echo $variation->get_formatted_variation_attributes();
            echo $variation->is_in_stock() ? '<span class="wishlist-out-of-stock">' . __( 'Out of Stock', 'yith-woocommerce-wishlist' ) . '</span>' : '<span class="wishlist-in-stock">' . __( 'In Stock', 'yith-woocommerce-wishlist' ) . '</span>';
          }
        }
      }
      elseif( $stock_status == 'out-of-stock' ) {
        $stock_status = "Out";
        echo '<span class="wishlist-out-of-stock">' . __( 'Out of Stock', 'yith-woocommerce-wishlist' ) . '</span>';
      } else {
        $stock_status = "In";
        echo '<span class="wishlist-in-stock">' . __( 'In Stock', 'yith-woocommerce-wishlist' ) . '</span>';
      }
    ?>

    Hope this helps
    Have a nice day

    Thread Starter newshop

    (@newshop)

    Thank you very much! First I thought everything works like a charm but then I noticed that the products in stock are marked as “not in stock” and vice versa. I think I have to switch a part of the code but I dont know how. Could you please help me with that?

    Thread Starter newshop

    (@newshop)

    Ok, I just switched the two spans and now its correct. Do you agree with my changes?

    Plugin Author YITHEMES

    (@yithemes)

    Yes, of course 🙂
    Sorry for the issue 😀

    I’m marking this topic as solved

    If you enjoy our plugin and our support, please, don’t forget to leave us a 5 star rating, and to consider buying our premium version of wishlist plugin 🙂

    Have a nice day

    Thread Starter newshop

    (@newshop)

    Awesome, thank you so much for the great support, 5star rating will follow!

    Thread Starter newshop

    (@newshop)

    Hello, I wanted to include the code you gave me in a shortcode and then I wanted to include that shortcode in a form that I created with contact form 7 and contact form 7 dynamic text extension. But it didnt work. The author of contact form 7 dynamic text extension gave me the following advice, but I dont know how to edit the code correctly. Could you please help me with that? Many thanks in advance!

    Your shortcode is not coded correctly and you need to fix it. You are echoing your output, not returning it.

    All shortcodes must return, not echo.

    Your $output variable that you are returning isn’t even defined, so you’re returning null. You need to define it at the beginning of your function, append your strings to that variable, and then return it. Don’t echo anything.

    Here is the code I used:

    function availability_info() {
    global $product;
    if( $product->is_type( 'variable' ) ){
    	$variations = $product->get_available_variations();
    	if( ! empty( $variations ) ){
    		foreach ( $variations as $variation ){
    			$variation = $product->get_child( $variation['variation_id'] );
    			echo $variation->get_formatted_variation_attributes() . (":") .(" "); echo $variation->is_in_stock() ? ("<span style=color:#00b805;>") . __( 'In Stock', 'yith-woocommerce-wishlist' ). ("</span>") . ("<br />") : ("<span style=color:#cb0000;>") . __( 'Out of Stock', 'yith-woocommerce-wishlist' ). ("</span>") . ("<br />");
    		}
    	}
    }
    elseif( $stock_status == 'out-of-stock' ) {
    	$stock_status = "Out";
    	echo __( 'Out of Stock', 'yith-woocommerce-wishlist' );
    } 
    else {
    	$stock_status = "In";
    	echo __( 'In Stock', 'yith-woocommerce-wishlist' );
    }							
    return $output;
    }
    add_shortcode( 'instock', 'availability_info' );
    • This reply was modified 9 years, 4 months ago by newshop.
Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Stock availability for each variation’ is closed to new replies.