Reputation: 69
I have set up a Woocommerce attribute for delivery time and am displaying it on the single product page with this:
//Single Product
function product_attribute_delivery(){
global $product;
$taxonomy = 'pa_delivery';
$value = $product->get_attribute( $taxonomy );
if ( $value ) {
$label = get_taxonomy( $taxonomy )->labels->singular_name;
echo '<p>' . $label . ': ' . $value . '</p>';
}
}
add_action( 'woocommerce_single_product_summary', 'product_attribute_delivery', 25 );
Would it be possible to only display it when the product is in stock?
Upvotes: 1
Views: 695
Reputation: 253919
It is possible using WC_Product
is_in_stock()
method:
add_action( 'woocommerce_single_product_summary', 'product_attribute_delivery', 25 );
function product_attribute_delivery(){
global $product;
$taxonomy = 'pa_delivery';
$value = $product->get_attribute( $taxonomy );
if ( $value && $product->is_in_stock() ) {
$label = get_taxonomy( $taxonomy )->labels->singular_name;
echo '<p>' . $label . ': ' . $value . '</p>';
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and work
Upvotes: 2