更改WooCommerce添加到购物车的特定标签的文本


Change WooCommerce add to cart text for specific tag

我正在寻找一个函数来更改 wooCommerce 按钮上的添加到购物车文本,但前提是相关产品具有特定标签。 即,如果产品具有"预购"标签,则按钮文本将更改为"立即预订"

全局

更改文本可以实现

;

http://docs.woothemes.com/document/change-add-to-cart-button-text/

谢谢。

您可以使用

has_term检查该特定术语。

    //For single product page
    add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' ); // 2.1 +
    function woo_custom_cart_button_text() {
        global $product;
        if ( has_term( 'preorder', 'product_cat', $product->ID ) ) :
            return __( 'Pre order Now!', 'woocommerce' );
        endif;
    }
    //For Archive page
    add_filter( 'woocommerce_product_add_to_cart_text', 'woo_archive_custom_cart_button_text' ); // 2.1 +
    function woo_archive_custom_cart_button_text() {
        if ( has_term( 'preorder', 'product_cat', $product->ID ) ) :
            return __( 'Pre order Now!', 'woocommerce' );
        else:
            return __( 'Add to Cart', 'woocommerce' );
        endif;
    }  

让我知道输出。

已经

有一段时间了,但请尝试以下方法:

<?php
add_filter( 'woocommerce_product_add_to_cart_text' , 'custom_woocommerce_product_add_to_cart_text' );
/**
* custom_woocommerce_template_loop_add_to_cart
*/
function custom_woocommerce_product_add_to_cart_text() {
global $product;
$product_tag = $product->product_tag;
switch ( $product_tag ) { 
  case 'preorder':
    return __( 'Pre order Now!', 'woocommerce' );
  break;
  default:
    return __( 'Add to cart', 'woocommerce' );
    }
}

基本上,您要做的是执行一个条件,在其中检查产品标签是否为"预购"。我不确定如果有多个标签,此确切代码是否有效。

编辑:您可能需要使用此过滤器在产品页面上更改它

add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' ); 

将其添加到您的子主题函数.php文件中

 add_filter( 'woocommerce_product_add_to_cart_text', 
 'woocommerce_add_to_cart_button_text_archives' );  
function woocommerce_add_to_cart_button_text_archives() {
return __( 'Add to Cart Button Text', 'woocommerce' );
您可以通过

functions.php中添加几行代码来修复它:

// Change the add to cart text on single product pages
add_filter( 'woocommerce_product_single_add_to_cart_text’, ‘woo_custom_cart_button_text' ); // 2.1 +
function woo_custom_cart_button_text() {
    return __( 'ADD TO CART', 'woocommerce' );
}
//Change the add to cart text on product archives
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_archive_custom_cart_button_text' ); // 2.1 +
function woo_archive_custom_cart_button_text() {
    return __( 'ADD TO CART', 'woocommerce' );
}

http://visupporti.com/woocommerce-how-to-change-add-to-cart-text/