Shipping method tweaks/api to add multiple rates easily.

This commit is contained in:
Mike Jolley 2012-01-03 19:07:32 +00:00
parent ca0e9744ab
commit 28922bb4ef
8 changed files with 113 additions and 87 deletions

View File

@ -657,9 +657,9 @@ function woocommerce_process_product_meta( $post_id, $post ) {
endif;
// Upsells
if (isset($_POST['_upsell_ids'])) :
if (isset($_POST['upsell_ids'])) :
$upsells = array();
$ids = $_POST['_upsell_ids'];
$ids = $_POST['upsell_ids'];
foreach ($ids as $id) :
if ($id && $id>0) $upsells[] = $id;
endforeach;
@ -669,9 +669,9 @@ function woocommerce_process_product_meta( $post_id, $post ) {
endif;
// Cross sells
if (isset($_POST['_crosssell_ids'])) :
if (isset($_POST['crosssell_ids'])) :
$crosssells = array();
$ids = $_POST['_crosssell_ids'];
$ids = $_POST['crosssell_ids'];
foreach ($ids as $id) :
if ($id && $id>0) $crosssells[] = $id;
endforeach;

View File

@ -955,9 +955,9 @@ class woocommerce_cart {
$this->shipping_total = $woocommerce->shipping->shipping_total; // Shipping Total
$this->shipping_label = $woocommerce->shipping->shipping_label; // Shipping Label
$this->shipping_tax_total = $woocommerce->shipping->shipping_tax; // Shipping tax amount
$this->shipping_tax_total = array_sum( $woocommerce->shipping->shipping_taxes ); // Shipping tax amount
// Shipping tax rows
// Merge Shipping tax rows with cart tax rows
if (is_array($woocommerce->shipping->shipping_taxes) && sizeof($woocommerce->shipping->shipping_taxes)>0) :
// Tax rows - merge the totals we just got
foreach (array_keys($this->taxes + $woocommerce->shipping->shipping_taxes) as $key) {

View File

@ -118,9 +118,9 @@ class flat_rate extends woocommerce_shipping_method {
$_tax = &new woocommerce_tax();
$this->shipping_total = 0;
$this->shipping_tax = 0;
$this->shipping_taxes = array();
$this->rates = array();
$shipping_taxes = array();
$shipping_total = 0;
if ($this->type=='order') :
@ -167,16 +167,20 @@ class flat_rate extends woocommerce_shipping_method {
endif;
// Shipping for whole order
$this->shipping_total = $cost + $this->get_fee( $fee, $woocommerce->cart->cart_contents_total );
$shipping_total = $cost + $this->get_fee( $fee, $woocommerce->cart->cart_contents_total );
if ( get_option('woocommerce_calc_taxes')=='yes' && $this->tax_status=='taxable' ) :
$rate = $_tax->get_shipping_tax_rates();
if ($rate>0) :
$tax_amount = $_tax->calc_shipping_tax( $this->shipping_total, $rate );
$this->shipping_tax = $this->shipping_tax + $tax_amount;
endif;
$rates = $_tax->get_shipping_tax_rates();
$shipping_taxes = $_tax->calc_shipping_tax( $shipping_total, $rates );
endif;
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => $shipping_total,
'taxes' => $shipping_taxes
);
elseif ($this->type=='class') :
// Shipping per class
$cost = null;
@ -216,16 +220,20 @@ class flat_rate extends woocommerce_shipping_method {
endif;
// Total
$this->shipping_total = $cost + $fee;
$shipping_total = $cost + $fee;
if ( get_option('woocommerce_calc_taxes')=='yes' && $this->tax_status=='taxable' ) :
$rate = $_tax->get_shipping_tax_rates();
if ($rate>0) :
$tax_amount = $_tax->calc_shipping_tax( $this->shipping_total, $rate );
$this->shipping_tax = $this->shipping_tax + $tax_amount;
endif;
$rates = $_tax->get_shipping_tax_rates();
$shipping_taxes = $_tax->calc_shipping_tax( $shipping_total, $rates );
endif;
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => $shipping_total,
'taxes' => $shipping_taxes
);
elseif ($this->type=='item') :
// Shipping per item
@ -247,7 +255,7 @@ class flat_rate extends woocommerce_shipping_method {
$item_shipping_price = ( $cost + $fee ) * $values['quantity'];
$this->shipping_total = $this->shipping_total + $item_shipping_price;
$shipping_total = $shipping_total + $item_shipping_price;
if ( $_product->is_shipping_taxable() && $this->tax_status=='taxable' ) :
@ -256,8 +264,8 @@ class flat_rate extends woocommerce_shipping_method {
$item_taxes = $_tax->calc_shipping_tax( $item_shipping_price, $rates );
// Sum the item taxes
foreach (array_keys($this->shipping_taxes + $item_taxes) as $key) {
$this->shipping_taxes[$key] = (isset($item_taxes[$key]) ? $item_taxes[$key] : 0) + (isset($this->shipping_taxes[$key]) ? $this->shipping_taxes[$key] : 0);
foreach (array_keys($shipping_taxes + $item_taxes) as $key) {
$shipping_taxes[$key] = (isset($item_taxes[$key]) ? $item_taxes[$key] : 0) + (isset($shipping_taxes[$key]) ? $shipping_taxes[$key] : 0);
}
endif;
@ -265,9 +273,17 @@ class flat_rate extends woocommerce_shipping_method {
endif;
endforeach; endif;
$this->shipping_tax = array_sum( $this->shipping_taxes );
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => $shipping_total,
'taxes' => $shipping_taxes
);
endif;
// Register the rate
$this->add_rate( $rate );
}
/**

View File

@ -159,9 +159,13 @@ class free_shipping extends woocommerce_shipping_method {
}
function calculate_shipping() {
$this->shipping_total = 0;
$this->shipping_tax = 0;
$this->shipping_label = $this->title;
$args = array(
'id' => $this->id,
'label' => $this->title,
'cost' => 0,
'taxes' => array()
);
$this->add_rate( $args );
}
}

View File

@ -15,7 +15,6 @@ class woocommerce_shipping {
var $shipping_methods = array();
var $chosen_method = null;
var $shipping_total = 0;
var $shipping_tax = 0;
var $shipping_taxes = array();
var $shipping_label = null;
var $shipping_classes;
@ -51,11 +50,9 @@ class woocommerce_shipping {
ksort($this->shipping_methods);
add_action('woocommerce_update_options_shipping', array(&$this, 'process_admin_options'));
}
function get_shipping_classes() {
if (!is_array($this->shipping_classes)) :
$args = array(
@ -71,7 +68,6 @@ class woocommerce_shipping {
}
function get_available_shipping_methods() {
if ($this->enabled=='yes') :
$_available_methods = array();
@ -83,22 +79,8 @@ class woocommerce_shipping {
$shipping_method->calculate_shipping();
// If available, put available methods/rates in the array
if ($shipping_method->multiple_rates) :
foreach ($shipping_method->rates as $rate) :
$method = $rate;
$_available_methods[$method->id] = $method;
endforeach;
else :
$method = $shipping_method;
$_available_methods[$method->id] = $method;
if (is_array($shipping_method->rates) && sizeof($shipping_method->rates) > 0) :
foreach ($shipping_method->rates as $rate) $_available_methods[$rate->id] = $rate;
endif;
endif;
@ -112,22 +94,17 @@ class woocommerce_shipping {
function reset_shipping_methods() {
foreach ( $this->shipping_methods as $shipping_method ) :
$shipping_method->shipping_total = 0;
$shipping_method->shipping_tax = 0;
$shipping_method->shipping_taxes = array();
$shipping_method->rates = array();
endforeach;
}
function calculate_shipping() {
if ($this->enabled=='yes') :
$this->shipping_total = 0;
$this->shipping_tax = 0;
$this->shipping_taxes = array();
$this->shipping_label = null;
$_cheapest_fee = '';
$_cheapest_cost = '';
$_cheapest_method = '';
if (isset($_SESSION['_chosen_shipping_method'])) $chosen_method = $_SESSION['_chosen_shipping_method']; else $chosen_method = '';
$calc_cheapest = false;
@ -141,13 +118,10 @@ class woocommerce_shipping {
if (sizeof($_available_methods)>0) :
foreach ($_available_methods as $method_id => $method) :
$fee = $method->shipping_total;
if ($fee < $_cheapest_fee || !is_numeric($_cheapest_fee)) :
$_cheapest_fee = $fee;
if ($method->cost < $_cheapest_cost || !is_numeric($_cheapest_cost)) :
$_cheapest_cost = $method->cost;
$_cheapest_method = $method_id;
endif;
endforeach;
// Default to cheapest
@ -156,24 +130,19 @@ class woocommerce_shipping {
endif;
if ($chosen_method) :
$_SESSION['_chosen_shipping_method'] = $chosen_method;
$this->shipping_total = $_available_methods[$chosen_method]->shipping_total;
$this->shipping_tax = $_available_methods[$chosen_method]->shipping_tax;
$this->shipping_taxes = $_available_methods[$chosen_method]->shipping_taxes;
$this->shipping_label = $_available_methods[$chosen_method]->title;
$this->shipping_total = $_available_methods[$chosen_method]->cost;
$this->shipping_taxes = $_available_methods[$chosen_method]->taxes;
$this->shipping_label = $_available_methods[$chosen_method]->label;
endif;
endif;
endif;
}
function reset_shipping() {
unset($_SESSION['_chosen_shipping_method']);
$this->shipping_total = 0;
$this->shipping_tax = 0;
$this->shipping_taxes = array();
$this->shipping_label = null;
}
@ -193,5 +162,4 @@ class woocommerce_shipping {
update_option( 'woocommerce_shipping_method_order', $order );
}
}

View File

@ -12,26 +12,54 @@
class woocommerce_shipping_method extends woocommerce_settings_api {
var $id;
var $method_title;
var $title;
var $method_title; // Method title
var $title; // User set title
var $availability;
var $countries;
var $type;
var $fee = 0;
var $min_amount = null;
var $enabled = false;
var $shipping_total = 0;
var $shipping_tax = 0;
var $cost = 0; // Stores cost if theres only one
var $multiple_rates = false;
var $rates = array(); // When a method has more than one cost/choice it will be in this array of titles/costs
/**
* Rates
*
* This is an array of rates - methods must populate this array to register shipping costs
*/
var $rates = array(); // This is an array of rates - methods must populate this array to register shipping costs
function add_rate( $args = array() ) {
$defaults = array(
'id' => '',
'label' => '',
'cost' => '0',
'taxes' => array()
);
$args = wp_parse_args( $args, $defaults );
extract( $args );
// Id and label are required
if (!$id || !$label) return;
$rate = new stdClass;
$rate->id = $id;
$rate->label = $label;
$rate->cost = $cost;
$rate->taxes = $taxes;
$this->rates[] = $rate;
}
function is_available() {
global $woocommerce;
if ($this->enabled=="no") return false;
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) 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 = '';

View File

@ -4,7 +4,7 @@ Tags: ecommerce, e-commerce, commerce, woothemes, wordpress ecommerce, store, sh
Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=paypal@woothemes.com&item_name=Donation+for+WooCommerce
Requires at least: 3.3
Tested up to: 3.3
Stable tag: 1.3.2.1
Stable tag: 1.4
WooCommerce is an open-source e-commerce toolkit that helps you sell anything. Beautifully.
@ -84,6 +84,7 @@ Yes you can! Join in on our GitHub repository :) https://github.com/woothemes/wo
= 1.4 =
* Support for multiple and stacked (compound) taxes
* Locale options for country address formatting and checkout fields
* Multiple taxes shown in order total tables
* Rewritten parts + re-organised files for increased performance and decreased memory usage
* Moved many shortcodes (contents) to template files for easier customisation
@ -94,7 +95,6 @@ Yes you can! Join in on our GitHub repository :) https://github.com/woothemes/wo
* Changed woocommerce_breadcrumb args
* Filters for customer email attachments
* Chosen selects for country/state select inputs (optional)
* Locale options for country address formatting and checkout fields
* Piwik (http://piwik.org/) tracking - requires http://wordpress.org/extend/plugins/wp-piwik/
* Option to hide cart widget if the cart is empty
* Category widget - order by option
@ -113,6 +113,8 @@ Yes you can! Join in on our GitHub repository :) https://github.com/woothemes/wo
* Enabled product custom fields panel
* Renamed custom fields for product data - upgrade script will run when upgrading. Some themes may be affected if using 'featured' - it is now '_featured'
* woocommerce_product_visibility_options filter for backend
* Shipping method classes/api changed to make rate definition simpler - shipping methods will need updating to stay compatible
* Change textdomain from woothemes to woocommerce
= 1.3.2.1 - 15/12/2011 =
* Category/Ordering fix
@ -404,7 +406,15 @@ Yes you can! Join in on our GitHub repository :) https://github.com/woothemes/wo
== Upgrade Notice ==
= 1.4 =
Major update with plenty of optimisations and new features. Product data meta has been renamed in this version so that custom-fields can be enabled. Product data is now prepended with an underscore so they are hidden from the custom-field panel. Existing data will be upgraded automatically. Please backup your database before upgrading and also ensure you are running the latest versions of any WooCommerce plugins after upgrading.
Major update with plenty of optimisations and new features. Changed to note:
* Requires WP 3.3
* Product data meta has been renamed in this version so that custom-fields can be enabled. Product data is now prepended with an underscore so they are hidden from the custom-field panel. Existing data will be upgraded automatically.
* The shipping method classes have been updated to make rate definition easier. Third party plugins will need updating.
* Textdomain has changed - re-scan your po/mo's
* Tax additions (tax rows, compounds etc) required a change to the way order items are stored. Old orders won't show items when viewed. Order totals should be unaffected.
Please backup your database before upgrading and also ensure you are running the latest versions of any WooCommerce plugins after upgrading.
= 1.3 =
This is a major update and includes improvements to the tax and coupon system in particular - please backup your database before upgrading and also ensure you are running the latest versions of any WooCommerce plugins after upgrading.

View File

@ -43,21 +43,21 @@ $available_methods = $woocommerce->shipping->get_available_shipping_methods();
echo '<option value="'.esc_attr($method->id).'" ';
echo '<option value="'.$method->id.'" '.selected($method->id, $_SESSION['_chosen_shipping_method'], false).'>'.$method->title.' &mdash; ';
echo '<option value="'.$method->id.'" '.selected($method->id, $_SESSION['_chosen_shipping_method'], false).'>'.$method->label.' &mdash; ';
if ($method->shipping_total>0) :
if ($method->cost>0) :
if ($woocommerce->cart->display_totals_ex_tax || !$woocommerce->cart->prices_include_tax) :
echo woocommerce_price($method->shipping_total);
if ($method->shipping_tax>0 && $woocommerce->cart->prices_include_tax) :
echo woocommerce_price($method->cost);
if (array_sum($method->taxes)>0 && $woocommerce->cart->prices_include_tax) :
echo ' ' . $woocommerce->countries->ex_tax_or_vat();
endif;
else :
echo woocommerce_price($method->shipping_total + $method->shipping_tax);
if ($method->shipping_tax>0 && !$woocommerce->cart->prices_include_tax) :
echo woocommerce_price($method->cost + array_sum($method->taxes));
if (array_sum($method->taxes)>0 && !$woocommerce->cart->prices_include_tax) :
echo ' ' . $woocommerce->countries->inc_tax_or_vat();
endif;