Reputation: 329
I show the variations of the product on the shop page, but it comes in variants that are out of stock. How can I show only the variations that are in stock?
Following my code:
add_action( 'woocommerce_after_shop_loop_item_title', 'display_size_attribute', 5 );
function display_size_attribute() {
global $product;
if ( $product->is_type('variable') ) {
$taxonomy_size = 'pa_numara';
echo '<span class="attribute-size">' . $product->get_attribute($taxonomy_size) . '</span>';
}
}
Upvotes: 0
Views: 429
Reputation: 329
I found the solution like this, maybe I'm sharing it as someone else's need.
add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_echo_stock_variations_loop' );
function bbloomer_echo_stock_variations_loop(){
global $product;
if ( $product->get_type() == 'variable' ) {
foreach ( $product->get_available_variations() as $key ) {
$variation = wc_get_product( $key['variation_id'] );
$stock = $variation->get_availability();
$stock_string = $stock['availability'] ? $stock['availability'] : __( 'In stock', 'woocommerce' );
$attr_string = array();
foreach ( $key['attributes'] as $attr_name => $attr_value ) {
$attr_string[] = $attr_value;
}
if($key['max_qty'] >= 1 ){
echo '<div class="shop-attribute">'. implode( ', ', $attr_string ).'</div>';
}
else {
echo "";
}
}
}
}
Upvotes: 1