Changes to allow shipping to be calculated from totals class

Moved some items from
https://github.com/woocommerce/woocommerce/pull/11889/files to support
this.
This commit is contained in:
Mike Jolley 2017-07-25 15:11:32 +01:00
parent e8e200195f
commit 4c4f26ff41
6 changed files with 208 additions and 135 deletions

View File

@ -18,6 +18,12 @@ if ( ! defined( 'ABSPATH' ) ) {
*/
class WC_Cart {
/**
* This stores the chosen shipping methods for the cart item packages.
* @var array
*/
protected $shipping_methods;
/** @var array Contains an array of cart items. */
public $cart_contents = array();
@ -295,6 +301,7 @@ class WC_Cart {
*/
public function empty_cart( $clear_persistent_cart = true ) {
$this->cart_contents = array();
$this->shipping_methods = null;
$this->reset( true );
unset( WC()->session->order_awaiting_payment, WC()->session->applied_coupons, WC()->session->coupon_discount_amounts, WC()->session->coupon_discount_tax_amounts, WC()->session->cart );
@ -1452,15 +1459,32 @@ class WC_Cart {
* Uses the shipping class to calculate shipping then gets the totals when its finished.
*/
public function calculate_shipping() {
if ( $this->needs_shipping() && $this->show_shipping() ) {
WC()->shipping->calculate_shipping( $this->get_shipping_packages() );
} else {
WC()->shipping->reset_shipping();
}
$this->shipping_methods = $this->needs_shipping() ? $this->get_chosen_shipping_methods( WC()->shipping->calculate_shipping( $this->get_shipping_packages() ) ) : array();
// Get totals for the chosen shipping method
$this->shipping_total = WC()->shipping->shipping_total; // Shipping Total
$this->shipping_taxes = WC()->shipping->shipping_taxes; // Shipping Taxes
// Set legacy totals for backwards compatibility with versions prior to 3.2.
$this->shipping_total = WC()->shipping->shipping_total = array_sum( wp_list_pluck( $this->shipping_methods, 'cost' ) );
$this->shipping_taxes = WC()->shipping->shipping_taxes = wp_list_pluck( $this->shipping_methods, 'taxes' );
return $this->shipping_methods;
}
/**
* Given a set of packages with rates, get the chosen ones only.
*
* @since 3.2.0
* @param array $calculated_shipping_packages
* @return array
*/
protected function get_chosen_shipping_methods( $calculated_shipping_packages = array() ) {
$chosen_methods = array();
// Get chosen methods for each package to get our totals.
foreach ( $calculated_shipping_packages as $key => $package ) {
$chosen_method = wc_get_chosen_shipping_method_for_package( $key, $package );
if ( $chosen_method ) {
$chosen_methods[ $key ] = $package['rates'][ $chosen_method ];
}
}
return $chosen_methods;
}
/**

View File

@ -140,7 +140,7 @@ class WC_Discounts {
* @todo is_valid_for_product accepts values - how can we deal with that?
*
* @since 3.2.0
* @param WC_Coupon $coupon
* @param WC_Coupon $coupon Coupon object being applied to the items.
* @return bool True if applied.
*/
public function apply_coupon( $coupon ) {

View File

@ -26,12 +26,6 @@ class WC_Shipping {
/** @var array|null Stores methods loaded into woocommerce. */
public $shipping_methods = null;
/** @var float Stores the cost of shipping */
public $shipping_total = 0;
/** @var array Stores an array of shipping taxes. */
public $shipping_taxes = array();
/** @var array Stores the shipping classes. */
public $shipping_classes = array();
@ -237,18 +231,17 @@ class WC_Shipping {
/**
* Calculate shipping for (multiple) packages of cart items.
*
* @param array $packages multi-dimensional array of cart items to calc shipping for
* @param array $packages multi-dimensional array of cart items to calc shipping for.
* @return array Array of calculated packages.
*/
public function calculate_shipping( $packages = array() ) {
$this->shipping_total = 0;
$this->shipping_taxes = array();
$this->packages = array();
$this->packages = array();
if ( ! $this->enabled || empty( $packages ) ) {
return;
return array();
}
// Calculate costs for passed packages
// Calculate costs for passed packages.
foreach ( $packages as $package_key => $package ) {
$this->packages[ $package_key ] = $this->calculate_shipping_for_package( $package, $package_key );
}
@ -264,58 +257,9 @@ class WC_Shipping {
*
* @param array $packages The array of packages after shipping costs are calculated.
*/
$this->packages = apply_filters( 'woocommerce_shipping_packages', $this->packages );
$this->packages = array_filter( (array) apply_filters( 'woocommerce_shipping_packages', $this->packages ) );
if ( ! is_array( $this->packages ) || empty( $this->packages ) ) {
return;
}
// Get all chosen methods
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$method_counts = WC()->session->get( 'shipping_method_counts' );
// Get chosen methods for each package
foreach ( $this->packages as $i => $package ) {
$chosen_method = false;
$method_count = false;
if ( ! empty( $chosen_methods[ $i ] ) ) {
$chosen_method = $chosen_methods[ $i ];
}
if ( ! empty( $method_counts[ $i ] ) ) {
$method_count = absint( $method_counts[ $i ] );
}
if ( sizeof( $package['rates'] ) > 0 ) {
// If not set, not available, or available methods have changed, set to the DEFAULT option
if ( empty( $chosen_method ) || ! isset( $package['rates'][ $chosen_method ] ) || sizeof( $package['rates'] ) !== $method_count ) {
$chosen_method = apply_filters( 'woocommerce_shipping_chosen_method', $this->get_default_method( $package['rates'], false ), $package['rates'], $chosen_method );
$chosen_methods[ $i ] = $chosen_method;
$method_counts[ $i ] = sizeof( $package['rates'] );
do_action( 'woocommerce_shipping_method_chosen', $chosen_method );
}
// Store total costs
if ( $chosen_method && isset( $package['rates'][ $chosen_method ] ) ) {
$rate = $package['rates'][ $chosen_method ];
// Merge cost and taxes - label and ID will be the same
$this->shipping_total += $rate->cost;
if ( ! empty( $rate->taxes ) && is_array( $rate->taxes ) ) {
foreach ( array_keys( $this->shipping_taxes + $rate->taxes ) as $key ) {
$this->shipping_taxes[ $key ] = ( isset( $rate->taxes[ $key ] ) ? $rate->taxes[ $key ] : 0 ) + ( isset( $this->shipping_taxes[ $key ] ) ? $this->shipping_taxes[ $key ] : 0 );
}
}
}
}
}
// Save all chosen methods (array)
WC()->session->set( 'chosen_shipping_methods', $chosen_methods );
WC()->session->set( 'shipping_method_counts', $method_counts );
return $this->packages;
}
/**
@ -396,8 +340,6 @@ class WC_Shipping {
*/
public function reset_shipping() {
unset( WC()->session->chosen_shipping_methods );
$this->shipping_total = 0;
$this->shipping_taxes = array();
$this->packages = array();
}

View File

@ -49,6 +49,14 @@ class WC_Totals {
*/
private $fees = array();
/**
* Shipping costs.
*
* @since 3.2.0
* @var array
*/
private $shipping = array();
/**
* Discount amounts in cents after calculation for the cart.
*
@ -112,13 +120,31 @@ class WC_Totals {
$item = $this->get_default_item_props();
$item->key = $cart_item_key;
$item->quantity = $cart_item['quantity'];
$item->price = $cart_item['data']->get_price() * $this->precision * $cart_item['quantity'];
$item->price = $this->add_precision( $cart_item['data']->get_price() ) * $cart_item['quantity'];
$item->product = $cart_item['data'];
$this->items[ $cart_item_key ] = $item;
}
}
}
/**
* Add precision (deep) to a price.
*
* @since 3.2.0
* @param int|array $value Value to remove precision from.
* @return float
*/
private function add_precision( $value ) {
if ( is_array( $value ) ) {
foreach ( $value as $key => $subvalue ) {
$value[ $key ] = $this->add_precision( $subvalue );
}
} else {
$value = $value * $this->precision;
}
return $value;
}
/**
* Remove precision (deep) from a price.
*
@ -173,6 +199,20 @@ class WC_Totals {
);
}
/**
* Get default blank set of props used per shipping row.
*
* @since 3.2.0
* @return array
*/
private function get_default_shipping_props() {
return (object) array(
'total' => 0,
'total_tax' => 0,
'taxes' => array(),
);
}
/**
* Only ran if woocommerce_adjust_non_base_location_prices is true.
*
@ -232,27 +272,6 @@ class WC_Totals {
}
}
/**
* Return array of shipping costs.
*
* @since 3.2.0
* @return array
*/
private function get_shipping() {
// @todo get this somehow. Where does calc occur?
return array();
}
/**
* Get discounts.
*
* @return array
*/
private function get_discounts() {
// @todo fee style API for discounts in cart/checkout.
return array();
}
/**
* Get a single total with or without precision (in cents).
*
@ -291,33 +310,25 @@ class WC_Totals {
/**
* Get all tax rows from items (including shipping and product line items).
*
* @todo consider an item object instead of array here
*
* @since 3.2.0
* @return array
*/
private function get_merged_taxes() {
$taxes = array();
foreach ( array_merge( $this->items, $this->fees, $this->get_shipping() ) as $item ) {
foreach ( array_merge( $this->items, $this->fees, $this->shipping ) as $item ) {
foreach ( $item->taxes as $rate_id => $rate ) {
$taxes[ $rate_id ] = array( 'tax_total' => 0, 'shipping_tax_total' => 0 );
}
}
foreach ( $this->items as $item ) {
foreach ( $this->items + $this->fees as $item ) {
foreach ( $item->taxes as $rate_id => $rate ) {
$taxes[ $rate_id ]['tax_total'] = $taxes[ $rate_id ]['tax_total'] + $rate;
}
}
foreach ( $this->fees as $fee ) {
foreach ( $fee->taxes as $rate_id => $rate ) {
$taxes[ $rate_id ]['tax_total'] = $taxes[ $rate_id ]['tax_total'] + $rate;
}
}
foreach ( $this->get_shipping() as $item ) {
foreach ( $this->shipping as $item ) {
foreach ( $item->taxes as $rate_id => $rate ) {
$taxes[ $rate_id ]['shipping_tax_total'] = $taxes[ $rate_id ]['shipping_tax_total'] + $rate;
}
@ -385,6 +396,7 @@ class WC_Totals {
* Calculate all discount and coupon amounts.
*
* @todo Manual discounts.
* @todo record coupon totals and counts for cart.
*
* @since 3.2.0
* @uses WC_Discounts class.
@ -398,11 +410,18 @@ class WC_Totals {
$this->discount_totals = $discounts->get_discounts();
$this->totals['discounts_total'] = array_sum( $this->discount_totals );
// @todo $this->totals['discounts_tax_total'] = $value;
/*$this->set_coupon_totals( wp_list_pluck( $this->coupons, 'total' ) );
//$this->set_coupon_tax_totals( wp_list_pluck( $this->coupons, 'total_tax' ) );
//$this->set_coupon_counts( wp_list_pluck( $this->coupons, 'count' ) );*/
// See how much tax was 'discounted'.
if ( wc_tax_enabled() ) {
foreach ( $this->discount_totals as $cart_item_key => $discount ) {
$item = $this->items[ $cart_item_key ];
if ( $item->product->is_taxable() ) {
$tax_rates = $this->get_item_tax_rates( $item );
$taxes = WC_Tax::calc_tax( $discount, $tax_rates, false );
$this->totals['discounts_tax_total'] += array_sum( $taxes );
}
}
}
}
/**
@ -442,12 +461,13 @@ class WC_Totals {
* @todo logic is unqiue to carts.
*/
private function calculate_fee_totals() {
$this->fees = array();
$this->object->calculate_fees();
foreach ( $this->object->get_fees() as $fee_key => $fee_object ) {
$fee = $this->get_default_fee_props();
$fee->object = $fee_object;
$fee->total = $fee->object->amount * $this->precision;
$fee->total = $this->add_precision( $fee->object->amount );
if ( wc_tax_enabled() && $fee->object->taxable ) {
$fee->taxes = WC_Tax::calc_tax( $fee->total, WC_Tax::get_rates( $fee->object->tax_class ), false );
@ -475,7 +495,18 @@ class WC_Totals {
* @since 3.2.0
*/
private function calculate_shipping_totals() {
//$this->set_shipping_total( array_sum( array_values( wp_list_pluck( $this->shipping, 'total' ) ) ) );
$this->shipping = array();
foreach ( $this->object->calculate_shipping() as $key => $shipping_object ) {
$shipping_line = $this->get_default_shipping_props();
$shipping_line->total = $this->add_precision( $shipping_object->cost );
$shipping_line->taxes = array_map( array( $this, 'add_precision' ), $shipping_object->taxes );
$shipping_line->total_tax = array_sum( $shipping_object->taxes );
$this->shipping[ $key ] = $shipping_line;
}
$this->set_total( 'shipping_total', array_sum( wp_list_pluck( $this->shipping, 'total' ) ) );
$this->set_total( 'shipping_tax_total', array_sum( wp_list_pluck( $this->shipping, 'total_tax' ) ) );
}
/**

View File

@ -388,3 +388,69 @@ function wc_get_chosen_shipping_method_ids() {
}
return $method_ids;
}
/**
* Get chosen method for package from session.
*
* @since 3.2.0
* @param int $key
* @param array $package
* @return string|bool
*/
function wc_get_chosen_shipping_method_for_package( $key, $package ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_method = isset( $chosen_methods[ $key ] ) ? $chosen_methods[ $key ] : false;
$changed = wc_shipping_methods_have_changed( $key, $package );
// If not set, not available, or available methods have changed, set to the DEFAULT option
if ( ! $chosen_method || $changed || ! isset( $package['rates'][ $chosen_method ] ) ) {
$chosen_method = wc_get_default_shipping_method_for_package( $key, $package, $chosen_method );
$chosen_methods[ $key ] = $chosen_method;
WC()->session->set( 'chosen_shipping_methods', $chosen_methods );
do_action( 'woocommerce_shipping_method_chosen', $chosen_method );
}
return $chosen_method;
}
/**
* Choose the default method for a package.
*
* @since 3.2.0
* @param string $key
* @param array $package
* @return string
*/
function wc_get_default_shipping_method_for_package( $key, $package, $chosen_method ) {
$rate_keys = array_keys( $package['rates'] );
$default = current( $rate_keys );
$coupons = WC()->cart->get_coupons();
foreach ( $coupons as $coupon ) {
if ( $coupon->get_free_shipping() ) {
foreach ( $rate_keys as $rate_key ) {
if ( 0 === stripos( $rate_key, 'free_shipping' ) ) {
$default = $rate_key;
break;
}
}
break;
}
}
return apply_filters( 'woocommerce_shipping_chosen_method', $default, $package['rates'], $chosen_method );
}
/**
* See if the methods have changed since the last request.
*
* @since 3.2.0
* @param int $key
* @param array $package
* @return bool
*/
function wc_shipping_methods_have_changed( $key, $package ) {
// Lookup previous methods from session.
$previous_shipping_methods = WC()->session->get( 'previous_shipping_methods' );
// Get new and old rates.
$new_rates = array_keys( $package['rates'] );
$prev_rates = isset( $previous_shipping_methods[ $key ] ) ? $previous_shipping_methods[ $key ] : false;
// Update session.
$previous_shipping_methods[ $key ] = $new_rates;
WC()->session->set( 'previous_shipping_methods', $previous_shipping_methods );
return $new_rates != $prev_rates;
}

View File

@ -1,18 +1,28 @@
<?php
/**
* Tests for the totals class.
*
* @package WooCommerce\Tests\Discounts
*/
/**
* WC_Tests_Totals
*/
class WC_Tests_Totals extends WC_Unit_Test_Case {
// Totals class for getter tests.
/**
* Totals class for getter tests.
*
* @var object
*/
protected $totals;
// ID tracking for cleanup.
protected $products = array();
protected $coupons = array();
protected $tax_rate_ids = array();
/**
* ID tracking for cleanup.
*
* @var array
*/
protected $ids = array();
/**
* Setup the cart for totals calculation.
@ -44,10 +54,10 @@ class WC_Tests_Totals extends WC_Unit_Test_Case {
$coupon->set_discount_type( 'percent' );
$coupon->save();
$this->tax_rate_ids[] = $tax_rate_id;
$this->products[] = $product;
$this->products[] = $product2;
$this->coupons[] = $coupon;
$this->ids['tax_rate_ids'][] = $tax_rate_id;
$this->ids['products'][] = $product;
$this->ids['products'][] = $product2;
$this->ids['coupons'][] = $coupon;
WC()->cart->add_to_cart( $product->get_id(), 1 );
WC()->cart->add_to_cart( $product2->get_id(), 2 );
@ -63,9 +73,9 @@ class WC_Tests_Totals extends WC_Unit_Test_Case {
* Add fees when the fees API is called.
*/
public function add_cart_fees_callback() {
WC()->cart->add_fee( "test fee", 10, true );
WC()->cart->add_fee( "test fee 2", 20, true );
WC()->cart->add_fee( "test fee non-taxable", 10, false );
WC()->cart->add_fee( 'test fee', 10, true );
WC()->cart->add_fee( 'test fee 2', 20, true );
WC()->cart->add_fee( 'test fee non-taxable', 10, false );
}
/**
@ -78,15 +88,15 @@ class WC_Tests_Totals extends WC_Unit_Test_Case {
update_option( 'woocommerce_calc_taxes', 'no' );
remove_action( 'woocommerce_cart_calculate_fees', array( $this, 'add_cart_fees_callback' ) );
foreach ( $this->products as $product ) {
foreach ( $this->ids['products'] as $product ) {
$product->delete( true );
}
foreach ( $this->coupons as $coupon ) {
foreach ( $this->ids['coupons'] as $coupon ) {
$coupon->delete( true );
}
foreach ( $this->tax_rate_ids as $tax_rate_id ) {
foreach ( $this->ids['tax_rate_ids'] as $tax_rate_id ) {
WC_Tax::_delete_tax_rate( $tax_rate_id );
}
}
@ -102,18 +112,18 @@ class WC_Tests_Totals extends WC_Unit_Test_Case {
'items_subtotal_tax' => 6.00,
'items_total' => 27.00,
'items_total_tax' => 5.40,
'total' => 78.40,
'total' => 90.40,
'taxes' => array(
1 => array(
'tax_total' => 11.40,
'shipping_tax_total' => 0.00,
)
'shipping_tax_total' => 2.00,
),
),
'tax_total' => 11.40,
'shipping_total' => 0, // @todo ?
'shipping_tax_total' => 0, // @todo ?
'shipping_total' => 10,
'shipping_tax_total' => 2,
'discounts_total' => 3.00,
'discounts_tax_total' => 0, // @todo ?
'discounts_tax_total' => 0.60,
), $this->totals->get_totals() );
}
}