Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Woo Redirect Certain Products to Checkout- How to skip cart page on woocomerce for certain products only?
<?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.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 ) );
}
@pintolog
Copy link

pintolog commented Jun 14, 2020

Hi,
I installed your plugin but it's not effecting the product i marked,
When i clicked on "add to cart" it's not redirect me to the checkout.
Any idea why?

@helgatheviking
Copy link
Author

helgatheviking commented Jun 18, 2020

Hi @pintolog Just tested this and it's working here with WC4.2. I'd make sure that you don't have any conflicts coming from your theme or other plugins.

@benrenhuldt
Copy link

benrenhuldt commented Sep 23, 2020

I dont believe this works for Woocommerce 4.5.1 / WP 5.1.1

@helgatheviking
Copy link
Author

helgatheviking commented Sep 23, 2020

@benrenhuldt woo is currently on version 4.5.2... did you mean something else? Also I've seen in recent work that the redirect doesn't work if your site is adding the product to the cart via ajax... Which is a deviation from default woo.

@benrenhuldt
Copy link

benrenhuldt commented Sep 23, 2020

@helgatheviking yes you are right, I mixed that up with the WP version. I just tried deactivating ajax in the product settings and that doesnt seem to help. Basically with the above code all subscription products skip the checkout directly, although it was working as expected before I did the update.

@helgatheviking
Copy link
Author

helgatheviking commented Sep 24, 2020

@benrenhuldt Subscriptions have their own redirect.. and if you don't have "mixed checkout" enabled... see this setting:

image

then all subscriptions will redirect the cart.... regardless of what you are doing with this plugin. But with mixed checkout enabled, I see an issue with products redirecting when the box is unchecked.... looks like it's bad order of operations and wc_string_to_bool() always returning true when it should return false. I'm going to make an edit now, so please test the update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment