Merge pull request #16166 from woocommerce/feature/discounts-class-set-items

Feature/discounts class set items
This commit is contained in:
Claudio Sanches 2017-07-18 11:49:09 -03:00 committed by GitHub
commit b2757c1be4
4 changed files with 265 additions and 22 deletions

View File

@ -0,0 +1,218 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WC_Cart_Item
*
* Class object which represents an item in the cart.
*
* @version 3.2.0
* @package WooCommerce/Classes
* @category Class
* @author Automattic
*/
class WC_Cart_Item implements ArrayAccess {
/**
* Cart Data array.
*
* @var array
*/
protected $data = array(
'product_id' => 0,
'quantity' => 0,
'variation' => array(),
);
/**
* Product this item represents.
*
* @var WC_Product
*/
protected $product = null;
/**
* Constructor.
*
* @param array $data
*/
public function __construct( $data = array() ) {
$this->set_data( $data );
}
/**
* Gets price of the product.
* @return float
*/
public function get_price() {
return $this->get_product() ? $this->get_product()->get_price() : 0;
}
/**
* Gets price of the product.
* @return float
*/
public function get_weight() {
return $this->get_product() ? $this->get_product()->get_weight() : 0;
}
/**
* Gets price of the product.
* @return float
*/
public function get_tax_class() {
return $this->get_product() ? $this->get_product()->get_tax_class() : '';
}
/**
* Set product.
* @param int $value
*/
public function set_product( $value ) {
$this->product = $value;
$this->data['product_id'] = is_callable( array( $this->product, 'get_variation_id' ) ) ? $this->product->get_variation_id() : $this->product->get_id();
}
/**
* Get product object.
*
* @return WC_Product
*/
public function get_product() {
return ! is_null( $this->product ) ? $this->product : ( $this->product = wc_get_product( $this->get_product_id() ) );
}
/**
* Get all item data.
* @return array
*/
public function get_data() {
return $this->data;
}
/**
* Product or variation ID this item represents.
* @return int
*/
public function get_product_id() {
return $this->data['product_id'];
}
/**
* Get quantity in cart.
* @return int
*/
public function get_quantity() {
return $this->data['quantity'];
}
/**
* Get variation data.
* @return array
*/
public function get_variation() {
return $this->data['variation'];
}
/**
* Set product ID.
* @param int $value
*/
public function set_product_id( $value ) {
$this->data['product_id'] = absint( $value );
$this->product = null;
}
/**
* Set Quantity.
* @param int $value
*/
public function set_quantity( $value ) {
$this->data['quantity'] = wc_stock_amount( $value );
}
/**
* Set variation data.
* @param array $value
*/
public function set_variation( $value ) {
$this->data['variation'] = (array) $value;
}
/**
* Set all data.
* @param array $value
*/
public function set_data( $values ) {
if ( is_a( $values, 'WC_Cart_Item' ) ) {
$values = $values->get_data();
}
foreach ( $values as $key => $value ) {
if ( in_array( $key, array( 'quantity', 'product_id', 'variation', 'product' ) ) ) {
$this->{ "set_$key" }( $value );
} else {
$this->data[ $key ] = $value;
}
}
}
/**
* ArrayAccess/Backwards compatibility.
*
* @param string $offset
* @return mixed
*/
public function offsetGet( $offset ) {
switch ( $offset ) {
case 'data' :
return $this->get_product();
case 'variation_id' :
return is_callable( array( $this, 'get_variation_id' ) ) ? $this->get_product()->get_variation_id() : 0;
}
return isset( $this->data[ $offset ] ) ? $this->data[ $offset ] : '';
}
/**
* ArrayAccess/Backwards compatibility.
*
* @param string $offset
* @param mixed $value
*/
public function offsetSet( $offset, $value ) {
switch ( $offset ) {
case 'data' :
$this->set_product( $value );
break;
case 'variation_id' :
$this->set_product( wc_get_product( $value ) );
break;
default :
$this->data[ $offset ] = $value;
break;
}
}
/**
* ArrayAccess/Backwards compatibility.
*
* @param string $offset
* @return bool
*/
public function offsetExists( $offset ) {
if ( in_array( $offset, array( 'data' ) ) || isset( $this->data[ $offset ] ) ) {
return true;
}
return false;
}
/**
* ArrayAccess/Backwards compatibility.
*
* @param string $offset
*/
public function offsetUnset( $offset ) {
unset( $this->data[ $offset ] );
}
}

View File

