问题描述
我只想更改特定文章作者的woocommerce可用性文本。
我已经有此代码段,但需要设置作者后的条件。在由作者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;
}
解决方法
使用以下内容更改特定帖子作者的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','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',$product->get_stock_quantity() );
}
return $availability;
}
应该可以。