【问题标题】:Change WooCommerce "out of stock" message based on category in OceanWP根据 OceanWP 中的类别更改 WooCommerce“缺货”消息
【发布时间】:2022-01-01 10:13:36
【问题描述】:

我想仅在单个产品页面和商店存档页面上更改 WooCommerce 中某个类别的缺货消息。

我正在使用 OceanWP 主题

这是我目前所拥有的,它有效,但我需要为类别添加“if”语句。

/** 
*This changes the out of stock text on the item in oceanwp theme product gallery  
*/ 
function my_woo_outofstock_text( $text ) {
    $text = __( 'Sold', 'oceanwp' );
    return $text;
}
add_filter( 'ocean_woo_outofstock_text', 'my_woo_outofstock_text', 20 );

这是我的代码尝试,基于此similar question here,但它仅适用于单个产品页面。有什么建议吗?

function my_woo_outofstock_text( $text, $product ) {
    $specific_categories = array( 'original-paintings' );
    
    if ( ! $product->is_in_stock() && has_term( $specific_categories, 'product_cat', $product->get_id() ) ) {
         $text = __( 'Sold', 'oceanwp' );
    }
    else {
        $text = __( 'Unavailable', 'oceanwp' );
    }        
    
    return $text;
}
add_filter( 'ocean_woo_outofstock_text', 'my_woo_outofstock_text', 20 );

【问题讨论】:

    标签: wordpress woocommerce product


    【解决方案1】:

    您使用的过滤器挂钩仅包含 1 个参数,因此 $product 不是其中的一部分。如果您仍想根据 $product 为商店存档页面应用更改,则应全局应用此设置。

    所以你得到:

    function my_woo_outofstock_text( $text ) {
        global $product;
        
        // Add categories. Multiple can be added, separated by a comma
        $specific_categories = array( 'original-paintings' );
        
        // Is a WC product
        if ( is_a( $product, 'WC_Product' ) ) {
            // NOT in stock & has term
            if ( ! $product->is_in_stock() && has_term( $specific_categories, 'product_cat', $product->get_id() ) ) {
                $text = __( 'Sold', 'oceanwp' );
            } else {
                $text = __( 'Unavailable', 'oceanwp' );
            }  
        }
        
        return $text;
    }
    add_filter( 'ocean_woo_outofstock_text', 'my_woo_outofstock_text', 20, 1 );
    

    单品页面可以使用Custom "Out of stock" text based on product category in WooCommerce回答码。

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 2014-08-29
    • 2018-08-23
    • 1970-01-01
    相关资源
    最近更新 更多