netzwerkstatt
netzwerkstatt

Reputation: 33

Change WooCommerce availability text for specific post authors

I would like to change the woocommerce availability text only for specific post authors.

I have already this snippet but need to have the post author condition set. On a product page which was created by author ID 3 a specific availability text should be displayed.

/**
 * 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;
}

Upvotes: 1

Views: 137

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253949

Use the following to change WooCommerce availability text for a specific post author:

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;
}

For multiple post authors, you will use in_array() as follow:

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;
}

It should work.

Upvotes: 1

Related Questions