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

205 lines
4.4 KiB
PHP
Raw Normal View History

2016-06-21 19:10:09 +00:00
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Order Line Item (coupon).
*
* @version 2.7.0
* @since 2.7.0
* @package WooCommerce/Classes
* @author WooThemes
*/
class WC_Order_Item_Coupon extends WC_Order_Item {
/**
2016-08-24 15:06:35 +00:00
* Default data values.
2016-06-21 19:10:09 +00:00
* @since 2.7.0
* @var array
*/
2016-08-24 15:06:35 +00:00
protected $_default_data = array(
2016-08-16 11:36:38 +00:00
'order_id' => 0,
'id' => 0,
'code' => '',
'discount' => 0,
'discount_tax' => 0,
2016-06-21 19:10:09 +00:00
);
2016-08-24 15:06:35 +00:00
/**
* Order Data array. This is the core order data exposed in APIs since 2.7.0.
* This is set the _defaults on load.
* @since 2.7.0
* @var array
*/
protected $_data = array();
2016-06-21 19:10:09 +00:00
/**
* offsetGet for ArrayAccess/Backwards compatibility.
* @deprecated Add deprecation notices in future release.
* @param string $offset
* @return mixed
*/
public function offsetGet( $offset ) {
if ( 'discount_amount' === $offset ) {
$offset = 'discount';
} elseif ( 'discount_amount_tax' === $offset ) {
$offset = 'discount_tax';
}
return parent::offsetGet( $offset );
}
/**
* offsetSet for ArrayAccess/Backwards compatibility.
* @deprecated Add deprecation notices in future release.
* @param string $offset
* @param mixed $value
*/
public function offsetSet( $offset, $value ) {
if ( 'discount_amount' === $offset ) {
$offset = 'discount';
} elseif ( 'discount_amount_tax' === $offset ) {
$offset = 'discount_tax';
}
parent::offsetSet( $offset, $value );
}
/**
* offsetExists for ArrayAccess
* @param string $offset
* @return bool
*/
public function offsetExists( $offset ) {
if ( in_array( $offset, array( 'discount_amount', 'discount_amount_tax' ) ) ) {
return true;
}
return parent::offsetExists( $offset );
}
/**
* Read/populate data properties specific to this order item.
*/
public function read( $id ) {
parent::read( $id );
if ( ! $this->get_id() ) {
return;
2016-06-21 19:10:09 +00:00
}
$this->set_props( array(
'discount' => get_metadata( 'order_item', $this->get_id(), 'discount_amount', true ),
'discount_tax' => get_metadata( 'order_item', $this->get_id(), 'discount_amount_tax', true ),
) );
2016-06-21 19:10:09 +00:00
}
/**
* Save properties specific to this order item.
* @return int Item ID
*/
public function save() {
parent::save();
if ( $this->get_id() ) {
wc_update_order_item_meta( $this->get_id(), 'discount_amount', $this->get_discount() );
wc_update_order_item_meta( $this->get_id(), 'discount_amount_tax', $this->get_discount_tax() );
}
return $this->get_id();
}
/**
* Internal meta keys we don't want exposed as part of meta_data.
* @return array()
*/
protected function get_internal_meta_keys() {
return array( 'discount_amount', 'discount_amount_tax' );
}
/*
|--------------------------------------------------------------------------
| Setters
|--------------------------------------------------------------------------
*/
/**
* Set order item name.
* @param string $value
2016-08-24 09:46:29 +00:00
* @throws WC_Data_Exception
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.
* @param string $value
2016-08-24 09:46:29 +00:00
* @throws WC_Data_Exception
2016-06-21 19:10:09 +00:00
*/
public function set_code( $value ) {
2016-08-24 09:46:29 +00:00
$this->set_prop( 'code', wc_clean( $value ) );
2016-06-21 19:10:09 +00:00
}
/**
* Set discount amount.
* @param string $value
2016-08-24 09:46:29 +00:00
* @throws WC_Data_Exception
2016-06-21 19:10:09 +00:00
*/
public function set_discount( $value ) {
2016-08-24 09:46:29 +00:00
$this->set_prop( 'discount', wc_format_decimal( $value ) );
2016-06-21 19:10:09 +00:00
}
/**
* Set discounted tax amount.
* @param string $value
2016-08-24 09:46:29 +00:00
* @throws WC_Data_Exception
2016-06-21 19:10:09 +00:00
*/
public function set_discount_tax( $value ) {
2016-08-24 09:46:29 +00:00
$this->set_prop( 'discount_tax', wc_format_decimal( $value ) );
2016-06-21 19:10:09 +00:00
}
/*
|--------------------------------------------------------------------------
| Getters
|--------------------------------------------------------------------------
*/
/**
* Get order item type.
* @return string
*/
public function get_type() {
return 'coupon';
}
/**
* Get order item name.
* @return string
*/
public function get_name() {
return $this->get_code();
}
/**
* Get coupon code.
* @return string
*/
public function get_code() {
2016-08-23 17:48:48 +00:00
return $this->get_prop( 'code' );
2016-06-21 19:10:09 +00:00
}
/**
* Get discount amount.
* @return string
*/
public function get_discount() {
2016-08-23 17:48:48 +00:00
return wc_format_decimal( $this->get_prop( 'discount' ) );
2016-06-21 19:10:09 +00:00
}
/**
* Get discounted tax amount.
* @return string
*/
public function get_discount_tax() {
2016-08-23 17:48:48 +00:00
return wc_format_decimal( $this->get_prop( 'discount_tax' ) );
2016-06-21 19:10:09 +00:00
}
}