2017-07-18 04:43:31 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Discount calculation
|
|
|
|
*
|
|
|
|
* @author Automattic
|
|
|
|
* @package WooCommerce/Classes
|
|
|
|
* @version 3.2.0
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Discounts class.
|
|
|
|
*/
|
|
|
|
class WC_Discounts {
|
|
|
|
|
|
|
|
/**
|
2017-07-18 13:04:56 +00:00
|
|
|
* An array of items to discount.
|
2017-07-18 04:43:31 +00:00
|
|
|
*
|
2017-07-18 13:04:56 +00:00
|
|
|
* @var array
|
2017-07-18 04:43:31 +00:00
|
|
|
*/
|
2017-07-18 13:04:56 +00:00
|
|
|
protected $items = array();
|
2017-07-18 04:43:31 +00:00
|
|
|
|
|
|
|
/**
|
2017-07-18 13:04:56 +00:00
|
|
|
* Get items.
|
2017-07-18 04:43:31 +00:00
|
|
|
*
|
2017-07-18 13:04:56 +00:00
|
|
|
* @since 3.2.0
|
2017-07-18 14:42:46 +00:00
|
|
|
* @return object[]
|
2017-07-18 04:43:31 +00:00
|
|
|
*/
|
2017-07-18 13:04:56 +00:00
|
|
|
public function get_items() {
|
|
|
|
return $this->items;
|
2017-07-18 04:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-18 13:04:56 +00:00
|
|
|
* Set cart/order items which will be discounted.
|
2017-07-18 04:43:31 +00:00
|
|
|
*
|
2017-07-18 13:04:56 +00:00
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $raw_items
|
2017-07-18 04:43:31 +00:00
|
|
|
*/
|
2017-07-18 13:04:56 +00:00
|
|
|
public function set_items( $raw_items ) {
|
2017-07-18 14:42:46 +00:00
|
|
|
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.
|
|
|
|
);
|
2017-07-18 04:43:31 +00:00
|
|
|
|
2017-07-18 14:42:46 +00:00
|
|
|
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();
|
|
|
|
}
|
2017-07-18 04:43:31 +00:00
|
|
|
|
2017-07-18 14:42:46 +00:00
|
|
|
$this->items[] = $item;
|
2017-07-18 13:04:56 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-18 04:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-18 13:04:56 +00:00
|
|
|
* Get all discount totals.
|
2017-07-18 04:43:31 +00:00
|
|
|
*
|
2017-07-18 13:04:56 +00:00
|
|
|
* @since 3.2.0
|
|
|
|
* @return array
|
2017-07-18 04:43:31 +00:00
|
|
|
*/
|
2017-07-18 13:04:56 +00:00
|
|
|
public function get_discounts() {
|
|
|
|
return array(
|
|
|
|
'items',
|
|
|
|
'discount_totals' => array(
|
|
|
|
// 'code' => 'amount'
|
|
|
|
)
|
|
|
|
);
|
2017-07-18 04:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-18 13:04:56 +00:00
|
|
|
* Apply a discount to all items using a coupon.
|
2017-07-18 04:43:31 +00:00
|
|
|
*
|
2017-07-18 13:04:56 +00:00
|
|
|
* @since 3.2.0
|
|
|
|
* @param WC_Coupon $coupon
|
|
|
|
* @return bool True if applied.
|
2017-07-18 04:43:31 +00:00
|
|
|
*/
|
2017-07-18 13:04:56 +00:00
|
|
|
public function apply_discount( $coupon ) {
|
|
|
|
if ( ! is_a( $coupon, 'WC_Coupon' ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Do something to the items.
|
2017-07-18 04:43:31 +00:00
|
|
|
}
|
|
|
|
}
|