如果您使用的是2.1,那么这应该适合您
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_archive_custom_cart_button_text' ); // 2.1 +
function woo_archive_custom_cart_button_text() {
return __( 'My Button Text', 'woocommerce' );
}
吴哥商业
add_filter( 'add_to_cart_text', 'woo_archive_custom_cart_button_text' ); // < 2.1
function woo_archive_custom_cart_button_text() {
return __( 'My Button Text', 'woocommerce' );
}
将其添加到子主题函数中。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_type = $product->product_type;
switch ( $product_type ) {
case 'external':
return __( 'Buy product', 'woocommerce' );
break;
case 'grouped':
return __( 'View products', 'woocommerce' );
break;
case 'simple':
return __( 'Add to cart', 'woocommerce' );
break;
case 'variable':
return __( 'Select options', 'woocommerce' );
break;
default:
return __( 'Read more', 'woocommerce' );
}
我修改了上面的内容来检查库存状态,并将其添加到购物车文本中。见下文:
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_archive_custom_cart_button_text' );
function woo_archive_custom_cart_button_text() {
global $product;
$product_sstatus = $product->get_stock_status();
switch ( $product_sstatus ) {
case 'instock':
return __( 'Add to Cart', 'woocommerce' );
break;
case 'outofstock':
return __( 'Sold Out', 'woocommerce' );
break;
case 'onbackorder':
return __( 'On Backorder', 'woocommerce' );
break;
default:
return __( 'More Info', 'woocommerce' );
}}