P粉8795174032023-08-17 11:41:37
The following will, on a single product page, replace the "Add to Cart" button with a custom text message if the cart is not empty:
// 添加到购物车替换文本消息 function add_to_cart_replacement(){ // 您的消息文本 $message_text = __( "在您可以将另一个产品添加到购物车之前,请完成购买或清空购物车。", "woocommerce" ); // 显示文本消息 echo '<p class="button message">' . $message_text . '</p>'; } // 用自定义文本消息替换单个产品的添加到购物车按钮 add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 ); function replace_single_add_to_cart_button() { global $product; // 如果购物车不为空 if( ! WC()->cart->is_empty() ){ // 对于变量产品类型(保留属性选择字段) if( $product->is_type( 'variable' ) ) { remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 ); add_action( 'woocommerce_single_variation', 'add_to_cart_replacement', 20 ); } // 对于其他所有产品类型 else { remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 ); add_action( 'woocommerce_single_product_summary', 'add_to_cart_replacement', 30 ); } } }
The code is placed in your child theme’s functions.php file (or in a plugin). Tested and available.
You will get results similar to: