如何在 WooCommerce 中检查一系列产品 ID [英] How to check for an array of product Ids in WooCommerce
本文介绍了如何在 WooCommerce 中检查一系列产品 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为特定产品更改 WooCommerce 商店页面上添加到购物车按钮的文本:
I am changing the text of the Add to cart button on shop pages in WooCommerce for specific products:
add_filter( 'woocommerce_product_add_to_cart_text', 'add_to_cart_text' );
function add_to_cart_text( $default ) {
if ( get_the_ID() == 1 ) {
return __( 'Text1', 'your-slug' );
} elseif ( get_the_ID() == 2 ) {
return __( 'Text2', 'your-slug' );
} elseif ( get_the_ID() == 3) {
return __( 'Text3', 'your-slug' );
} else {
return $default;
}
}
如果我有两个或多个 ID 需要具有相同的按钮文本,我该如何将这些 ID 的数组添加到我的函数中?
If I have two or more IDs that need to have the same button text, how can I add an array of these ids into my function?
推荐答案
您可以使用 in_array()
PHP 条件函数 如:
You can use in_array()
PHP conditional function like:
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_text', 10, 2 );
function custom_add_to_cart_text( $text, $product ) {
if ( in_array( $product->get_id(), array(1) ) ) {
$text = __( 'Text1', 'text-domain' );
} elseif ( in_array( $product->get_id(), array(2) ) ) {
$text = __( 'Text2', 'text-domain' );
} elseif ( in_array( $product->get_id(), array(3, 4) ) ) {
$text = __( 'Text3', 'text-domain' );
}
return $text;
}
它应该可以工作.
你也应该在你的钩子函数中使用 $product
缺少的参数.
Also you should use $product
missing arguments in your hooked function.
这篇关于如何在 WooCommerce 中检查一系列产品 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文