-
-
Save Averroes/7f85ccad4606b5e9ab5f8d3d551c38ef to your computer and use it in GitHub Desktop.
WooCommerce - Set a custom add to cart URL to redirect to
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function custom_add_to_cart_redirect() { | |
| /** | |
| * Replace with the url of your choosing | |
| * e.g. return 'http://www.yourshop.com/' | |
| */ | |
| return get_permalink( get_option('woocommerce_checkout_page_id') ); | |
| } | |
| add_filter( 'woocommerce_add_to_cart_redirect', 'custom_add_to_cart_redirect' ); | |
| /** | |
| Redirect for certain products | |
| */ | |
| /** | |
| * Redirect users after add to cart. | |
| */ | |
| function my_custom_add_to_cart_redirect( $url ) { | |
| if ( ! isset( $_REQUEST['add-to-cart'] ) || ! is_numeric( $_REQUEST['add-to-cart'] ) ) { | |
| return $url; | |
| } | |
| $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_REQUEST['add-to-cart'] ) ); | |
| // Only redirect the product IDs in the array to the checkout | |
| if ( in_array( $product_id, array( 1, 16, 24) ) ) { | |
| $url = WC()->cart->get_checkout_url(); | |
| } | |
| return $url; | |
| } | |
| add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' ); | |
| view raw | |
| woocommerce-conditionally-redirect-users-after-add-to-cart.php hosted with ❤ by GitHub | |
| Redirect for certain categories | |
| <?php | |
| /** | |
| * Redirect users after add to cart. | |
| */ | |
| function my_custom_add_to_cart_redirect( $url ) { | |
| if ( ! isset( $_REQUEST['add-to-cart'] ) || ! is_numeric( $_REQUEST['add-to-cart'] ) ) { | |
| return $url; | |
| } | |
| $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_REQUEST['add-to-cart'] ) ); | |
| // Only redirect products that have the 't-shirts' category | |
| if ( has_term( 't-shirts', 'product_cat', $product_id ) ) { | |
| $url = WC()->cart->get_checkout_url(); | |
| } | |
| return $url; | |
| } | |
| add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' ); | |
| view raw | |
| woocommerce-conditionally-redirect-users-after-add-to-cart-category.php hosted with ❤ by GitHub | |
| Redirect for certain shipping classes | |
| <?php | |
| /** | |
| * Redirect users after add to cart. | |
| */ | |
| function my_custom_add_to_cart_redirect( $url ) { | |
| if ( ! isset( $_REQUEST['add-to-cart'] ) || ! is_numeric( $_REQUEST['add-to-cart'] ) ) { | |
| return $url; | |
| } | |
| $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_REQUEST['add-to-cart'] ) ); | |
| // Only redirect products with the 'small-item' shipping class | |
| if ( has_term( 'small-item', 'product_shipping_class', $product_id ) ) { | |
| $url = WC()->cart->get_checkout_url(); | |
| } | |
| return $url; | |
| } | |
| add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );* | |
| /** | |
| * Custom Add To Cart Messages | |
| * Add this to your theme functions.php file | |
| **/ | |
| add_filter( 'woocommerce_add_to_cart_message', 'custom_add_to_cart_message' ); | |
| function custom_add_to_cart_message() { | |
| global $woocommerce; | |
| // Output success messages | |
| if (get_option('woocommerce_cart_redirect_after_add')=='yes') : | |
| $return_to = get_permalink(woocommerce_get_page_id('shop')); | |
| $message = sprintf('<a href="%s" class="button">%s</a> %s', $return_to, __('Continue Shopping →', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') ); | |
| else : | |
| $message = sprintf('<a href="%s" class="button">%s</a> %s', get_permalink(woocommerce_get_page_id('cart')), __('View Cart →', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') ); | |
| endif; | |
| return $message; | |
| } | |
| // Redirect custom thank you | |
| add_action( 'woocommerce_thankyou', 'bbloomer_redirectcustom'); | |
| function bbloomer_redirectcustom( $order_id ){ | |
| $order = new WC_Order( $order_id ); | |
| $url = 'http://yoursite.com/custom-url'; | |
| if ( $order->status != 'failed' ) { | |
| wp_redirect($url); | |
| exit; | |
| } | |
| } | |
| // Redirect custom thank you | |
| add_action( 'woocommerce_thankyou', 'bbloomer_redirectcustom'); | |
| function bbloomer_redirectcustom( $order_id ){ | |
| $order = new WC_Order( $order_id ); | |
| $url = 'http://yoursite.com/custom-url'; | |
| if ( $order->status != 'failed' ) { | |
| echo "<script type='text/javascript'>window.location = '" . $url . "'</script>"; | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment