Flat rate shipping - if no rules match, and no default is set, don't return a rate. Closes #1695.

This commit is contained in:
Mike Jolley 2012-11-19 14:48:40 +00:00
parent 22afc34052
commit ad78c3ccc0
2 changed files with 73 additions and 35 deletions

View File

@ -175,37 +175,55 @@ class WC_Flat_Rate extends WC_Shipping_Method {
function calculate_shipping( $package = array() ) { function calculate_shipping( $package = array() ) {
global $woocommerce; global $woocommerce;
$this->rates = array(); $this->rates = array();
$cost_per_order = ( isset( $this->cost_per_order ) && ! empty( $this->cost_per_order ) ) ? $this->cost_per_order : 0; $cost_per_order = ( isset( $this->cost_per_order ) && ! empty( $this->cost_per_order ) ) ? $this->cost_per_order : 0;
if ( $this->type == 'order' ) { if ( $this->type == 'order' ) {
$shipping_total = $this->order_shipping( $package ); $shipping_total = $this->order_shipping( $package );
$rate = array( if ( ! is_null( $shipping_total ) || $cost_per_order > 0 )
'id' => $this->id, $rate = array(
'label' => $this->title, 'id' => $this->id,
'cost' => $shipping_total + $cost_per_order, 'label' => $this->title,
); 'cost' => $shipping_total + $cost_per_order,
);
} elseif ( $this->type == 'class' ) { } elseif ( $this->type == 'class' ) {
$shipping_total = $this->class_shipping( $package ); $shipping_total = $this->class_shipping( $package );
$rate = array( if ( ! is_null( $shipping_total ) || $cost_per_order > 0 )
'id' => $this->id, $rate = array(
'label' => $this->title, 'id' => $this->id,
'cost' => $shipping_total + $cost_per_order, 'label' => $this->title,
); 'cost' => $shipping_total + $cost_per_order,
} elseif ( $this->type == 'item' ) { );
$costs = $this->item_shipping( $package );
$costs['order'] = $cost_per_order;
$rate = array( } elseif ( $this->type == 'item' ) {
'id' => $this->id,
'label' => $this->title, $costs = $this->item_shipping( $package );
'cost' => $costs,
'calc_tax' => 'per_item', if ( ! is_null( $costs ) || $cost_per_order > 0 ) {
);
if ( ! is_array( $costs ) )
$costs = array();
$costs['order'] = $cost_per_order;
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => $costs,
'calc_tax' => 'per_item',
);
}
} }
if ( ! isset( $rate ) )
return;
// Register the rate // Register the rate
$this->add_rate( $rate ); $this->add_rate( $rate );
@ -286,7 +304,7 @@ class WC_Flat_Rate extends WC_Shipping_Method {
} }
} else { } else {
// No matching classes so use defaults // No matching classes so use defaults
if ( $this->cost > $cost ) { if ( ! empty( $this->cost ) && $this->cost > $cost ) {
$cost = $this->cost; $cost = $this->cost;
$fee = $this->fee; $fee = $this->fee;
} }
@ -295,10 +313,13 @@ class WC_Flat_Rate extends WC_Shipping_Method {
} }
// Default rates // Default rates if set
if ( is_null( $cost ) ) { if ( is_null( $cost ) && $this->cost !== '' ) {
$cost = $this->cost; $cost = $this->cost;
$fee = $this->fee; $fee = $this->fee;
} elseif ( is_null( $cost ) ) {
// No match
return null;
} }
// Shipping for whole order // Shipping for whole order
@ -335,21 +356,28 @@ class WC_Flat_Rate extends WC_Shipping_Method {
$found_shipping_classes = array_unique( $found_shipping_classes ); $found_shipping_classes = array_unique( $found_shipping_classes );
$matched = false;
// For each found class, add up the costs and fees // For each found class, add up the costs and fees
foreach ( $found_shipping_classes as $shipping_class => $class_price ) { foreach ( $found_shipping_classes as $shipping_class => $class_price ) {
if ( isset( $this->flat_rates[ $shipping_class ] ) ) { if ( isset( $this->flat_rates[ $shipping_class ] ) ) {
$cost += $this->flat_rates[ $shipping_class ]['cost']; $cost += $this->flat_rates[ $shipping_class ]['cost'];
$fee += $this->get_fee( $this->flat_rates[ $shipping_class ]['fee'], $class_price ); $fee += $this->get_fee( $this->flat_rates[ $shipping_class ]['fee'], $class_price );
} else { $matched = true;
// Class not set so we use default rate } elseif ( $this->cost !== '' ) {
// Class not set so we use default rate if its set
$cost += $this->cost; $cost += $this->cost;
$fee += $this->get_fee( $this->fee, $class_price ); $fee += $this->get_fee( $this->fee, $class_price );
$matched = true;
} }
} }
} }
// Total // Total
return $cost + $fee; if ( $matched )
return $cost + $fee;
else
return null;
} }
@ -364,6 +392,8 @@ class WC_Flat_Rate extends WC_Shipping_Method {
// Per item shipping so we pass an array of costs (per item) instead of a single value // Per item shipping so we pass an array of costs (per item) instead of a single value
$costs = array(); $costs = array();
$matched = false;
// Shipping per item // Shipping per item
foreach ( $package['contents'] as $item_id => $values ) { foreach ( $package['contents'] as $item_id => $values ) {
$_product = $values['data']; $_product = $values['data'];
@ -371,19 +401,26 @@ class WC_Flat_Rate extends WC_Shipping_Method {
if ( $values['quantity'] > 0 && $_product->needs_shipping() ) { if ( $values['quantity'] > 0 && $_product->needs_shipping() ) {
$shipping_class = $_product->get_shipping_class(); $shipping_class = $_product->get_shipping_class();
$fee = $cost = 0;
if ( isset( $this->flat_rates[ $shipping_class ] ) ) { if ( isset( $this->flat_rates[ $shipping_class ] ) ) {
$cost = $this->flat_rates[ $shipping_class ]['cost']; $cost = $this->flat_rates[ $shipping_class ]['cost'];
$fee = $this->get_fee( $this->flat_rates[ $shipping_class ]['fee'], $_product->get_price() ); $fee = $this->get_fee( $this->flat_rates[ $shipping_class ]['fee'], $_product->get_price() );
} else { $matched = true;
} elseif ( $this->cost !== '' ) {
$cost = $this->cost; $cost = $this->cost;
$fee = $this->get_fee( $this->fee, $_product->get_price() ); $fee = $this->get_fee( $this->fee, $_product->get_price() );
$matched = true;
} }
$costs[ $item_id ] = ( ( $cost + $fee ) * $values['quantity'] ); $costs[ $item_id ] = ( ( $cost + $fee ) * $values['quantity'] );
} }
} }
return $costs; if ( $matched )
return $costs;
else
return null;
} }

View File

@ -219,6 +219,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
* Tweak - Made armed forces 'states' under the US rather than in their own 'country'. * Tweak - Made armed forces 'states' under the US rather than in their own 'country'.
* Tweak - Extended woocommerce_update_options for flexibility. * Tweak - Extended woocommerce_update_options for flexibility.
* Tweak - Added disabled to settings API. * Tweak - Added disabled to settings API.
* Tweak - Flat rate shipping - if no rules match, and no default is set, don't return a rate.
* Fix - Added more error messages for coupons. * Fix - Added more error messages for coupons.
* Fix - Variation sku updating after selection. * Fix - Variation sku updating after selection.