0, 'id' => 0, 'code' => '', 'discount' => 0, 'discount_tax' => 0, ); /** * 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() ) { $this->set_discount( get_metadata( 'order_item', $this->get_id(), 'discount_amount', true ) ); $this->set_discount_tax( get_metadata( 'order_item', $this->get_id(), 'discount_amount_tax', true ) ); } } /** * 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 * @return bool|WP_Error Returns success true or false/WP Error on failure. */ public function set_name( $value ) { return $this->set_code( $value ); } /** * Set code. * @param string $value * @return bool|WP_Error Returns success true or false/WP Error on failure. */ public function set_code( $value ) { return $this->set_prop( 'code', wc_clean( $value ) ); } /** * Set discount amount. * @param string $value * @return bool|WP_Error Returns success true or false/WP Error on failure. */ public function set_discount( $value ) { return $this->set_prop( 'discount', wc_format_decimal( $value ) ); } /** * Set discounted tax amount. * @param string $value * @return bool|WP_Error Returns success true or false/WP Error on failure. */ public function set_discount_tax( $value ) { return $this->set_prop( 'discount_tax', wc_format_decimal( $value ) ); } /* |-------------------------------------------------------------------------- | 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() { return $this->_data['code']; } /** * Get discount amount. * @return string */ public function get_discount() { return wc_format_decimal( $this->_data['discount'] ); } /** * Get discounted tax amount. * @return string */ public function get_discount_tax() { return wc_format_decimal( $this->_data['discount_tax'] ); } }