<?php function ft_show_stock_level() { global $product; $out_of_stock = filter_var( $atts['out_of_stock'], FILTER_VALIDATE_BOOLEAN ); if ( ! is_a( $product, 'WC_Product' ) ) { $product = wc_get_product( get_the_ID() ); } if ( ! $product ) { return ''; } if ( $product && $product->is_type( 'variable' ) ) { $available_variations = $product->get_available_variations(); $total_stock = 0; foreach ( $available_variations as $variation ) { $variation_obj = new WC_Product_Variation( $variation['variation_id'] ); if ( $variation_obj->is_in_stock() ) { $total_stock++; } } if ( $total_stock === 0 ) { return '<p class="font-bold">Producto agotado</p>'; } else { return ''; } } if ( $product && $product->is_in_stock() ) { return ''; } else { return '<p class="font-bold">Producto agotado</p>'; } return ''; } add_shortcode( 'stock_level', 'ft_show_stock_level' );
Snippet que te permite mostrar el mensaje de producto agotado cuando tienes marcado en el producto sin stock, funciona sin activar la gestión de stocks, marcando si hay o no stock. Funciona con productos simples y variables, en el loop o en la página de detalle.