2017-07-20 19:50:09 +00:00
|
|
|
<?php
|
2017-07-21 13:22:40 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2017-07-20 19:50:09 +00:00
|
|
|
/**
|
2017-07-28 12:02:39 +00:00
|
|
|
* A discount.
|
2017-07-20 19:50:09 +00:00
|
|
|
*
|
2017-07-28 12:02:39 +00:00
|
|
|
* Represents a fixed, percent or coupon based discount calculated by WC_Discounts class.
|
2017-07-20 19:50:09 +00:00
|
|
|
*
|
|
|
|
* @author Automattic
|
|
|
|
* @package WooCommerce/Classes
|
|
|
|
* @version 3.2.0
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
2017-07-21 13:22:40 +00:00
|
|
|
class WC_Discount extends WC_Data {
|
2017-07-20 19:50:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Data array, with defaults.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $data = array(
|
2017-07-28 12:02:39 +00:00
|
|
|
'coupon_code' => '',
|
|
|
|
'discounts' => array(), // Array of discounts. Keys for item ids, values for the discount amount.
|
2017-07-21 13:22:40 +00:00
|
|
|
'amount' => 0,
|
|
|
|
'discount' => 0,
|
2017-07-28 12:02:39 +00:00
|
|
|
'type' => 'fixed',
|
2017-07-20 19:50:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
2017-07-21 13:22:40 +00:00
|
|
|
* Checks the coupon type.
|
|
|
|
* @param string $type Array or string of types
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function is_type( $type ) {
|
|
|
|
return ( $this->get_discount_type() === $type || ( is_array( $type ) && in_array( $this->get_discount_type(), $type ) ) );
|
|
|
|
}
|
|
|
|
|
2017-07-28 12:02:39 +00:00
|
|
|
/**
|
|
|
|
* Valid discount types.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function get_valid_discount_types() {
|
|
|
|
return array( 'fixed', 'percent', 'coupon' );
|
|
|
|
}
|
|
|
|
|
2017-07-21 13:22:40 +00:00
|
|
|
/**
|
|
|
|
* Prefix for action and filter hooks on data.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function get_hook_prefix() {
|
|
|
|
return 'woocommerce_discount_get_';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the ID of this dicount.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_id() {
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Discount ID.
|
2017-07-20 19:50:09 +00:00
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
*/
|
|
|
|
public function set_id( $id ) {
|
2017-07-21 13:22:40 +00:00
|
|
|
$this->id = $id;
|
|
|
|
}
|
2017-07-20 19:50:09 +00:00
|
|
|
|
2017-07-21 13:22:40 +00:00
|
|
|
/**
|
|
|
|
* Get discount amount.
|
|
|
|
*
|
|
|
|
* @param string $context
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function get_amount( $context = 'view' ) {
|
|
|
|
return $this->get_prop( 'amount', $context );
|
2017-07-20 19:50:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Discount amount - either fixed or percentage.
|
|
|
|
*
|
2017-07-21 13:22:40 +00:00
|
|
|
* @param string $raw_amount
|
2017-07-20 19:50:09 +00:00
|
|
|
*/
|
2017-07-21 13:22:40 +00:00
|
|
|
public function set_amount( $raw_amount ) {
|
|
|
|
if ( strstr( $raw_amount, '%' ) ) {
|
|
|
|
$amount = absint( rtrim( $raw_amount, '%' ) );
|
|
|
|
$this->set_prop( 'amount', $amount );
|
|
|
|
$this->set_discount_type( 'percent' );
|
|
|
|
} else {
|
2017-07-27 18:08:22 +00:00
|
|
|
$this->set_prop( 'amount', wc_format_decimal( $raw_amount ) );
|
2017-07-21 13:22:40 +00:00
|
|
|
}
|
2017-07-20 19:50:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-21 13:22:40 +00:00
|
|
|
* Get discount type.
|
|
|
|
*
|
|
|
|
* @param string $context
|
|
|
|
* @return string
|
2017-07-20 19:50:09 +00:00
|
|
|
*/
|
2017-07-28 12:02:39 +00:00
|
|
|
public function get_type( $context = 'view' ) {
|
|
|
|
return $this->get_prop( 'type', $context );
|
2017-07-21 13:22:40 +00:00
|
|
|
}
|
2017-07-20 19:50:09 +00:00
|
|
|
|
2017-07-21 13:22:40 +00:00
|
|
|
/**
|
|
|
|
* Set discount type.
|
|
|
|
*
|
|
|
|
* @param string $discount_type
|
|
|
|
* @throws WC_Data_Exception
|
|
|
|
*/
|
2017-07-28 12:02:39 +00:00
|
|
|
public function set_type( $discount_type ) {
|
|
|
|
if ( ! in_array( $discount_type, $this->get_valid_discount_types() ) ) {
|
|
|
|
$this->error( 'invalid_discount_type', __( 'Invalid discount type', 'woocommerce' ) );
|
2017-07-21 13:22:40 +00:00
|
|
|
}
|
2017-07-28 12:02:39 +00:00
|
|
|
$this->set_prop( 'type', $discount_type );
|
2017-07-20 19:50:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-27 21:08:38 +00:00
|
|
|
* Amount of discount this has given in total cents.
|
|
|
|
*
|
|
|
|
* @param int $total
|
2017-07-20 19:50:09 +00:00
|
|
|
*/
|
2017-07-27 21:08:38 +00:00
|
|
|
public function set_discount_total( $total ) {
|
|
|
|
$this->set_prop( 'discount', absint( $total ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get amount of discount this has in cents.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function get_discount_total( $context = 'view' ) {
|
|
|
|
return $this->get_prop( 'discount', $context );
|
|
|
|
}
|
2017-07-20 19:50:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the amount of negative tax to apply for this discount, since
|
|
|
|
* discounts are applied before tax.
|
|
|
|
*
|
|
|
|
* For percent discounts this is simply a percent of each cart item's tax.
|
|
|
|
*
|
|
|
|
* For fixed discounts, the taxes are calculated proportionally so the
|
|
|
|
* discount is fairly split between items.
|
|
|
|
*
|
2017-07-21 13:22:40 +00:00
|
|
|
* @todo Should this bere here?
|
2017-07-20 19:50:09 +00:00
|
|
|
*/
|
2017-07-21 13:22:40 +00:00
|
|
|
public function calculate_negative_taxes() {}
|
2017-07-20 19:50:09 +00:00
|
|
|
|
|
|
|
}
|