15488

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!

Answer1:

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

Answer2:

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

Answer3:

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

Recommend

  • woocommerce hook delete product from the cart
  • How can i get price in woocommerce cart product title hook
  • Hooks around add to cart for Woocommerce
  • Get parent product thumbnail filter in WooCommerce Cart and Checkout Page
  • Set a custom calculated item price in Woocommerce mini-cart / Cart
  • Chef - how to write a custom resource containing DSL for “execute”
  • Wrapping an SVN Python hook in a Windows batch file
  • Does ruby on rails have client side validation like ASP.NET MVC
  • Merging Users in Kinvey
  • Integrate two requests into one Javascript
  • Reload UITableView from another tab
  • Add two consecutive lines in sed
  • How to convert row to column in notepad++
  • Find tangent points on a curve from a user-given point outside the curve
  • Concise regex extract function in XSLT 2.0
  • Converting raw frames into webm live stream
  • Insert space after period using sed
  • Ruby: Why does this way of using map throw an error?
  • Why I can't use uniform1f instead of uniform4f for setting a vec4 uniform?
  • C Program Compiled with Cygwin and Called from Python Hangs
  • Creating a C++ function that calls other Lua function
  • pythonanywhere - How do I use websockets to transmit messages as per the web2py messaging example?
  • Use default value of a column in stored procedures
  • Geom_jitter colour based on values
  • Replace last two characters in column
  • NSIS decompiler
  • PayPal API Listener Website Payments Standard URI
  • In loopback documentation what does variable 'cb' stands for?
  • Defined variables not working in javascript files when I use getScript
  • Can't remove headers after they are sent
  • How to synchronize jQuery dialog box to act like alert() of Javascript
  • SAXReader not re-ecape characters
  • Checking free space on FTP server
  • jquery mobile loadPage not working
  • R: gsub and capture
  • AT Commands to Send SMS not working in Windows 8.1
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How to stop GridView from loading again when I press back button?