Skip to content

Instantly share code, notes, and snippets.

@leandro
Created August 24, 2016 00:03
Show Gist options
  • Save leandro/89b1e7622227a06fd57ece09ae55e4a4 to your computer and use it in GitHub Desktop.
Save leandro/89b1e7622227a06fd57ece09ae55e4a4 to your computer and use it in GitHub Desktop.
Redirect to Cart page when shipping info is not filled yet
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