I 'm trying to show stock availibility for a WooCommerce category 'workshops' in this case. The code here works, but works on everything in the store. How can I apply this to just the 'workshops' category?
<?php
add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $_product ) {
global $product;
// Change In Stock Text
if ( $_product->is_in_stock() ) {
$availability['availability'] = __('Available!', 'woocommerce');
}
// Change in Stock Text to only 1 or 2 left
if ( $_product->is_in_stock() && $product->get_stock_quantity() <= 4 ) {
$availability['availability'] = sprintf( __('Only %s left!', 'woocommerce'), $product->get_stock_quantity());
}
// Change Out of Stock Text
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __('Sorry, we sold out!', 'woocommerce');
}
return $availability;
}
I 'm trying to show stock availibility for a WooCommerce category 'workshops' in this case. The code here works, but works on everything in the store. How can I apply this to just the 'workshops' category?
<?php
add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $_product ) {
global $product;
// Change In Stock Text
if ( $_product->is_in_stock() ) {
$availability['availability'] = __('Available!', 'woocommerce');
}
// Change in Stock Text to only 1 or 2 left
if ( $_product->is_in_stock() && $product->get_stock_quantity() <= 4 ) {
$availability['availability'] = sprintf( __('Only %s left!', 'woocommerce'), $product->get_stock_quantity());
}
// Change Out of Stock Text
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __('Sorry, we sold out!', 'woocommerce');
}
return $availability;
}
Share
Improve this question
edited Jul 29, 2019 at 5:44
Jacob Peattie
44.2k10 gold badges50 silver badges64 bronze badges
asked Jul 29, 2019 at 5:30
OliverOliver
12 bronze badges
1 Answer
Reset to default 1To account for variations, I'd suggest using $product->get_category_ids()
and checking if your category is in there:
function wcs_custom_get_availability( $availability, $_product ) {
$workshops_category = get_term_by( 'slug', 'workshops', 'product_cat' );
$product_category_ids = $_product->get_category_ids();
if ( ! in_array( $workshops_category->term_id, $product_category_ids ) ) {
return $availability;
}
// Change In Stock Text
if ( $_product->is_in_stock() ) {
$availability['availability'] = __('Available!', 'woocommerce');
}
// Change in Stock Text to only 1 or 2 left
if ( $_product->is_in_stock() && $_product->get_stock_quantity() <= 4 ) {
$availability['availability'] = sprintf( __('Only %s left!', 'woocommerce'), $_product->get_stock_quantity());
}
// Change Out of Stock Text
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __('Sorry, we sold out!', 'woocommerce');
}
return $availability;
}
Also note that I removed the global $product;
variable. Make sure you're checking the same product object for all your conditions, and that it's the product that's passed to the filter callback.