Created
August 24, 2016 00:03
-
-
Save leandro/89b1e7622227a06fd57ece09ae55e4a4 to your computer and use it in GitHub Desktop.
Redirect to Cart page when shipping info is not filled yet
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
diff --git a/wordpress/wp-content/plugins/woocommerce/includes/class-wc-cart.php b/wordpress/wp-content/plugins/woocommerce/includes/class-wc-cart.php | |
index 5563396..681a859 100644 | |
--- a/wordpress/wp-content/plugins/woocommerce/includes/class-wc-cart.php | |
+++ b/wordpress/wp-content/plugins/woocommerce/includes/class-wc-cart.php | |
@@ -105,6 +105,7 @@ class WC_Cart { | |
* Constructor for the cart class. Loads options and hooks in the init method. | |
*/ | |
public function __construct() { | |
+ add_action( 'wp_head', array( $this, 'force_shipping_to_be_set_on_checkout' ) ); | |
add_action( 'wp_loaded', array( $this, 'init' ) ); // Get cart after WP and plugins are loaded. | |
add_action( 'wp', array( $this, 'maybe_set_cart_cookies' ), 99 ); // Set cookies | |
add_action( 'shutdown', array( $this, 'maybe_set_cart_cookies' ), 0 ); // Set cookies before shutdown and ob flushing | |
@@ -1617,6 +1618,22 @@ class WC_Cart { | |
} | |
/*-----------------------------------------------------------------------------------*/ | |
+ /* Custom functions | |
+ /*-----------------------------------------------------------------------------------*/ | |
+ | |
+ /** | |
+ * Certifies that user gets redirected to Cart page if shipping is enabled, mandatory but the user didn't set to where he wants the products to be delivered. | |
+ * This will only happen if in Checkout page. | |
+ */ | |
+ public function force_shipping_to_be_set_on_checkout() { | |
+ if ( is_checkout() && $this->needs_shipping() && empty( WC()->customer->get_shipping_postcode() ) ) { | |
+ wc_add_notice( __( 'Before going to Checkout page you need to set your shipping information', 'woocommerce' ), 'error' ); | |
+ wp_redirect( wc_get_page_permalink( 'cart' ) ); | |
+ exit(); | |
+ } | |
+ } | |
+ | |
+ /*-----------------------------------------------------------------------------------*/ | |
/* Coupons/Discount related functions */ | |
/*-----------------------------------------------------------------------------------*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment