• Resolved vagrantweb

    (@vagrantweb)


    FYI, I’ve discovered how to show out of stock variations, yet keep them visible without disabling universally and I’d like to share what I’ve discovered in case anyone could benifit. I didn’t write any of this code, I just found it and thought I’d share.

    Place this code in functions.php:

    function gwp_variation_is_active( $active, $variation ) {
    	if( ! $variation->is_in_stock() ) {
    		return false;
    	}
    
    	return $active;
    }
    
    add_filter( 'woocommerce_variation_is_active', 'gwp_variation_is_active', 10, 2 );

    Then place this code in functions.php to show an “Out of Stock” notice on shop/archive pages:

    // display an 'Out of Stock' label on archive pages
    add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_stock', 10 );
    function woocommerce_template_loop_stock() {
        global $product;
        if ( ! $product->managing_stock() && ! $product->is_in_stock() )
            echo '<p class="stock out-of-stock">Out of Stock</p>';
    }
    
    add_action( 'woocommerce_before_shop_loop_item_title', 'bbloomer_display_sold_out_loop_woocommerce' );
     
    function bbloomer_display_sold_out_loop_woocommerce() {
        global $product;
        if ( ! $product->is_in_stock() ) {
            echo '<span class="soldout">Out of Stock</span>';
        }
    }

    If you like, you can add this css to make it a “label”:

    .soldout {
    padding: 3px 8px;
    text-align: center;
    background: #222;
    color: white;
    font-weight: bold;
    position: absolute;
    top: 6px;
    right: 6px;
    font-size: 12px;
    }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Showing out of stock variations without disabling sitewide’ is closed to new replies.