Skip to content

Instantly share code, notes, and snippets.

@lukecav
Created September 16, 2022 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukecav/f399f606aa2a7a3b39a0e0331bd09b35 to your computer and use it in GitHub Desktop.
Save lukecav/f399f606aa2a7a3b39a0e0331bd09b35 to your computer and use it in GitHub Desktop.
Display stock status on variable products in shop for WooCommerce
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