woocommerce/includes/class-wc-order-item-coupon.php

183 lines
4.0 KiB
PHP
Raw Normal View History

2016-06-21 19:10:09 +00:00
<?php
/**
* Order Line Item (coupon)
2016-06-21 19:10:09 +00:00
*
2020-08-05 16:36:24 +00:00
* @package WooCommerce\Classes
* @version 3.0.0
* @since 3.0.0
*/
defined( 'ABSPATH' ) || exit;
/**
* Order item coupon class.
2016-06-21 19:10:09 +00:00
*/
class WC_Order_Item_Coupon 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.
*
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-08-16 11:36:38 +00:00
'code' => '',
'discount' => 0,
'discount_tax' => 0,
2016-06-21 19:10:09 +00:00
);
/*
|--------------------------------------------------------------------------
| Setters
|--------------------------------------------------------------------------
*/
/**
* Set order item name.
2016-11-17 21:30:34 +00:00
*
* @param string $value Coupon code.
2016-06-21 19:10:09 +00:00
*/
public function set_name( $value ) {
2016-08-23 14:25:50 +00:00
return $this->set_code( $value );
2016-06-21 19:10:09 +00:00
}
/**
* Set code.
2016-11-17 21:30:34 +00:00
*
* @param string $value Coupon code.
2016-06-21 19:10:09 +00:00
*/
public function set_code( $value ) {
$this->set_prop( 'code', wc_format_coupon_code( $value ) );
2016-06-21 19:10:09 +00:00
}
/**
* Set discount amount.
2016-11-17 21:30:34 +00:00
*
* @param string $value Discount.
2016-06-21 19:10:09 +00:00
*/
public function set_discount( $value ) {
2016-11-17 21:30:34 +00:00
$this->set_prop( 'discount', wc_format_decimal( $value ) );
2016-06-21 19:10:09 +00:00
}
/**
* Set discounted tax amount.
2016-11-17 21:30:34 +00:00
*
* @param string $value Discount tax.
2016-06-21 19:10:09 +00:00
*/
public function set_discount_tax( $value ) {
2016-11-17 21:30:34 +00:00
$this->set_prop( 'discount_tax', wc_format_decimal( $value ) );
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
*/
public function get_type() {
2016-06-21 19:10:09 +00:00
return 'coupon';
}
/**
* Get order item name.
2016-11-17 21:30:34 +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_code( $context );
2016-06-21 19:10:09 +00:00
}
/**
* Get coupon code.
2016-11-17 21:30:34 +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_code( $context = 'view' ) {
return $this->get_prop( 'code', $context );
2016-06-21 19:10:09 +00:00
}
/**
* Get discount amount.
2016-11-17 21:30:34 +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_discount( $context = 'view' ) {
return $this->get_prop( 'discount', $context );
2016-06-21 19:10:09 +00:00
}
/**
* Get discounted tax amount.
*
* @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_discount_tax( $context = 'view' ) {
return $this->get_prop( 'discount_tax', $context );
}
/*
|--------------------------------------------------------------------------
| Array Access Methods
|--------------------------------------------------------------------------
|
| For backwards compatibility with legacy arrays.
2016-11-17 21:30:34 +00:00
|
*/
/**
* OffsetGet for ArrayAccess/Backwards compatibility.
*
* @deprecated 4.4.0
* @param string $offset Offset.
2016-11-17 21:30:34 +00:00
* @return mixed
*/
public function offsetGet( $offset ) {
wc_deprecated_function( 'WC_Order_Item_Coupon::offsetGet', '4.4.0', '' );
2016-11-17 21:30:34 +00:00
if ( 'discount_amount' === $offset ) {
$offset = 'discount';
} elseif ( 'discount_amount_tax' === $offset ) {
$offset = 'discount_tax';
}
return parent::offsetGet( $offset );
}
/**
* OffsetSet for ArrayAccess/Backwards compatibility.
*
* @deprecated 4.4.0
* @param string $offset Offset.
* @param mixed $value Value.
2016-11-17 21:30:34 +00:00
*/
public function offsetSet( $offset, $value ) {
wc_deprecated_function( 'WC_Order_Item_Coupon::offsetSet', '4.4.0', '' );
2016-11-17 21:30:34 +00:00
if ( 'discount_amount' === $offset ) {
$offset = 'discount';
} elseif ( 'discount_amount_tax' === $offset ) {
$offset = 'discount_tax';
}
parent::offsetSet( $offset, $value );
}
/**
* OffsetExists for ArrayAccess.
*
* @param string $offset Offset.
2016-11-17 21:30:34 +00:00
* @return bool
*/
public function offsetExists( $offset ) {
if ( in_array( $offset, array( 'discount_amount', 'discount_amount_tax' ), true ) ) {
2016-11-17 21:30:34 +00:00
return true;
}
return parent::offsetExists( $offset );
}
2016-06-21 19:10:09 +00:00
}