[英]Change add to cart button and text based on WooCommerce product type
How to change a WooCommerce add to cart button in Product List loop but depending on the product type, like for example:如何在产品列表循环中更改 WooCommerce 添加到购物车按钮,但取决于产品类型,例如:
I tried with below code but doesn't work:我尝试使用以下代码但不起作用:
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;
}
Try the following instead:请尝试以下操作:
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>';
}
Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 It should work
它应该工作
This doesnt seem to update anything for me.这似乎对我没有任何更新。 Ive added it in the child functions.php file but there was no change
我已将其添加到子函数中。php 文件但没有变化
I installed this plugin https://wordpress.org/plugins/button-customizer-for-woocommerce/ and added 2 filters to functions.php and finally, it works我安装了这个插件https://wordpress.org/plugins/button-customizer-for-woocommerce/并为函数添加了 2 个过滤器。php 最后,它可以工作了
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";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.