From c4407d1b6f0bd26181a7e5b0bc0b5898304bd897 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 28 Nov 2011 16:10:31 +0000 Subject: [PATCH] FREESHIPPING --- admin/writepanels/writepanel-coupon_data.php | 5 +++ classes/coupons.class.php | 6 +++ classes/shipping/shipping-free_shipping.php | 47 ++++++++++++++++++++ readme.txt | 1 + 4 files changed, 59 insertions(+) diff --git a/admin/writepanels/writepanel-coupon_data.php b/admin/writepanels/writepanel-coupon_data.php index d4cab60fab5..09bc1708b7d 100644 --- a/admin/writepanels/writepanel-coupon_data.php +++ b/admin/writepanels/writepanel-coupon_data.php @@ -38,6 +38,9 @@ function woocommerce_coupon_data_meta_box($post) { // Apply before tax woocommerce_wp_checkbox( array( 'id' => 'apply_before_tax', 'label' => __('Apply coupon before tax?', 'woothemes'), 'description' => __('Check this box if the coupon should be applied before calculating a product\'s tax', 'woothemes') ) ); + // Free Shipping + woocommerce_wp_checkbox( array( 'id' => 'free_shipping', 'label' => __('Enable free shipping', 'woothemes'), 'description' => sprintf(__('Check this box if the coupon enables free shipping (see Free Shipping)', 'woothemes'), admin_url('admin.php?page=woocommerce&tab=shipping_methods&subtab=shipping-free_shipping')) ) ); + // Product ids woocommerce_wp_text_input( array( 'id' => 'product_ids', 'label' => __('Product IDs', 'woothemes'), 'placeholder' => __('N/A', 'woothemes'), 'description' => __('(optional) Comma separate IDs which need to be in the cart to use this coupon or, for "Product Discounts", which products are discounted.', 'woothemes') ) ); @@ -78,6 +81,7 @@ function woocommerce_process_shop_coupon_meta( $post_id, $post ) { $individual_use = isset($_POST['individual_use']) ? 'yes' : 'no'; $expiry_date = strip_tags(stripslashes( $_POST['expiry_date'] )); $apply_before_tax = isset($_POST['apply_before_tax']) ? 'yes' : 'no'; + $free_shipping = isset($_POST['free_shipping']) ? 'yes' : 'no'; // Save update_post_meta( $post_id, 'discount_type', $type ); @@ -88,6 +92,7 @@ function woocommerce_process_shop_coupon_meta( $post_id, $post ) { update_post_meta( $post_id, 'usage_limit', $usage_limit ); update_post_meta( $post_id, 'expiry_date', $expiry_date ); update_post_meta( $post_id, 'apply_before_tax', $apply_before_tax ); + update_post_meta( $post_id, 'free_shipping', $free_shipping ); do_action('woocommerce_coupon_options'); diff --git a/classes/coupons.class.php b/classes/coupons.class.php index 25bd26eec4a..cf7f78f0559 100644 --- a/classes/coupons.class.php +++ b/classes/coupons.class.php @@ -21,6 +21,7 @@ class woocommerce_coupon { var $usage_count; var $expiry_date; var $apply_before_tax; + var $free_shipping; /** get coupon with $code */ function woocommerce_coupon( $code ) { @@ -41,6 +42,7 @@ class woocommerce_coupon { $this->usage_count = (int) get_post_meta($coupon->ID, 'usage_count', true); $this->expiry_date = ($expires = get_post_meta($coupon->ID, 'expiry_date', true)) ? strtotime($expires) : ''; $this->apply_before_tax = get_post_meta($coupon->ID, 'apply_before_tax', true); + $this->free_shipping = get_post_meta($coupon->ID, 'free_shipping', true); return true; @@ -54,6 +56,10 @@ class woocommerce_coupon { if ($this->apply_before_tax=='yes') return true; else return false; } + function enable_free_shipping() { + if ($this->free_shipping=='yes') return true; else return false; + } + /** Increase usage count */ function inc_usage_count() { $this->usage_count++; diff --git a/classes/shipping/shipping-free_shipping.php b/classes/shipping/shipping-free_shipping.php index ef9c01db030..7b035b084b8 100644 --- a/classes/shipping/shipping-free_shipping.php +++ b/classes/shipping/shipping-free_shipping.php @@ -27,6 +27,7 @@ class free_shipping extends woocommerce_shipping_method { $this->min_amount = $this->settings['min_amount']; $this->availability = $this->settings['availability']; $this->countries = $this->settings['countries']; + $this->requires_coupon = $this->settings['requires_coupon']; // Actions add_action('woocommerce_update_options_shipping_methods', array(&$this, 'process_admin_options')); @@ -57,6 +58,13 @@ class free_shipping extends woocommerce_shipping_method { 'description' => __('Users will need to spend this amount to get free shipping. Leave blank to disable.', 'woothemes'), 'default' => '' ), + 'requires_coupon' => array( + 'title' => __( 'Coupon', 'woothemes' ), + 'type' => 'checkbox', + 'label' => __( 'Free shipping requires a free shipping coupon', 'woothemes' ), + 'description' => __('Users will need to enter a valid free shipping coupon code to use this method.', 'woothemes'), + 'default' => 'no' + ), 'availability' => array( 'title' => __( 'Method availability', 'woothemes' ), 'type' => 'select', @@ -100,6 +108,45 @@ class free_shipping extends woocommerce_shipping_method { enabled=="no") return false; + + if (isset($woocommerce->cart->cart_contents_total) && isset($this->min_amount) && $this->min_amount && $this->min_amount > $woocommerce->cart->cart_contents_total) return false; + + $ship_to_countries = ''; + + if ($this->availability == 'specific') : + $ship_to_countries = $this->countries; + else : + if (get_option('woocommerce_allowed_countries')=='specific') : + $ship_to_countries = get_option('woocommerce_specific_allowed_countries'); + endif; + endif; + + if (is_array($ship_to_countries)) : + if (!in_array($woocommerce->customer->get_shipping_country(), $ship_to_countries)) return false; + endif; + + if ($this->requires_coupon=="yes") : + + if ($woocommerce->cart->applied_coupons) : foreach ($woocommerce->cart->applied_coupons as $code) : + $coupon = &new woocommerce_coupon( $code ); + + if ( $coupon->enable_free_shipping() ) : + return true; + endif; + + endforeach; endif; + + return false; + + endif; + + return true; + } function calculate_shipping() { $this->shipping_total = 0; diff --git a/readme.txt b/readme.txt index 48918e5f11e..01f484e47ba 100644 --- a/readme.txt +++ b/readme.txt @@ -112,6 +112,7 @@ Yes you can! Join in on our GitHub repository :) https://github.com/woothemes/wo * Added dimensions to individual variations * Added settings API to be used by Shipping Methods and Payment Gateways * Free shipping/Flat rate uses setting API +* Free shipping coupons = 1.2.4 - 18/11/2011 = * More sale price logic fixes for variations. Now correctly compares variation's prices.