Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I added this to my functions.php file :

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;
}

But now, all the products are re-directing strait to check-out. I would like to have this option only in one product. Is that a way I can add a product ID to that same filer?

Thank you!

like image 436
Marcio Avatar asked Oct 06 '15 05:10

Marcio


People also ask

How do I skip the cart page in WooCommerce?

Go to WooCommerce > Products settings. Under the General section, you need to configure Add to cart behavior settings. Tick the first checkbox to redirect to the cart page after successful addition of product(s) as shown in the screenshot below. Save the settings.

How do I redirect add to cart in WooCommerce?

You can find the option in the WooCommerce -> Settings -> click on the Products tab. When the option “Redirect to the cart page after successful addition” is checked, it will redirect all users to the cart after adding a product to the cart. If unchecked, the page will not get redirected.

How do I disable cart page?

Upload the entire disable-cart-page-for-woocommerce folder to the /wp-content/plugins/ directory, or install the plugin through the WordPress plugins screen directly. Activate the plugin through the Plugins menu in WordPress. Go to settings tab, under WooCommerce settings page, and enable the plugin functionality.


3 Answers

You need to get the product when It is just added to cart , then check if for this product you want to redirect the cart page to checkout page . You need to change $desire_product = 'certain_product'; line on below code and It will definitely work.

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;
    }
}
like image 143
Prafulla Kumar Sahu Avatar answered Oct 29 '22 05:10

Prafulla Kumar Sahu


I wrote a little plugin for this, sharing it here. The plugin adds a small checkbox to the product metabox, so you can specify which products should trigger the automatic skip to checkout. Basically using the same woocommerce_add_to_cart_redirect filter as in the other answers, but providing the admin backend option to determine which products trigger the redirection.

<?php
/**
 * Plugin Name: Redirect to checkout
 * Plugin URI: http://stackoverflow.com/q/32962653/383847
 * 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;
    }
}

Updating WooCommerce 3.0+

<?php
/**
 * Plugin Name: WC Redirect to checkout
 * Plugin URI: http://stackoverflow.com/q/32962653/383847
 * 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

like image 24
helgatheviking Avatar answered Oct 29 '22 05:10

helgatheviking


There are a few action hooks as well that you can use, for eg: woocommerce_add_to_cart which passes the product id to the callback function:

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;   
    }
}
like image 40
Anand Shah Avatar answered Oct 29 '22 04:10

Anand Shah