woocommerce/includes/class-wc-discount.php

147 lines
3.2 KiB
PHP
Raw Normal View History

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
/**
* A single discount.
*
* Represents a fixed or percent based discount at cart level. Extended by cart
* and order discounts since they share the same logic for things like tax
* calculation.
*
* @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-21 13:22:40 +00:00
'amount' => 0,
'discount' => 0,
'discount_type' => 'fixed_cart',
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 ) ) );
}
/**
* 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 {
$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-21 13:22:40 +00:00
public function get_discount_type( $context = 'view' ) {
return $this->get_prop( 'discount_type', $context );
}
2017-07-20 19:50:09 +00:00
public function get_discount_amount( $total ) {
if ( 'percent' === $this->get_discount_type() ) {
return $this->get_amount() * ( $total / 100 );
} else {
return min( absint( wc_add_number_precision( $this->get_amount() ) ), $total );
}
}
2017-07-21 13:22:40 +00:00
/**
* Set discount type.
*
* @param string $discount_type
* @throws WC_Data_Exception
*/
public function set_discount_type( $discount_type ) {
if ( ! in_array( $discount_type, array( 'percent', 'fixed_cart' ) ) ) {
2017-07-21 13:22:40 +00:00
$this->error( 'coupon_invalid_discount_type', __( 'Invalid discount type', 'woocommerce' ) );
}
$this->set_prop( 'discount_type', $discount_type );
2017-07-20 19:50:09 +00:00
}
/**
2017-07-21 13:22:40 +00:00
* Amount of discount this has given in total. @todo should this be here?
2017-07-20 19:50:09 +00:00
*/
2017-07-21 13:22:40 +00:00
public function set_discount_total() {}
2017-07-20 19:50:09 +00:00
2017-07-21 13:22:40 +00:00
/**
* Array of negative taxes. @todo should this be here?
*/
public function set_taxes() {}
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
}