From 27b2c87600448a62d27b2b2cd30b8358c5122674 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 16 Apr 2012 10:13:13 +0100 Subject: [PATCH] Feature - Local delivery cost per item condition --- admin/woocommerce-admin-settings.php | 4 ++-- classes/shipping/class-wc-local-delivery.php | 20 ++++++++++++++++--- classes/shipping/class-wc-shipping-method.php | 12 +++++------ readme.txt | 1 + 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/admin/woocommerce-admin-settings.php b/admin/woocommerce-admin-settings.php index 450ef44fded..c7f975e698d 100644 --- a/admin/woocommerce-admin-settings.php +++ b/admin/woocommerce-admin-settings.php @@ -1287,13 +1287,13 @@ function woocommerce_settings() { $current = ( empty( $_GET['section'] ) ) ? 'class="current"' : ''; - $links = array( '' . __('Shipping Options', 'woocommerce') . '' ); + $links = array( '' . __('Shipping Options', 'woocommerce') . '' ); foreach ( $woocommerce->shipping->shipping_methods as $method ) { $title = ( isset( $method->method_title ) && $method->method_title) ? ucwords($method->method_title) : ucwords($method->id); $current = ( ! empty( $_GET['section'] ) && $method->id == urldecode( $_GET['section'] ) ) ? 'class="current"' : ''; - $links[] = '' . $title . ''; + $links[] = '' . $title . ''; } echo '
'; diff --git a/classes/shipping/class-wc-local-delivery.php b/classes/shipping/class-wc-local-delivery.php index 6fe8f100d66..383a5fbdd5d 100644 --- a/classes/shipping/class-wc-local-delivery.php +++ b/classes/shipping/class-wc-local-delivery.php @@ -28,6 +28,7 @@ class WC_Local_Delivery extends WC_Shipping_Method { // Define user set variables $this->enabled = empty( $this->settings['enabled'] ) ? 'no' : $this->settings['enabled']; $this->title = empty( $this->settings['title'] ) ? '' : $this->settings['title']; + $this->type = $this->settings['type']; $this->fee = empty( $this->settings['fee'] ) ? '' : $this->settings['fee']; $this->type = empty( $this->settings['type'] ) ? '' : $this->settings['type']; $this->codes = empty( $this->settings['codes'] ) ? '' : $this->settings['codes']; @@ -39,11 +40,23 @@ class WC_Local_Delivery extends WC_Shipping_Method { function calculate_shipping( $package = array() ) { global $woocommerce; - + + $shipping_total = 0; $fee = ( trim( $this->fee ) == '' ) ? 0 : $this->fee; + if ( $this->type =='fixed' ) $shipping_total = $this->fee; + if ( $this->type =='percent' ) $shipping_total = $package['contents_cost'] * ( $this->fee / 100 ); + if ( $this->type == 'product' ) { + foreach ( $woocommerce->cart->get_cart() as $item_id => $values ) { + $_product = $values['data']; + + if ( $values['quantity'] > 0 && $_product->needs_shipping() ) + $shipping_total += $this->fee * $values['quantity']; + } + } + $rate = array( 'id' => $this->id, 'label' => $this->title, @@ -74,8 +87,9 @@ class WC_Local_Delivery extends WC_Shipping_Method { 'description' => __( 'How to calculate delivery charges', 'woocommerce' ), 'default' => 'fixed', 'options' => array( - 'fixed' => __('Fixed Amount', 'woocommerce'), - 'percent' => __('Percentage of Cart Total', 'woocommerce'), + 'fixed' => __('Fixed amount', 'woocommerce'), + 'percent' => __('Percentage of cart total', 'woocommerce'), + 'product' => __('Fixed amount per product', 'woocommerce'), ), ), 'fee' => array( diff --git a/classes/shipping/class-wc-shipping-method.php b/classes/shipping/class-wc-shipping-method.php index 4f512331b65..b2a1c4f1c88 100644 --- a/classes/shipping/class-wc-shipping-method.php +++ b/classes/shipping/class-wc-shipping-method.php @@ -118,27 +118,27 @@ class WC_Shipping_Method extends WC_Settings_API { $this->rates[] = new WC_Shipping_Rate( $id, $label, $total_cost, $taxes ); } - function is_available() { + function is_available( $package ) { global $woocommerce; if ($this->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) + 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') : + if ( $this->availability == 'specific' ) : $ship_to_countries = $this->countries; else : - if (get_option('woocommerce_allowed_countries')=='specific') : + 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; + if ( is_array( $ship_to_countries ) ) : + if ( ! in_array( $package['destination']['country'], $ship_to_countries ) ) return false; endif; return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', true ); diff --git a/readme.txt b/readme.txt index 2e26ad75056..c5cd02f1269 100644 --- a/readme.txt +++ b/readme.txt @@ -153,6 +153,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc * Feature - Shortcode to show a list of all product categories * Feature - Minimum fee option for flat rate shipping * Feature - "Package" support for shipping calculations - allows third parties to split the cart up to calc shipping. Also caches packages meaning the cart shipping only needs to be calculated once (until totals or customer location changes) +* Feature - Local delivery cost per item condition * Tweak - Redesigned variation panels and variation bulk edit to make them easier to use and expand. * Tweak - Show multiple success messages/error messages if added, rather than one or the other * Tweak - Adding an item to an order now uses the ajax style product finder