Fix undefined found shipping classes. Closes #2974.

This commit is contained in:
Mike Jolley 2013-04-16 13:01:55 +01:00
parent 0ad75a6d44
commit 65be3c67d8
2 changed files with 45 additions and 29 deletions

View File

@ -267,7 +267,7 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
switch ( $this_type ) { switch ( $this_type ) {
case 'class' : case 'class' :
$this_cost = $this_cost * $found_shipping_classes; $this_cost = $this_cost * sizeof( $this->find_shipping_classes( $package ) );
break; break;
case 'item' : case 'item' :
$this_cost = $this_cost * $total_quantity; $this_cost = $this_cost * $total_quantity;
@ -299,20 +299,10 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
if ( sizeof( $this->flat_rates ) > 0 ) { if ( sizeof( $this->flat_rates ) > 0 ) {
$found_shipping_classes = array(); $found_shipping_classes = $this->find_shipping_classes( $package );
// Find shipping classes for products in the cart
if ( sizeof( $package['contents'] ) > 0 ) {
foreach ( $package['contents'] as $item_id => $values ) {
if ( $values['data']->needs_shipping() )
$found_shipping_classes[] = $values['data']->get_shipping_class();
}
}
$found_shipping_classes = array_unique( $found_shipping_classes );
// Find most expensive class (if found) // Find most expensive class (if found)
foreach ( $found_shipping_classes as $shipping_class ) { foreach ( $found_shipping_classes as $shipping_class => $products ) {
if ( isset( $this->flat_rates[ $shipping_class ] ) ) { if ( isset( $this->flat_rates[ $shipping_class ] ) ) {
if ( $this->flat_rates[ $shipping_class ]['cost'] > $cost ) { if ( $this->flat_rates[ $shipping_class ]['cost'] > $cost ) {
$cost = $this->flat_rates[ $shipping_class ]['cost']; $cost = $this->flat_rates[ $shipping_class ]['cost'];
@ -354,28 +344,27 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
$cost = null; $cost = null;
$fee = null; $fee = null;
if ( sizeof( $this->flat_rates ) > 0 ) { if ( sizeof( $this->flat_rates ) > 0 || $this->cost !== '' ) {
$found_shipping_classes = array();
// Find shipping classes for products in the cart. Store prices too, so we can calc a fee for the class. // Find shipping classes for products in the cart.
if ( sizeof( $package['contents'] ) > 0 ) { $found_shipping_classes = $this->find_shipping_classes( $package );
foreach ( $package['contents'] as $item_id => $values ) {
if ( $values['data']->needs_shipping() ) {
if ( isset( $found_shipping_classes[ $values['data']->get_shipping_class() ] ) ) {
$found_shipping_classes[ $values['data']->get_shipping_class() ] = ( $values['data']->get_price() * $values['quantity'] ) + $found_shipping_classes[ $values['data']->get_shipping_class() ];
} else {
$found_shipping_classes[ $values['data']->get_shipping_class() ] = ( $values['data']->get_price() * $values['quantity'] );
}
}
}
}
$found_shipping_classes = array_unique( $found_shipping_classes ); // Store prices too, so we can calc a fee for the class.
$found_shipping_classes_values = array();
foreach ( $found_shipping_classes as $shipping_class => $products ) {
if ( ! isset( $found_shipping_classes_values[ $shipping_class ] ) )
$found_shipping_classes_values[ $shipping_class ] = 0;
foreach ( $products as $product ) {
$found_shipping_classes_values[ $shipping_class ] += $product['data']->get_price() * $product['quantity'];
}
}
$matched = false; $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_values 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 );
@ -439,6 +428,32 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
return null; return null;
} }
/**
* Finds and returns shipping classes and the products with said class.
*
* @access public
* @param mixed $package
* @return array
*/
public function find_shipping_classes( $package ) {
$found_shipping_classes = array();
// Find shipping classes for products in the cart
if ( sizeof( $package['contents'] ) > 0 ) {
foreach ( $package['contents'] as $item_id => $values ) {
if ( $values['data']->needs_shipping() ) {
$found_class = $values['data']->get_shipping_class();
if ( ! isset( $found_shipping_classes[ $found_class ] ) )
$found_shipping_classes[ $found_class ] = array();
$found_shipping_classes[ $found_class ][ $item_id ] = $values;
}
}
}
return $found_shipping_classes;
}
/** /**
* validate_additional_costs_field function. * validate_additional_costs_field function.
* *

View File

@ -173,6 +173,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
* Fix - checkmark after adding to cart multiple times. * Fix - checkmark after adding to cart multiple times.
* Fix - Saving text attributes. Posted 'text' terms are not slugs. Only striptags/slashes - don't change to slugs. * Fix - Saving text attributes. Posted 'text' terms are not slugs. Only striptags/slashes - don't change to slugs.
* Fix - Insert URL button when working with multiple variations. * Fix - Insert URL button when working with multiple variations.
* Fix - Undefined found_shipping_classes in flat rate shipping.
* Localization - Norwegian updates by Tore Hjartland * Localization - Norwegian updates by Tore Hjartland
* Localization - Spanish updates by Laguna Sanchez * Localization - Spanish updates by Laguna Sanchez