【问题标题】:Change WooCommerce availability text for specific post authors更改特定帖子作者的 WooCommerce 可用性文本
【发布时间】:2020-09-13 08:59:59
【问题描述】:

我想仅为特定帖子作者更改 woocommerce 可用性文本。

我已经有了这个 sn-p 但需要设置 post author 条件。在作者 ID 3 创建的产品页面上,应显示特定的可用性文本。

/**
 * Code snippet to change WooCommerce In Stock text 
 */ 

add_filter( 'woocommerce_get_availability', 'change_in_stock_text', 1, 2);

function change_in_stock_text( $availability, $_product) {
    
    // Change In Stock Text
    if ( $_product->is_in_stock() || $post->post_author == '3') {
                $availability['availability'] = sprintf( __('%s an Lager', 'woocommerce'), $_product->get_stock_quantity() );
    }
   
    return $availability;
}

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce author


    【解决方案1】:

    使用以下内容更改特定帖子作者的 WooCommerce 可用性文本:

    add_filter( 'woocommerce_get_availability', 'change_in_stock_text', 1, 2);
    function change_in_stock_text( $availability, $product ) {
        global $post;
    
        if ( ! is_a( $post, 'WP_Post' ) ) {
            $post = get_post( $product->get_id() );
        }
        
        // Change In Stock Text for a specific post author
        if ( $product->is_in_stock() && $post->post_author == '3') {
            $availability['availability'] = sprintf( __('%s an Lager', 'woocommerce'), $product->get_stock_quantity() );
        }
       
        return $availability;
    }
    

    对于多个帖子作者,您将使用in_array(),如下所示:

    add_filter( 'woocommerce_get_availability', 'change_in_stock_text', 1, 2);
    function change_in_stock_text( $availability, $product ) {
        global $post;
    
        if ( ! is_a( $post, 'WP_Post' ) ) {
            $post = get_post( $product->get_id() );
        }
        
        // Change In Stock Text for specifics post authors
        if ( $product->is_in_stock() && in_array( $post->post_author, array('3' ,'5') ) ) {
            $availability['availability'] = sprintf( __('%s an Lager', 'woocommerce'), $product->get_stock_quantity() );
        }
       
        return $availability;
    }
    

    它应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 2019-03-29
      相关资源
      最近更新 更多