Skip to content

Instantly share code, notes, and snippets.

@lukecav
Created Sep 16, 2022
Embed
What would you like to do?
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