-
-
Save lukecav/f399f606aa2a7a3b39a0e0331bd09b35 to your computer and use it in GitHub Desktop.
Display stock status on variable products in shop for WooCommerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'woocommerce_after_shop_loop_item', 'wc_echo_stock_variations_loop' ); | |
function wc_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; | |
} | |
echo '<br/>' . implode( ', ', $attr_string ) . ': ' . $stock_string; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.businessbloomer.com/woocommerce-display-variations-name-stock-shop-page/