I'm working on a shop with several categories, most of which are held on-site, but there's a category where the items are then sourced upon request. For this category (and children), I'd like to output some text directly below the 'short_description' asking that the client contact us for availability prior to purchase.
It is important that it only shows in that category and its children categories, in this case 'grow-shop'.
I've attempted to use some other code found on stackoverflow to get text to be output for a specific category.
<div class="woocommerce-product-details__short-description">
<?php echo $short_description; // WPCS: XSS ok. ?>
<?php global $product;
if( has_term( 'grow-shop', 'product_cat', $product->id ) ) {
echo "Please note: This item is not held in stock. Please contact us for availability before purchase.";
} ?>
</div>
So far, I'm not able to achieve any accurate output. Echoing text is fine, but I don't have the right query to determine if it falls within the grow-shop category and its subcategories I feel.
You need to modified code as below.
// If a product in the 'grow-shop' category is being viewed...
if ( is_product() && has_term( 'grow-shop', 'product_cat' ) ) {
echo "Please note: This item is not held in stock. Please contact us for
availability before purchase.";
}
// for multiple categories
if(is_product_category( array( 'grow-shop', 'reflectors', 'nutrients' )))
{
echo "Please note: This item is not held in stock. Please contact us for
availability before purchase.";
}
// for apply same condition as or concept
if( is_product_category( 'category1-slug' ) || is_product_category(
'category2-slug' ) )
{
echo "Please note: This item is not held in stock. Please contact us
for availability before purchase.";
}
Just add the follows code snippet -
function woocommerce_short_description( $excerpt ){
global $post;
$cats_slugs = wp_get_post_terms( $post->ID, 'product_cat', array( 'fields' => 'slugs' ) );
if( in_array( 'grow-shop', $cats_slugs ) ){
$excerpt .= " Please note: This item is not held in stock. Please contact us for availability before purchase.";
}
return $excerpt;
}
add_filter( 'woocommerce_short_description', 'woocommerce_short_description', 99 );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.