2016-06-21 19:10:09 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2018-03-21 03:52:55 +00:00
|
|
|
* Order Line Item (tax)
|
2016-06-21 19:10:09 +00:00
|
|
|
*
|
2018-03-21 03:52:55 +00:00
|
|
|
* @package WooCommerce/Classes
|
|
|
|
* @version 3.0.0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Order item tax.
|
2016-06-21 19:10:09 +00:00
|
|
|
*/
|
|
|
|
class WC_Order_Item_Tax extends WC_Order_Item {
|
|
|
|
|
|
|
|
/**
|
2017-03-15 16:36:53 +00:00
|
|
|
* Order Data array. This is the core order data exposed in APIs since 3.0.0.
|
2018-03-21 03:52:55 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-06-21 19:10:09 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2016-11-17 21:30:34 +00:00
|
|
|
protected $extra_data = array(
|
2016-06-21 19:10:09 +00:00
|
|
|
'rate_code' => '',
|
|
|
|
'rate_id' => 0,
|
|
|
|
'label' => '',
|
|
|
|
'compound' => false,
|
|
|
|
'tax_total' => 0,
|
2016-08-27 01:46:45 +00:00
|
|
|
'shipping_tax_total' => 0,
|
2019-07-11 13:56:28 +00:00
|
|
|
'rate_percent' => null,
|
2016-06-21 19:10:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Setters
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set order item name.
|
2016-11-17 21:30:34 +00:00
|
|
|
*
|
2018-03-21 03:52:55 +00:00
|
|
|
* @param string $value Name.
|
2016-06-21 19:10:09 +00:00
|
|
|
*/
|
|
|
|
public function set_name( $value ) {
|
2016-08-26 09:54:18 +00:00
|
|
|
$this->set_rate_code( $value );
|
2016-06-21 19:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set item name.
|
2016-11-17 21:30:34 +00:00
|
|
|
*
|
2018-03-21 03:52:55 +00:00
|
|
|
* @param string $value Rate code.
|
2016-06-21 19:10:09 +00:00
|
|
|
*/
|
|
|
|
public function set_rate_code( $value ) {
|
2016-11-17 21:30:34 +00:00
|
|
|
$this->set_prop( 'rate_code', wc_clean( $value ) );
|
2016-06-21 19:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set item name.
|
2018-03-21 03:52:55 +00:00
|
|
|
*
|
|
|
|
* @param string $value Label.
|
2016-06-21 19:10:09 +00:00
|
|
|
*/
|
|
|
|
public function set_label( $value ) {
|
2016-11-17 21:30:34 +00:00
|
|
|
$this->set_prop( 'label', wc_clean( $value ) );
|
2016-06-21 19:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set tax rate id.
|
2018-03-21 03:52:55 +00:00
|
|
|
*
|
|
|
|
* @param int $value Rate ID.
|
2016-06-21 19:10:09 +00:00
|
|
|
*/
|
|
|
|
public function set_rate_id( $value ) {
|
2016-11-17 21:30:34 +00:00
|
|
|
$this->set_prop( 'rate_id', absint( $value ) );
|
2016-06-21 19:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set tax total.
|
2018-03-21 03:52:55 +00:00
|
|
|
*
|
|
|
|
* @param string $value Tax total.
|
2016-06-21 19:10:09 +00:00
|
|
|
*/
|
|
|
|
public function set_tax_total( $value ) {
|
2017-03-20 22:14:41 +00:00
|
|
|
$this->set_prop( 'tax_total', $value ? wc_format_decimal( $value ) : 0 );
|
2016-06-21 19:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-21 03:52:55 +00:00
|
|
|
* Set shipping tax total.
|
|
|
|
*
|
|
|
|
* @param string $value Shipping tax total.
|
2016-06-21 19:10:09 +00:00
|
|
|
*/
|
|
|
|
public function set_shipping_tax_total( $value ) {
|
2017-03-20 22:14:41 +00:00
|
|
|
$this->set_prop( 'shipping_tax_total', $value ? wc_format_decimal( $value ) : 0 );
|
2016-06-21 19:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-21 03:52:55 +00:00
|
|
|
* Set compound.
|
|
|
|
*
|
|
|
|
* @param bool $value If tax is compound.
|
2016-06-21 19:10:09 +00:00
|
|
|
*/
|
|
|
|
public function set_compound( $value ) {
|
2016-11-17 21:30:34 +00:00
|
|
|
$this->set_prop( 'compound', (bool) $value );
|
2016-06-21 19:10:09 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 15:35:43 +00:00
|
|
|
/**
|
|
|
|
* Set rate value.
|
|
|
|
*
|
|
|
|
* @param float $value tax rate value.
|
|
|
|
*/
|
|
|
|
public function set_rate_percent( $value ) {
|
|
|
|
$this->set_prop( 'rate_percent', (float) $value );
|
|
|
|
}
|
|
|
|
|
2016-08-22 13:52:03 +00:00
|
|
|
/**
|
|
|
|
* Set properties based on passed in tax rate by ID.
|
2018-03-21 03:52:55 +00:00
|
|
|
*
|
|
|
|
* @param int $tax_rate_id Tax rate ID.
|
2016-08-22 13:52:03 +00:00
|
|
|
*/
|
|
|
|
public function set_rate( $tax_rate_id ) {
|
2017-02-20 21:50:07 +00:00
|
|
|
$tax_rate = WC_Tax::_get_tax_rate( $tax_rate_id, OBJECT );
|
|
|
|
|
2016-08-22 13:52:03 +00:00
|
|
|
$this->set_rate_id( $tax_rate_id );
|
2017-02-20 21:50:07 +00:00
|
|
|
$this->set_rate_code( WC_Tax::get_rate_code( $tax_rate ) );
|
|
|
|
$this->set_label( WC_Tax::get_rate_label( $tax_rate ) );
|
|
|
|
$this->set_compound( WC_Tax::is_compound( $tax_rate ) );
|
2019-04-10 15:35:43 +00:00
|
|
|
$this->set_rate_percent( WC_Tax::get_rate_percent_value( $tax_rate ) );
|
2016-08-22 13:52:03 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 19:10:09 +00:00
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Getters
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get order item type.
|
2016-11-17 21:30:34 +00:00
|
|
|
*
|
2016-06-21 19:10:09 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-18 11:33:31 +00:00
|
|
|
public function get_type() {
|
2016-06-21 19:10:09 +00:00
|
|
|
return 'tax';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get rate code/name.
|
2016-11-17 21:30:34 +00:00
|
|
|
*
|
2018-03-21 03:52:55 +00:00
|
|
|
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
|
2016-06-21 19:10:09 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-17 21:30:34 +00:00
|
|
|
public function get_name( $context = 'view' ) {
|
|
|
|
return $this->get_rate_code( $context );
|
2016-06-21 19:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get rate code/name.
|
2016-11-17 21:30:34 +00:00
|
|
|
*
|
2018-03-21 03:52:55 +00:00
|
|
|
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
|
2016-06-21 19:10:09 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-17 21:30:34 +00:00
|
|
|
public function get_rate_code( $context = 'view' ) {
|
|
|
|
return $this->get_prop( 'rate_code', $context );
|
2016-06-21 19:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get label.
|
2016-11-17 21:30:34 +00:00
|
|
|
*
|
2018-03-21 03:52:55 +00:00
|
|
|
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
|
2016-06-21 19:10:09 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-17 21:30:34 +00:00
|
|
|
public function get_label( $context = 'view' ) {
|
|
|
|
$label = $this->get_prop( 'label', $context );
|
2016-11-21 12:14:53 +00:00
|
|
|
if ( 'view' === $context ) {
|
|
|
|
return $label ? $label : __( 'Tax', 'woocommerce' );
|
|
|
|
} else {
|
|
|
|
return $label;
|
|
|
|
}
|
2016-06-21 19:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get tax rate ID.
|
2016-11-17 21:30:34 +00:00
|
|
|
*
|
2018-03-21 03:52:55 +00:00
|
|
|
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
|
2016-06-21 19:10:09 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2016-11-17 21:30:34 +00:00
|
|
|
public function get_rate_id( $context = 'view' ) {
|
|
|
|
return $this->get_prop( 'rate_id', $context );
|
2016-06-21 19:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get tax_total
|
2016-11-17 21:30:34 +00:00
|
|
|
*
|
2018-03-21 03:52:55 +00:00
|
|
|
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
|
2016-06-21 19:10:09 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-17 21:30:34 +00:00
|
|
|
public function get_tax_total( $context = 'view' ) {
|
|
|
|
return $this->get_prop( 'tax_total', $context );
|
2016-06-21 19:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get shipping_tax_total
|
2016-11-17 21:30:34 +00:00
|
|
|
*
|
2018-03-21 03:52:55 +00:00
|
|
|
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
|
2016-06-21 19:10:09 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-17 21:30:34 +00:00
|
|
|
public function get_shipping_tax_total( $context = 'view' ) {
|
|
|
|
return $this->get_prop( 'shipping_tax_total', $context );
|
2016-06-21 19:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get compound.
|
2016-11-17 21:30:34 +00:00
|
|
|
*
|
2018-03-21 03:52:55 +00:00
|
|
|
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
|
2016-06-21 19:10:09 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-11-17 21:30:34 +00:00
|
|
|
public function get_compound( $context = 'view' ) {
|
|
|
|
return $this->get_prop( 'compound', $context );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is this a compound tax rate?
|
2018-03-21 03:52:55 +00:00
|
|
|
*
|
2016-11-17 21:30:34 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function is_compound() {
|
|
|
|
return $this->get_compound();
|
2016-06-21 19:10:09 +00:00
|
|
|
}
|
2017-02-23 19:14:42 +00:00
|
|
|
|
2019-04-10 15:35:43 +00:00
|
|
|
/**
|
|
|
|
* Get rate value
|
|
|
|
*
|
|
|
|
* @param string $context What the value is for. Valid values are 'view' and 'edit'.
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function get_rate_percent( $context = 'view' ) {
|
|
|
|
return $this->get_prop( 'rate_percent', $context );
|
|
|
|
}
|
|
|
|
|
2017-02-23 19:14:42 +00:00
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Array Access Methods
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
2017-07-17 10:10:52 +00:00
|
|
|
| For backwards compatibility with legacy arrays.
|
2017-02-23 19:14:42 +00:00
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2018-03-21 03:52:55 +00:00
|
|
|
* O for ArrayAccess/Backwards compatibility.
|
|
|
|
*
|
2017-02-23 19:14:42 +00:00
|
|
|
* @deprecated Add deprecation notices in future release.
|
2018-03-21 03:52:55 +00:00
|
|
|
* @param string $offset Offset.
|
2017-02-23 19:14:42 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function offsetGet( $offset ) {
|
|
|
|
if ( 'tax_amount' === $offset ) {
|
|
|
|
$offset = 'tax_total';
|
|
|
|
} elseif ( 'shipping_tax_amount' === $offset ) {
|
|
|
|
$offset = 'shipping_tax_total';
|
|
|
|
}
|
|
|
|
return parent::offsetGet( $offset );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-21 03:52:55 +00:00
|
|
|
* OffsetSet for ArrayAccess/Backwards compatibility.
|
|
|
|
*
|
2017-02-23 19:14:42 +00:00
|
|
|
* @deprecated Add deprecation notices in future release.
|
2018-03-21 03:52:55 +00:00
|
|
|
* @param string $offset Offset.
|
|
|
|
* @param mixed $value Value.
|
2017-02-23 19:14:42 +00:00
|
|
|
*/
|
|
|
|
public function offsetSet( $offset, $value ) {
|
|
|
|
if ( 'tax_amount' === $offset ) {
|
|
|
|
$offset = 'tax_total';
|
|
|
|
} elseif ( 'shipping_tax_amount' === $offset ) {
|
|
|
|
$offset = 'shipping_tax_total';
|
|
|
|
}
|
|
|
|
parent::offsetSet( $offset, $value );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-21 03:52:55 +00:00
|
|
|
* OffsetExists for ArrayAccess.
|
|
|
|
*
|
|
|
|
* @param string $offset Offset.
|
2017-02-23 19:14:42 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function offsetExists( $offset ) {
|
2018-03-21 03:52:55 +00:00
|
|
|
if ( in_array( $offset, array( 'tax_amount', 'shipping_tax_amount' ), true ) ) {
|
2017-02-23 19:14:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return parent::offsetExists( $offset );
|
|
|
|
}
|
2016-06-21 19:10:09 +00:00
|
|
|
}
|