You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WooCommerce中跳过结账页面,直接跳转至支付网关?

Skip WooCommerce Checkout Page: Direct Jump to Payment Gateway

Got it, let's break this down—since all your checkout data is already auto-filled, skipping the checkout page's "Place Order" step and jumping straight to the payment gateway is totally feasible. Below are two practical methods tailored to different user flows, along with code you can drop in and tweak for your setup.

Method 1: Add a "Direct Pay" Button to the Cart Page

This lets users go from their cart straight to the payment gateway without ever hitting the checkout page.

Add this code to your child theme's functions.php file, or use a code snippet plugin like Code Snippets to avoid losing changes when your theme updates:

// Add Direct Pay button to cart page
add_action( 'woocommerce_proceed_to_checkout', 'add_direct_pay_button', 20 );
function add_direct_pay_button() {
    if ( ! WC()->cart->is_empty() ) {
        echo '<a href="' . esc_url( wc_get_checkout_url() . '?direct_pay=1' ) . '" class="button alt">' . __( 'Direct Pay', 'woocommerce' ) . '</a>';
    }
}

// Handle the direct payment request
add_action( 'template_redirect', 'handle_direct_pay' );
function handle_direct_pay() {
    if ( isset( $_GET['direct_pay'] ) && $_GET['direct_pay'] == '1' && is_checkout() && ! is_wc_endpoint_url() ) {
        // Get current user (adjust if you allow guest checkout)
        $user = wp_get_current_user();
        
        if ( ! $user->ID ) {
            wc_add_notice( __( 'Please log in to complete direct payment', 'woocommerce' ), 'error' );
            wp_redirect( wc_get_page_permalink( 'myaccount' ) );
            exit;
        }

        // Build order data using user's saved billing/shipping info
        $order_data = array(
            'status'        => 'pending',
            'customer_id'   => $user->ID,
            'customer_note' => '',
            'billing'       => array(
                'first_name' => $user->billing_first_name,
                'last_name'  => $user->billing_last_name,
                'address_1'  => $user->billing_address_1,
                'address_2'  => $user->billing_address_2,
                'city'       => $user->billing_city,
                'state'      => $user->billing_state,
                'postcode'   => $user->billing_postcode,
                'country'    => $user->billing_country,
                'email'      => $user->billing_email,
                'phone'      => $user->billing_phone,
            ),
            'shipping'      => array(
                'first_name' => $user->shipping_first_name,
                'last_name'  => $user->shipping_last_name,
                'address_1'  => $user->shipping_address_1,
                'address_2'  => $user->shipping_address_2,
                'city'       => $user->shipping_city,
                'state'      => $user->shipping_state,
                'postcode'   => $user->shipping_postcode,
                'country'    => $user->shipping_country,
            ),
        );

        // Create the order
        $order = wc_create_order( $order_data );

        // Add cart items to the order
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            $product = $cart_item['data'];
            $order->add_product( $product, $cart_item['quantity'], array(
                'variation' => $cart_item['variation'],
                'cart_item_key' => $cart_item_key,
            ) );
        }

        // Calculate order totals
        $order->calculate_totals();

        // Empty the cart
        WC()->cart->empty_cart();

        // Redirect to payment gateway
        $payment_gateways = WC()->payment_gateways->get_available_payment_gateways();
        if ( ! empty( $payment_gateways ) ) {
            $gateway = reset( $payment_gateways ); // Use first available gateway
            $payment_url = $gateway->get_checkout_payment_url( $order );
            wp_redirect( $payment_url );
            exit;
        } else {
            wc_add_notice( __( 'No available payment gateways', 'woocommerce' ), 'error' );
            wp_redirect( wc_get_checkout_url() );
            exit;
        }
    }
}

How this works:

  1. Adds a "Direct Pay" button next to the standard "Proceed to Checkout" button on the cart page.
  2. When clicked, it creates an order using the logged-in user's saved billing/shipping details (you can modify this to pull guest checkout data if needed).
  3. Automatically adds cart items to the order, calculates totals, empties the cart, and redirects straight to your active payment gateway.

Method 2: Add a "Buy & Pay Directly" Button to Single Product Pages

If you want users to skip both cart and checkout, add this button to individual product pages:

