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,36 +175,54 @@ class WC_Flat_Rate extends WC_Shipping_Method {
function calculate_shipping( $package = array() ) {
global $woocommerce;
$this->rates = array();
$cost_per_order = ( isset( $this->cost_per_order ) && ! empty( $this->cost_per_order ) ) ? $this->cost_per_order : 0;
$this->rates = array();
$cost_per_order = ( isset( $this->cost_per_order ) && ! empty( $this->cost_per_order ) ) ? $this->cost_per_order : 0;
if ( $this->type == 'order' ) {
$shipping_total = $this->order_shipping( $package );
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => $shipping_total + $cost_per_order,
);
if ( ! is_null( $shipping_total ) || $cost_per_order > 0 )
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => $shipping_total + $cost_per_order,
);
} elseif ( $this->type == 'class' ) {
$shipping_total = $this->class_shipping( $package );
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => $shipping_total + $cost_per_order,
);
if ( ! is_null( $shipping_total ) || $cost_per_order > 0 )
$rate = array(
'id' => $this->id,
'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(
'id' => $this->id,
'label' => $this->title,
'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
$this->add_rate( $rate );
@ -286,7 +304,7 @@ class WC_Flat_Rate extends WC_Shipping_Method {
}
} else {
// No matching classes so use defaults
if ( $this->cost > $cost ) {
if ( ! empty( $this->cost ) && $this->cost > $cost ) {
$cost = $this->cost;
$fee = $this->fee;
}
@ -295,11 +313,14 @@ class WC_Flat_Rate extends WC_Shipping_Method {
}
// Default rates
if ( is_null( $cost ) ) {
$cost = $this->cost;
$fee = $this->fee;
}
// Default rates if set
if ( is_null( $cost ) && $this->cost !== '' ) {
$cost = $this->cost;
$fee = $this->fee;
} elseif ( is_null( $cost ) ) {
// No match
return null;
}
// Shipping for whole order
return $cost + $this->get_fee( $fee, $package['contents_cost'] );
@ -334,22 +355,29 @@ class WC_Flat_Rate extends WC_Shipping_Method {
}
$found_shipping_classes = array_unique( $found_shipping_classes );
$matched = false;
// For each found class, add up the costs and fees
foreach ( $found_shipping_classes as $shipping_class => $class_price ) {
if ( isset( $this->flat_rates[ $shipping_class ] ) ) {
$cost += $this->flat_rates[ $shipping_class ]['cost'];
$fee += $this->get_fee( $this->flat_rates[ $shipping_class ]['fee'], $class_price );
} else {
// Class not set so we use default rate
$matched = true;
} elseif ( $this->cost !== '' ) {
// Class not set so we use default rate if its set
$cost += $this->cost;
$fee += $this->get_fee( $this->fee, $class_price );
$matched = true;
}
}
}
// Total
return $cost + $fee;
if ( $matched )
return $cost + $fee;
else
return null;
}
@ -363,6 +391,8 @@ class WC_Flat_Rate extends WC_Shipping_Method {
function item_shipping( $package ) {
// Per item shipping so we pass an array of costs (per item) instead of a single value
$costs = array();
$matched = false;
// Shipping per item
foreach ( $package['contents'] as $item_id => $values ) {
@ -370,20 +400,27 @@ class WC_Flat_Rate extends WC_Shipping_Method {
if ( $values['quantity'] > 0 && $_product->needs_shipping() ) {
$shipping_class = $_product->get_shipping_class();
$fee = $cost = 0;
if ( isset( $this->flat_rates[ $shipping_class ] ) ) {
$cost = $this->flat_rates[ $shipping_class ]['cost'];
$fee = $this->get_fee( $this->flat_rates[ $shipping_class ]['fee'], $_product->get_price() );
} else {
$matched = true;
} elseif ( $this->cost !== '' ) {
$cost = $this->cost;
$fee = $this->get_fee( $this->fee, $_product->get_price() );
$matched = true;
}
$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 - Extended woocommerce_update_options for flexibility.
* 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 - Variation sku updating after selection.