修改指定WooCommerce商品(product_id=13528)的加入购物车按钮为立即购买并跳转结账
Got it, let's tackle this specific requirement for your WooCommerce product (ID 13528) — changing the add-to-cart button to "Buy Now" and skipping the cart entirely to jump straight to checkout. Here's how to implement this cleanly without disrupting other products on your site:
1. Change the Button Text from "Add to Cart" to "Buy Now"
We'll use a WooCommerce filter to target only the product with ID 13528. Add this code to your child theme's functions.php file, or a custom plugin (using a child theme is safer to avoid losing changes when your parent theme updates):
// Swap "Add to Cart" with "Buy Now" for product ID 13528 add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_buy_now_button_label', 10, 2 ); function custom_buy_now_button_label( $default_text, $product ) { // Check if we're on the target product if ( $product->get_id() === 13528 ) { $default_text = __( 'Buy Now', 'woocommerce' ); } return $default_text; }
2. Skip Cart and Redirect Directly to Checkout
Next, we'll modify the add-to-cart behavior so clicking "Buy Now" sends the user straight to checkout instead of the cart page. Add this code right after the previous snippet:
// Redirect product 13528 to checkout immediately after adding to cart add_filter( 'woocommerce_add_to_cart_redirect', 'custom_buy_now_redirect_to_checkout' ); function custom_buy_now_redirect_to_checkout( $default_redirect_url ) { // Grab the product ID from the add-to-cart request $target_product_id = isset( $_REQUEST['add-to-cart'] ) ? absint( $_REQUEST['add-to-cart'] ) : 0; // If it's our target product, redirect to checkout if ( $target_product_id === 13528 ) { $default_redirect_url = wc_get_checkout_url(); } return $default_redirect_url; }
Quick Notes:
- If your product is a variable product (has multiple options like size/color), this code will still work, but make sure customers select an option before clicking "Buy Now" (WooCommerce handles this validation by default).
- Always test this after adding the code: clear your site and browser cache, then visit product 13528 to verify the button text and checkout redirect work as expected.
内容的提问来源于stack exchange,提问作者Kels Ivi

