简体   繁体   中英

Showing and hiding out of stock variations woocommerce

We have a unique situation whereby we need to show all variations of a product even if they are out of stock - this is because they are not technically out of stock - just not purchasable online. However, when it comes to purchasing, the dropdown obviously should not show the variations that we have set to out of stock, because they cannot be purchased online.

Allowing them to be selected anyway, then an out os stock message being shown is very clunky and confusing for the user.

Is there a woocommerce function, perhaps to get all variations rather than the default get_available_variations() ?

Currently we have Hide out of stock items from the catalog selected, so that they do not appear in the purchase dropdown , and then the following for where we need to display the info anyway: content-single-product.php (child theme)

<?php

     global $product;

     $variations = $product->get_available_variations();


        if (!empty($variations)){

            foreach ($variations as $variation) {
                $id = $variation['variation_id'];
                echo $variation['attributes']['attribute_pa_delivery-method'] . '<br>';
                echo $variation['duration'] . '<br>';
                echo $variation['capacity'];

            }

        }

?> 

The other option of course, is to uncheck 'Hide out of stock items from the catalog', and somehow prevent the variation from appearing in the purchase dropdown instead.

Thanks in advance.

you can simply disable the "out of stock" using this snippet

add_filter( 'woocommerce_variation_is_active', 'grey_out_variations_when_out_of_stock', 10, 2 );
function grey_out_variations_when_out_of_stock( $grey_out, $variation ){
if ( ! $variation->is_in_stock() ){
  return false;
 }else{
  return true;
 }
}

( source )

then simply hide it with this CSS code

.variations option:disabled {
  display:none;
}

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM