如何根据产品类型更改产品列表循环中的“添加到购物车”按钮,例如:
我尝试使用以下代码,但不起作用:
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
$button_text = __( "Out of stock", "woocommerce" );
return '<a class="view-product" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
if( ! $product->managing_stock() && ! $product->is_in_stock() ) {
return $button;
}
if( $product->is_type( 'variable' ) ) return $button;
}
请尝试以下操作:
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
// Out of stock products
if( ! $product->is_in_stock() ) {
$button_text = __( "Unavailable", "woocommerce" );
}
// Simple and Variable products
elseif( $product->is_type( 'simple' ) || $product->is_type( 'variable' ) ) {
$button_text = __( "Show product", "woocommerce" );
}
// Other product types
else {
$button_text = add_to_cart_text();
}
return '<a class="view-product button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
代码进入函数。活动子主题(或活动主题)的php文件。应该行得通
要更改woocommerce Book Now按钮,我安装了这个插件https://wordpress.org/plugins/button-customizer-for-woocommerce/并在函数中添加了2个过滤器。最后,它工作了
add_filter( 'woocommerce_booking_single_check_availability_text', 'wooninja_booking_check_availability_text' );
function wooninja_booking_check_availability_text() {
return "your text";
}
add_filter( 'woocommerce_booking_single_add_to_cart_text', 'wooninja_woocommerce_booking_single_add_to_cart_text' );
function wooninja_woocommerce_booking_single_add_to_cart_text() {
return "your text";
}