From 31d535c932d9f23e25e48b5f4371e3fbd0a30e3e Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 18 Jul 2017 14:04:56 +0100 Subject: [PATCH] Methods and tests --- includes/class-wc-discounts.php | 179 ++++++++--------------- includes/class-woocommerce.php | 1 + tests/unit-tests/discounts/discounts.php | 34 +++++ 3 files changed, 93 insertions(+), 121 deletions(-) create mode 100644 tests/unit-tests/discounts/discounts.php diff --git a/includes/class-wc-discounts.php b/includes/class-wc-discounts.php index 14a7bd5a006..7738dc0197c 100644 --- a/includes/class-wc-discounts.php +++ b/includes/class-wc-discounts.php @@ -14,140 +14,77 @@ class WC_Discounts { /** - * Display name. + * An array of items to discount. * - * @var string + * @var array */ - protected $name = ''; + protected $items = array(); /** - * Amount of discount. - * - * @var float - */ - protected $amount = 0; - - /** - * Type of discount. - * - * @var string - */ - protected $type = 'percent'; - - /** - * Allow or not accumulate with other discounts. - * - * @var bool - */ - protected $individual_use = false; - - /** - * Coupon ID. - * - * @var int - */ - protected $coupon_id = 0; - - /** - * Get available discount types. + * Get items. * + * @since 3.2.0 * @return array */ - public function get_available_types() { + public function get_items() { + return $this->items; + } + + /** + * Set cart/order items which will be discounted. + * + * @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 ( is_a( $raw_item, 'WC_Cart_Item' ) ) { + + } 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'] = $values['quantity']; + $item['price'] = $values['data']->get_price(); + } + + $this->items[] = $item; + } + } + + /** + * Get all discount totals. + * + * @since 3.2.0 + * @return array + */ + public function get_discounts() { return array( - 'percent', - 'fixed_cart', - 'fixed_product', + 'items', + 'discount_totals' => array( + // 'code' => 'amount' + ) ); } /** - * Set name. + * Apply a discount to all items using a coupon. * - * @param string $name New name. + * @since 3.2.0 + * @param WC_Coupon $coupon + * @return bool True if applied. */ - public function set_name( $name ) { - $this->name = $name; - } - - /** - * Set amount. - * - * @param float $amount Total discount amount. - */ - public function set_amount( $amount ) { - $this->amount = $amount; - } - - /** - * Set type. - * - * @param string $type Type of discount. - */ - public function set_type( $type ) { - $this->type = in_array( $type, $this->get_available_types(), true ) ? $type : 'percent'; - } - - /** - * Set individual use. - * - * @param bool $individual_use Allow individual use. - */ - public function set_individual_use( $individual_use ) { - $this->individual_use = $individual_use; - } - - /** - * Set coupon ID. - * - * @param int $coupon_id Coupon ID. - */ - public function set_coupon_id( $coupon_id ) { - $this->coupon_id = $coupon_id; - } - - /** - * Get name. - * - * @return string. - */ - public function get_name() { - return $this->name; - } - - /** - * Get amount. - * - * @return float - */ - public function get_amount() { - return $this->amount; - } - - /** - * Get type. - * - * @return string - */ - public function get_type() { - return $this->type; - } - - /** - * Get individual use. - * - * @return bool - */ - public function get_individual_use() { - return $this->individual_use; - } - - /** - * Get coupon ID. - * - * @return int - */ - public function get_coupon_id() { - return $this->coupon_id; + public function apply_discount( $coupon ) { + if ( ! is_a( $coupon, 'WC_Coupon' ) ) { + return false; + } + // Do something to the items. } } diff --git a/includes/class-woocommerce.php b/includes/class-woocommerce.php index fea2e8a57d7..3b2fdbe3ec7 100644 --- a/includes/class-woocommerce.php +++ b/includes/class-woocommerce.php @@ -333,6 +333,7 @@ final class WooCommerce { include_once( WC_ABSPATH . 'includes/class-wc-deprecated-action-hooks.php' ); include_once( WC_ABSPATH . 'includes/class-wc-deprecated-filter-hooks.php' ); include_once( WC_ABSPATH . 'includes/class-wc-background-emailer.php' ); + include_once( WC_ABSPATH . 'includes/class-wc-discounts.php' ); /** * Data stores - used to store and retrieve CRUD object data from the database. diff --git a/tests/unit-tests/discounts/discounts.php b/tests/unit-tests/discounts/discounts.php new file mode 100644 index 00000000000..4a2f680a772 --- /dev/null +++ b/tests/unit-tests/discounts/discounts.php @@ -0,0 +1,34 @@ +cart->add_to_cart( $product->get_id(), 1 ); + WC()->cart->calculate_totals(); + + // Tests. + $discounts = new WC_Discounts(); + $discounts->set_items( WC()->cart->get_cart() ); + + $this->assertEquals( array( array( 'price' => '10', 'qty' => 1, 'discount' => 0 ) ), $discounts->get_items() ); + + // Cleanup. + WC()->cart->empty_cart(); + WC()->cart->remove_coupons(); + WC_Helper_Product::delete_product( $product->get_id() ); + } +}