woocommerce/includes/class-wc-order-item-discoun...

188 lines
4.4 KiB
PHP
Raw Normal View History

2017-08-09 15:16:44 +00:00
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Order Line Item (discount).
*
* @version 3.2.0
* @since 3.2.0
* @package WooCommerce/Classes
* @author WooCommerce
*/
class WC_Order_Item_Discount extends WC_Order_Item {
/**
* Data array.
*
* @var array
*/
protected $extra_data = array(
2017-08-09 17:53:06 +00:00
'amount' => 0, // Discount amount.
'discount_type' => 'fixed', // Fixed or percent type.
'total' => '',
'total_tax' => '',
'taxes' => array(
'total' => array(),
),
2017-08-09 15:16:44 +00:00
);
2017-08-09 17:53:06 +00:00
/**
* Calculate item taxes.
*
* @since 3.2.0
* @param array $calculate_tax_for Location data to get taxes for. Required.
* @return bool True if taxes were calculated.
*/
public function calculate_taxes( $calculate_tax_for = array() ) {
if ( ! isset( $calculate_tax_for['country'], $calculate_tax_for['state'], $calculate_tax_for['postcode'], $calculate_tax_for['city'] ) ) {
return false;
}
if ( wc_tax_enabled() && ( $order = $this->get_order() ) ) {
2017-08-11 15:17:00 +00:00
// Apportion taxes to order items, shipping, and fees.
2017-08-09 17:53:06 +00:00
$order = $this->get_order();
2017-08-11 15:17:00 +00:00
$tax_class_counts = $order->get_tax_class_counts_for_items( array( 'line_item', 'fee', 'shipping' ) );
$item_count = $order->get_item_count( array( 'line_item', 'fee', 'shipping' ) );
2017-08-09 17:53:06 +00:00
$discount_taxes = array();
foreach ( $tax_class_counts as $tax_class => $tax_class_count ) {
$proportion = $tax_class_count / $item_count;
$cart_discount_proportion = $this->get_total() * $proportion;
$discount_taxes = wc_array_merge_recursive_numeric( $discount_taxes, WC_Tax::calc_tax( $cart_discount_proportion, WC_Tax::get_rates( $tax_class ) ) );
}
$this->set_taxes( array( 'total' => $discount_taxes ) );
} else {
$this->set_taxes( false );
}
return true;
}
2017-08-09 15:16:44 +00:00
/*
|--------------------------------------------------------------------------
| Setters
|--------------------------------------------------------------------------
*/
/**
* Set amount.
*
2017-08-09 18:02:10 +00:00
* @param string $value Value to set.
2017-08-09 15:16:44 +00:00
*/
2017-08-09 18:02:10 +00:00
public function set_amount( $value ) {
$this->set_prop( 'amount', $value );
2017-08-09 15:16:44 +00:00
}
/**
* Set discount_type.
*
* @param string $value Value to set.
*/
public function set_discount_type( $value ) {
$this->set_prop( 'discount_type', $value );
}
/**
2017-08-09 17:53:06 +00:00
* Set total.
2017-08-09 15:16:44 +00:00
*
* @param string $value Value to set.
*/
2017-08-09 17:53:06 +00:00
public function set_total( $value ) {
$this->set_prop( 'total', wc_format_decimal( $value ) );
}
/**
* Set total tax.
*
* @param string $value Value to set.
*/
public function set_total_tax( $value ) {
$this->set_prop( 'total_tax', wc_format_decimal( $value ) );
}
/**
* Set taxes.
*
* This is an array of tax ID keys with total amount values.
2017-08-10 13:22:27 +00:00
*
* @param array $raw_tax_data Array of taxes.
2017-08-09 17:53:06 +00:00
*/
public function set_taxes( $raw_tax_data ) {
$raw_tax_data = maybe_unserialize( $raw_tax_data );
$tax_data = array(
'total' => array(),
);
if ( ! empty( $raw_tax_data['total'] ) ) {
$tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data['total'] );
}
$this->set_prop( 'taxes', $tax_data );
$this->set_total_tax( array_sum( $tax_data['total'] ) );
2017-08-09 15:16:44 +00:00
}
/*
|--------------------------------------------------------------------------
| Getters
|--------------------------------------------------------------------------
*/
/**
* Get order item type.
*
* @return string
*/
public function get_type() {
return 'discount';
}
/**
* Get amount.
*
* @param string $context View or edit context.
* @return string
*/
public function get_amount( $context = 'view' ) {
return $this->get_prop( 'amount', $context );
}
/**
* Get discount_type.
*
* @param string $context View or edit context.
* @return string
*/
public function get_discount_type( $context = 'view' ) {
return $this->get_prop( 'discount_type', $context );
}
/**
2017-08-09 17:53:06 +00:00
* Get total fee.
2017-08-09 15:16:44 +00:00
*
2017-08-09 17:53:06 +00:00
* @param string $context
2017-08-09 15:16:44 +00:00
* @return string
*/
2017-08-09 17:53:06 +00:00
public function get_total( $context = 'view' ) {
return $this->get_prop( 'total', $context );
}
/**
* Get total tax.
*
* @param string $context
* @return string
*/
public function get_total_tax( $context = 'view' ) {
return $this->get_prop( 'total_tax', $context );
}
/**
* Get fee taxes.
*
* @param string $context
* @return array
*/
public function get_taxes( $context = 'view' ) {
return $this->get_prop( 'taxes', $context );
2017-08-09 15:16:44 +00:00
}
}