functions - add_filter to specific WooCommerce Category

I 'm trying to show stock availibility for a WooCommerce category 'workshops' in this case. The code her

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
Add a comment  | 

1 Answer 1

Reset to default 1

To 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745288685a4620723.html

相关推荐

  • functions - add_filter to specific WooCommerce Category

    I 'm trying to show stock availibility for a WooCommerce category 'workshops' in this case. The code her

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信