// Add Direct Buy button to single product pages
add_action( 'woocommerce_single_product_summary', 'add_single_product_direct_pay_button', 35 );
function add_single_product_direct_pay_button() {
    global $product;
    if ( $product->is_purchasable() && $product->is_in_stock() ) {
        $direct_pay_url = add_query_arg( 
            array( 'add-to-cart' => $product->get_id(), 'direct_pay' => '1' ), 
            wc_get_checkout_url() 
        );
        echo '<a href="' . esc_url( $direct_pay_url ) . '" class="button alt">' . __( 'Buy & Pay Directly', 'woocommerce' ) . '</a>';
    }
}

// Handle single product direct payment
add_action( 'template_redirect', 'handle_single_product_direct_pay' );
function handle_single_product_direct_pay() {
    if ( isset( $_GET['direct_pay'] ) && $_GET['direct_pay'] == '1' && isset( $_GET['add-to-cart'] ) && is_checkout() && ! is_wc_endpoint_url() ) {
        $product_id = intval( $_GET['add-to-cart'] );
        $product = wc_get_product( $product_id );
        
        // Validate product availability
        if ( ! $product || ! $product->is_purchasable() || ! $product->is_in_stock() ) {
            wc_add_notice( __( 'Product is unavailable or out of stock', 'woocommerce' ), 'error' );
            wp_redirect( get_permalink( $product_id ) );
            exit;
        }

        // Add product to cart
        WC()->cart->add_to_cart( $product_id );

        // Reuse the order creation logic from Method 1
        $user = wp_get_current_user();
        
        if ( ! $user->ID ) {
            wc_add_notice( __( 'Please log in to complete direct payment', 'woocommerce' ), 'error' );
            wp_redirect( wc_get_page_permalink( 'myaccount' ) );
            exit;
        }

        $order_data = array(
            'status'        => 'pending',
            'customer_id'   => $user->ID,
            'customer_note' => '',
            'billing'       => array(
                'first_name' => $user->billing_first_name,
                'last_name'  => $user->billing_last_name,
                'address_1'  => $user->billing_address_1,
                'address_2'  => $user->billing_address_2,
                'city'       => $user->billing_city,
                'state'      => $user->billing_state,
                'postcode'   => $user->billing_postcode,
                'country'    => $user->billing_country,
                'email'      => $user->billing_email,
                'phone'      => $user->billing_phone,
            ),
            'shipping'      => array(
                'first_name' => $user->shipping_first_name,
                'last_name'  => $user->shipping_last_name,
                'address_1'  => $user->shipping_address_1,
                'address_2'  => $user->shipping_address_2,
                'city'       => $user->shipping_city,
                'state'      => $user->shipping_state,
                'postcode'   => $user->shipping_postcode,
                'country'    => $user->shipping_country,
            ),
        );

        $order = wc_create_order( $order_data );

        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            $product = $cart_item['data'];
            $order->add_product( $product, $cart_item['quantity'], array(
                'variation' => $cart_item['variation'],
                'cart_item_key' => $cart_item_key,
            ) );
        }

        $order->calculate_totals();
        WC()->cart->empty_cart();

        // Redirect to payment gateway
        $payment_gateways = WC()->payment_gateways->get_available_payment_gateways();
        if ( ! empty( $payment_gateways ) ) {
            $gateway = reset( $payment_gateways );
            $payment_url = $gateway->get_checkout_payment_url( $order );
            wp_redirect( $payment_url );
            exit;
        } else {
            wc_add_notice( __( 'No available payment gateways', 'woocommerce' ), 'error' );
            wp_redirect( wc_get_checkout_url() );
            exit;
        }
    }
}

Key Notes & Adjustments

  • Guest Checkout: If your store allows guest payments, modify the user validation section to pull billing/shipping data from the WooCommerce session instead of user meta.
  • Payment Gateway Specifics: Some gateways require extra order metadata. Check your gateway's documentation and add any required fields to the $order_data array.
  • Testing: Always test this in a staging environment first to ensure orders are created correctly and payments process as expected.
  • Code Safety: Use a child theme or code snippet plugin instead of editing your parent theme's functions.php—this prevents code loss during theme updates.

内容的提问来源于stack exchange,提问作者MIRROR SHOW

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 14:47:47