简体   繁体   English

更改“无价”和“缺货”产品的“添加到购物车”按钮文本

[英]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:我想为满足以下条件的产品更改“添加到购物车”文本按钮:

  • empty price空价
  • out of stock缺货

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.

相关问题 在 WooCommerce 中产品缺货时更改单个添加到购物车文本 - Change single add to cart text when product is out of stock in WooCommerce 更改 Woocommerce 中特定产品的添加到购物车按钮文本 - Change Add to cart button text for specific products in Woocommerce WooCommerce - 更改 40 英镑或更多产品的“添加到购物车”按钮文本 - WooCommerce - Change Add to Cart button text for products £40 or more WooCommerce:更改多个产品的添加到购物车按钮文本,并重定向到结帐 - WooCommerce: Change add to cart button text for multiple products with a redirection to checkout 将显示的价格替换为缺货 WooCommerce 产品的文本 - Replace displayed price by a text for out of stock WooCommerce products 重命名WooCommerce 3中缺货产品的“添加到购物车”按钮 - Rename Add to Cart buttons for Out Of Stock Products in WooCommerce 3 如果库存状态为0,Woocommerce更改将添加到购物车文本 - Woocommerce change add to cart text if stock status is 0 将“添加到购物车”文本更改为产品价格 - Change “add to cart” text to the price of the product 更改缺货 Woocommerce 产品上的“阅读更多”按钮链接 - Change “Read More” button link on Out of stock Woocommerce products WooCommerce 积木:更改缺货产品上的阅读更多按钮 - WooCommerce Blocks: Change Read More Button on Out of Stock Products
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM