[英]Change “add to cart” button text for products that have "no price" and "out of stock"
I want to change the "add to cart" text button for products that meet the following conditions:我想为满足以下条件的产品更改“添加到购物车”文本按钮:
The intention is to change the text to "Not available right now"目的是将文本更改为“目前不可用”
Here is an example image to clarify my question这是一个示例图像来澄清我的问题
This is the code that I use.这是我使用的代码。 But I can't make it work.
但我不能让它工作。 Any ideas of where I am going wrong?
关于我哪里出错的任何想法?
add_filter( 'woocommerce_after_shop_loop_item', 'price_zero_empty', 9999, 2 );
function price_zero_empty( $price, $product ) {
global $product;
if ( $stock == 'outofstock' && '' == $product->get_price() || 0 == $product->get_price() ) {
add_filter( 'gettext', 'change_readmore_text', 20, 3 );
}
function change_readmore_text(){
if (! is_admin() && $domain === 'woocommerce' && $price === 0 && $translated_text === 'Read more') {
$translated_text = 'Not available right now';}
return $translated_text;
}
To change the text for products that have "no price" and are "out of stock", you can use the woocommerce_product_add_to_cart_text
filter hook.要更改“无价格”和“缺货”的产品的文本,您可以使用
woocommerce_product_add_to_cart_text
过滤器挂钩。
So you get:所以你得到:
function filter_woocommerce_product_add_to_cart_text( $add_to_cart_text, $product ) {
// Price empty & Product is out of stock
if ( empty ( $product->get_price() ) && $product->get_stock_status() == 'outofstock' ) {
$add_to_cart_text = __( 'Not available right now', 'woocommerce' );
}
return $add_to_cart_text;
}
add_filter( 'woocommerce_product_add_to_cart_text', 'filter_woocommerce_product_add_to_cart_text', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'filter_woocommerce_product_add_to_cart_text', 10, 2 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.