从特定产品中排除自定义 WooCommerce 可用性文本

Exclude Custom WooCommerce availability text from specific products

我正在使用以下 php 代码在 Woocommerce 产品页面上的产品可用性文本旁边添加自定义的全局消息(在 stock/Out 库存中)。

add_filter( 'woocommerce_get_availability_text', 'filter_product_availability_text', 10, 2 );
function filter_product_availability_text( $availability_text, $product ) {

    if( $product->is_in_stock() && $product->managing_stock() && 
    ! $product-> is_on_backorder( 1 ) && $product->get_stock_quantity() > 0 ) {
        $availability_text .= 'Free Shipping' . __("", "woocommerce");
    }
    return $availability_text;
}

“免费送货”消息目前显示在所有产品页面上。我如何才能排除特定产品显示该自定义可用性消息?

已更新

通过在您的 IF 语句中添加一组产品 ID 以排除如下:

&& ! in_array( $product->get_id(), array(37, 45, 46, 58) )

所以在你的代码中:

add_filter( 'woocommerce_get_availability_text', 'filter_product_availability_text', 10, 2 );
function filter_product_availability_text( $availability_text, $product ) {
    if( $product->is_in_stock() && $product->managing_stock() 
    && ! $product->is_on_backorder( 1 ) && $product->get_stock_quantity() > 0 
    && ! in_array( $product->get_id(), array(37, 45, 46, 58) ) ) {
        $availability_text .= ' ' . __("Free Shipping", "woocommerce");
    }
    return $availability_text;
}

应该可以。


处理可变产品

要处理可变产品,您可以替换代码行:

&& ! in_array( $product->get_id(), array(37, 45, 46, 58) ) ) {

通过以下代码行:

&& ! array_intersect( array($product->get_id(), $product->get_parent_id()), array(37, 45, 46, 58) ) ) {