@ -24,7 +24,7 @@ class WC_Discounts {
* Get items.
*
* @since 3.2.0
* @return array
* @return object[]
*/
public function get_items() {
return $this->items;
@ -35,27 +35,30 @@ class WC_Discounts {
*
* @since 3.2.0
* @param array $raw_items
* @todo Create https://github.com/woocommerce/woocommerce/pull/11889/files#diff-d9e4f5367e9d615985099b0d135629b8 class.
*/
public function set_items( $raw_items ) {
foreach ( $raw_items as $raw_item ) {
$item = array(
'price' => 0, // Unit price without discounts.
'qty' => 0, // Line qty.
'discount' => 0, // Total discounts to apply.
);
if ( ! empty( $raw_items ) && is_array( $raw_items ) ) {
foreach ( $raw_items as $raw_item ) {
$item = (object) array(
'price' => 0, // Unit price without discounts.
'quantity' => 0, // Line qty.
'discount' => 0, // Total discounts to apply.
);
if ( is_a( $raw_item, 'WC_Cart_Item' ) ) {
if ( is_a( $raw_item, 'WC_Cart_Item' ) ) {
$item->quantity = $raw_item->get_quantity();
$item->price = $raw_item->get_price();
} elseif ( is_a( $raw_item, 'WC_Order_Item_Product' ) ) {
$item->quantity = $raw_item->get_quantity();
$item->price = $raw_item->get_subtotal();
} else {
// @todo remove when we implement WC_Cart_Item. This is the old cart item schema.
$item->quantity = $raw_item['quantity'];
$item->price = $raw_item['data']->get_price();
}
} elseif ( is_a( $raw_item, 'WC_Order_Item_Product' ) ) {
} else {
// @todo remove when we implement WC_Cart_Item. This is the old cart item schema.
$item['qty'] = $raw_item['quantity'];
$item['price'] = $raw_item['data']->get_price();
$this->items[] = $item;
}
$this->items[] = $item;
}
}

View File

@ -404,6 +404,7 @@ final class WooCommerce {
include_once( WC_ABSPATH . 'includes/class-wc-frontend-scripts.php' ); // Frontend Scripts.
include_once( WC_ABSPATH . 'includes/class-wc-form-handler.php' ); // Form Handlers.
include_once( WC_ABSPATH . 'includes/class-wc-cart.php' ); // The main cart class.
include_once( WC_ABSPATH . 'includes/class-wc-cart-item.php' );
include_once( WC_ABSPATH . 'includes/class-wc-tax.php' ); // Tax class.
include_once( WC_ABSPATH . 'includes/class-wc-shipping-zones.php' ); // Shipping Zones class.
include_once( WC_ABSPATH . 'includes/class-wc-customer.php' ); // Customer class.

View File

@ -6,7 +6,9 @@
*/
class WC_Tests_Discounts extends WC_Unit_Test_Case {
/**
* Test get and set items.
*/
public function test_get_set_items() {
// We need this to have the calculate_totals() method calculate totals
if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) {
@ -16,15 +18,34 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case {
// Create dummy product - price will be 10
$product = WC_Helper_Product::create_simple_product();
// Add product to cart x1, calc and test
// Add product to the cart.
WC()->cart->add_to_cart( $product->get_id(), 1 );
WC()->cart->calculate_totals();
// Tests.
// Add product to a dummy order.
$order = new WC_Order();
$order->add_product( $product, 4 );
$order->calculate_totals();
$order->save();
// Test setting items to the cart.
$discounts = new WC_Discounts();
$discounts->set_items( WC()->cart->get_cart() );
$this->assertEquals( array( (object) array( 'price' => '10', 'quantity' => 1, 'discount' => 0 ) ), $discounts->get_items() );
$this->assertEquals( array( array( 'price' => '10', 'qty' => 1, 'discount' => 0 ) ), $discounts->get_items() );
// Test setting items to an order.
$discounts = new WC_Discounts();
$discounts->set_items( $order->get_items() );
$this->assertEquals( array( (object) array( 'price' => '40', 'quantity' => 4, 'discount' => 0 ) ), $discounts->get_items() );
// Empty array of items.
$discounts = new WC_Discounts();
$discounts->set_items( array() );
$this->assertEquals( array(), $discounts->get_items() );
// Invalid items.
$discounts = new WC_Discounts();
$discounts->set_items( false );
$this->assertEquals( array(), $discounts->get_items() );
// Cleanup.
WC()->cart->empty_cart();