Cothurnes : Rediriger certains produits directement vers checkout sans passer par le panier.

Trouvé un code qui fait ça sur ce lien:

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

(détaillé et versionné ici )

The plugin adds a small checkbox to the product metabox, so you can specify which products should trigger the automatic skip to checkout.

/**
 * Plugin Name: WC Redirect to checkout
 * Plugin URI: http://stackoverflow.com/q/32962653/383847
 * Description: Redirect to checkout for certain products 
 * Version: 1.0.1
 * 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 $product instanceof WC_Product && wc_string_to_bool( $product->get_meta( '_redirect_to_checkout', true ) );
}

Le code fonctionne si on décoche l’option liée aux boutons AJAX d’ajout au panier.

Produit 1 : Abonnement spectacles

case ‘direct checkout’ non cochée

Produit 2 : Inscription Gratuite

case ‘direct checkout’ cochée