如何仅针对某些产品跳过 woocommerce 上的购物车页面?

How to skip cart page on woocomerce for certain products only?

我将其添加到我的 functions.php 文件中:

add_filter ('woocommerce_add_to_cart_redirect', 'woo_redirect_to_checkout');
function woo_redirect_to_checkout() {
$checkout_url = WC()->cart->get_checkout_url();
return $checkout_url;
}

但是现在,所有的产品都重新定向到结账。我只想在一个产品中使用这个选项。这是我可以向同一个文件管理器添加产品 ID 的方法吗?

谢谢!

您需要在产品刚添加到购物车时获取产品,然后检查是否要将此产品的购物车页面重定向到结帐页面。您需要更改下面代码中的 $desire_product = 'certain_product'; 行,它肯定会起作用。

add_filter( 'woocommerce_add_to_cart_redirect', 'woo_redirect_checkout' );

function woo_redirect_checkout() {
    global $woocommerce;
    $desire_product = 'certain_product';
    //Get product ID
    $product_id = (int) apply_filters( 'woocommerce_add_to_cart_product_id', $_POST['add-to-cart'] );

    //Check if current product is subscription
    if ( $product_id == $desire_product ){
        $checkout_url = $woocommerce->cart->get_checkout_url();
        return $checkout_url;
        exit;
    } else {
        $cart_url = $woocommerce->cart->get_cart_url();
        return $cart_url;
        exit;
    }
}

您还可以使用一些操作挂钩,例如:woocommerce_add_to_cart 将产品 ID 传递给回调函数:

add_action( 'woocommerce_add_to_cart', 'custom_add_to_cart', 10, 2 );

function custom_add_to_cart( $cart_item_key, $product_id ) {
    // replace 123 with a valid product id
    if( 123 == $product_id ) {
        wp_redirect( WC()->cart->get_checkout_url() );
        exit;   
    }
}

我为此写了一个小插件,在这里分享。该插件向产品元数据框添加了一个小复选框,因此您可以指定哪些产品应触发自动跳转到结帐。基本上使用与其他答案相同的 woocommerce_add_to_cart_redirect 过滤器,但提供管理后端选项来确定哪些产品触发重定向。

<?php
/**
 * Plugin Name: Redirect to checkout
 * Plugin URI: 
 * Description: redirect to checkout for certain products 
 * Version: 1.0
 * Author: Kathy Darling
 * Author URI: http://kathyisawesome.com
 * Requires at least: 3.8
 * Tested up to: 3.9
 *
 * Text Domain: kia-redirect-to-checkout
 * Domain Path: /languages/
 *
 */
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

/*
* Add text inputs to product metabox
*/
function kia_add_to_wc_metabox(){
    global $post;

    echo '<div class="options_group">';

    // Suggested Price
    echo woocommerce_wp_checkbox( array(
        'id' => '_redirect_to_checkout',
        'label' => __( 'Redirect to checkout', 'kia-redirect-to-checkout' ) ,
        'description' => __( 'When this item is added to the cart, re-direct the customer to checkout immediately.', 'kia-redirect-to-checkout' )
        )
    );

    echo '</div>';

}
add_action( 'woocommerce_product_options_general_product_data', 'kia_add_to_wc_metabox' );


/*
 * Save extra meta info
 */
function kia_process_wc_meta_box( $post_id, $post ) {

    if ( isset( $_POST['_redirect_to_checkout'] ) ) {
        update_post_meta( $post_id, '_redirect_to_checkout', 'yes' );
    } else {
        update_post_meta( $post_id, '_redirect_to_checkout', 'no' );
    }

}
add_action( 'woocommerce_process_product_meta', 'kia_process_wc_meta_box', 1, 2 );


/*
 * Redirect to checkout
 */
function kia_add_to_cart_redirect( $url ){

    // If product is one of our special types
    if ( is_numeric( $_REQUEST['add-to-cart'] ) && kia_maybe_redirect_cart( (int) $_REQUEST['add-to-cart'] ) ) {

        // Remove default cart message
        WC()->clear_messages();

        // Redirect to checkout
        $url = WC()->cart->get_checkout_url();
    }

    return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'kia_add_to_cart_redirect' );


/*
 * check if an item has custom field
 */
function kia_maybe_redirect_cart( $product_id ){

    if ( 'yes' == get_post_meta( $product_id, '_redirect_to_checkout', true ) ){
        return TRUE;
    } else {
        return false;
    }
}

正在更新 WooCommerce 3.0+

<?php
/**
 * Plugin Name: WC Redirect to checkout
 * Plugin URI: 
 * Description: Redirect to checkout for certain products 
 * Version: 1.0
 * Author: Kathy Darling
 * Author URI: http://kathyisawesome.com
 * Requires at least: 3.8
 * Tested up to: 3.9
 * WC requires at least: 3.1.0
 * WC tested up to: 4.0.1
 *
 * Text Domain: kia-redirect-to-checkout
 * Domain Path: /languages/
 *
 */
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

/**
 * Add text inputs to product metabox
 */
function kia_add_to_wc_metabox(){
    global $post;

    echo '<div class="options_group">';

    // Suggested Price
    echo woocommerce_wp_checkbox( array(
        'id' => '_redirect_to_checkout',
        'label' => __( 'Redirect to checkout', 'kia-redirect-to-checkout' ) ,
        'description' => __( 'When this item is added to the cart, re-direct the customer to checkout immediately.', 'kia-redirect-to-checkout' )
        )
    );

    echo '</div>';

}
add_action( 'woocommerce_product_options_general_product_data', 'kia_add_to_wc_metabox' );


/**
 * Save extra meta info
 *
 * @param  WC_Product  $product
 */
function kia_process_wc_meta_box( $product ) {

    if ( isset( $_POST['_redirect_to_checkout'] ) ) {
        $product->update_meta_data( '_redirect_to_checkout', 'yes' );
    } else {
        $product->update_meta_data( '_redirect_to_checkout', 'no' );
    }

}
add_action( 'woocommerce_admin_process_product_object', 'kia_process_wc_meta_box' );


/**
 * Redirect to checkout
 *
 * @param  WC_Product  $product
 */
function kia_add_to_cart_redirect( $url, $product ) {

    // If product is one of our special products.
    if ( kia_maybe_redirect_cart( $product ) ) {

        // Remove default cart message.
        wc_clear_notices();

        // Redirect to checkout.
        $url = wc_get_checkout_url();
    }

    return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'kia_add_to_cart_redirect', 10, 2 );


/**
 * Check if an item has custom field.
 *
 * @param  WC_Product  $product
 */
function kia_maybe_redirect_cart( $product ) {
    return wc_string_to_bool( $product instanceof WC_Product && $product->get_meta( '_redirect_to_checkout', true ) );
}

https://gist.github.com/helgatheviking/f76b97d7d19813538e32b8f5f2dae6ec