2016-06-21 19:04:49 +00:00
|
|
|
<?php
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Legacy Abstract Order
|
|
|
|
*
|
|
|
|
* Legacy and deprecated functions are here to keep the WC_Abstract_Order clean.
|
|
|
|
* This class will be removed in future versions.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @version 3.0.0
|
2020-08-05 16:36:24 +00:00
|
|
|
* @package WooCommerce\Abstracts
|
2016-06-21 19:04:49 +00:00
|
|
|
* @category Abstract Class
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
|
|
|
abstract class WC_Abstract_Legacy_Order extends WC_Data {
|
|
|
|
|
2016-08-22 13:51:53 +00:00
|
|
|
/**
|
|
|
|
* Add coupon code to the order.
|
2017-05-15 11:50:52 +00:00
|
|
|
* @param string|array $code
|
2016-08-22 13:51:53 +00:00
|
|
|
* @param int $discount tax amount.
|
|
|
|
* @param int $discount_tax amount.
|
|
|
|
* @return int order item ID
|
2016-08-24 15:02:19 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-08-22 13:51:53 +00:00
|
|
|
*/
|
|
|
|
public function add_coupon( $code = array(), $discount = 0, $discount_tax = 0 ) {
|
2017-03-15 16:36:53 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::add_coupon', '3.0', 'a new WC_Order_Item_Coupon object and add to order with WC_Order::add_item()' );
|
2016-08-22 13:51:53 +00:00
|
|
|
|
2016-11-17 21:30:34 +00:00
|
|
|
$item = new WC_Order_Item_Coupon();
|
|
|
|
$item->set_props( array(
|
2016-08-22 13:51:53 +00:00
|
|
|
'code' => $code,
|
|
|
|
'discount' => $discount,
|
|
|
|
'discount_tax' => $discount_tax,
|
|
|
|
'order_id' => $this->get_id(),
|
|
|
|
) );
|
|
|
|
$item->save();
|
|
|
|
$this->add_item( $item );
|
2017-03-15 16:36:53 +00:00
|
|
|
wc_do_deprecated_action( 'woocommerce_order_add_coupon', array( $this->get_id(), $item->get_id(), $code, $discount, $discount_tax ), '3.0', 'woocommerce_new_order_item action instead.' );
|
2016-08-22 13:51:53 +00:00
|
|
|
return $item->get_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a tax row to the order.
|
2017-05-15 11:50:52 +00:00
|
|
|
* @param int $tax_rate_id
|
2016-08-22 13:51:53 +00:00
|
|
|
* @param int $tax_amount amount of tax.
|
|
|
|
* @param int $shipping_tax_amount shipping amount.
|
|
|
|
* @return int order item ID
|
2016-08-24 15:02:19 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-08-22 13:51:53 +00:00
|
|
|
*/
|
|
|
|
public function add_tax( $tax_rate_id, $tax_amount = 0, $shipping_tax_amount = 0 ) {
|
2017-03-15 16:36:53 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::add_tax', '3.0', 'a new WC_Order_Item_Tax object and add to order with WC_Order::add_item()' );
|
2016-08-22 13:51:53 +00:00
|
|
|
|
2016-11-17 21:30:34 +00:00
|
|
|
$item = new WC_Order_Item_Tax();
|
|
|
|
$item->set_props( array(
|
2016-08-22 13:51:53 +00:00
|
|
|
'rate_id' => $tax_rate_id,
|
|
|
|
'tax_total' => $tax_amount,
|
|
|
|
'shipping_tax_total' => $shipping_tax_amount,
|
|
|
|
) );
|
|
|
|
$item->set_rate( $tax_rate_id );
|
|
|
|
$item->set_order_id( $this->get_id() );
|
|
|
|
$item->save();
|
|
|
|
$this->add_item( $item );
|
2017-03-15 16:36:53 +00:00
|
|
|
wc_do_deprecated_action( 'woocommerce_order_add_tax', array( $this->get_id(), $item->get_id(), $tax_rate_id, $tax_amount, $shipping_tax_amount ), '3.0', 'woocommerce_new_order_item action instead.' );
|
2016-08-22 13:51:53 +00:00
|
|
|
return $item->get_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a shipping row to the order.
|
|
|
|
* @param WC_Shipping_Rate shipping_rate
|
|
|
|
* @return int order item ID
|
2016-08-24 15:02:19 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-08-22 13:51:53 +00:00
|
|
|
*/
|
|
|
|
public function add_shipping( $shipping_rate ) {
|
2017-03-15 16:36:53 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::add_shipping', '3.0', 'a new WC_Order_Item_Shipping object and add to order with WC_Order::add_item()' );
|
2016-08-22 13:51:53 +00:00
|
|
|
|
2016-11-17 21:30:34 +00:00
|
|
|
$item = new WC_Order_Item_Shipping();
|
|
|
|
$item->set_props( array(
|
2016-08-22 13:51:53 +00:00
|
|
|
'method_title' => $shipping_rate->label,
|
|
|
|
'method_id' => $shipping_rate->id,
|
|
|
|
'total' => wc_format_decimal( $shipping_rate->cost ),
|
|
|
|
'taxes' => $shipping_rate->taxes,
|
|
|
|
'order_id' => $this->get_id(),
|
|
|
|
) );
|
2017-01-23 11:30:53 +00:00
|
|
|
foreach ( $shipping_rate->get_meta_data() as $key => $value ) {
|
|
|
|
$item->add_meta_data( $key, $value, true );
|
|
|
|
}
|
2016-08-22 13:51:53 +00:00
|
|
|
$item->save();
|
|
|
|
$this->add_item( $item );
|
2017-03-15 16:36:53 +00:00
|
|
|
wc_do_deprecated_action( 'woocommerce_order_add_shipping', array( $this->get_id(), $item->get_id(), $shipping_rate ), '3.0', 'woocommerce_new_order_item action instead.' );
|
2016-08-22 13:51:53 +00:00
|
|
|
return $item->get_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a fee to the order.
|
|
|
|
* Order must be saved prior to adding items.
|
2017-02-24 18:37:51 +00:00
|
|
|
*
|
|
|
|
* Fee is an amount of money charged for a particular piece of work
|
|
|
|
* or for a particular right or service, and not supposed to be negative.
|
|
|
|
*
|
2016-08-24 15:02:19 +00:00
|
|
|
* @throws WC_Data_Exception
|
2017-02-24 18:37:51 +00:00
|
|
|
* @param object $fee Fee data.
|
|
|
|
* @return int Updated order item ID.
|
2016-08-22 13:51:53 +00:00
|
|
|
*/
|
|
|
|
public function add_fee( $fee ) {
|
2017-03-15 16:36:53 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::add_fee', '3.0', 'a new WC_Order_Item_Fee object and add to order with WC_Order::add_item()' );
|
2016-08-22 13:51:53 +00:00
|
|
|
|
2016-11-17 21:30:34 +00:00
|
|
|
$item = new WC_Order_Item_Fee();
|
|
|
|
$item->set_props( array(
|
2016-08-22 13:51:53 +00:00
|
|
|
'name' => $fee->name,
|
|
|
|
'tax_class' => $fee->taxable ? $fee->tax_class : 0,
|
|
|
|
'total' => $fee->amount,
|
|
|
|
'total_tax' => $fee->tax,
|
|
|
|
'taxes' => array(
|
|
|
|
'total' => $fee->tax_data,
|
|
|
|
),
|
|
|
|
'order_id' => $this->get_id(),
|
|
|
|
) );
|
|
|
|
$item->save();
|
|
|
|
$this->add_item( $item );
|
2017-03-15 16:36:53 +00:00
|
|
|
wc_do_deprecated_action( 'woocommerce_order_add_fee', array( $this->get_id(), $item->get_id(), $fee ), '3.0', 'woocommerce_new_order_item action instead.' );
|
2016-08-22 13:51:53 +00:00
|
|
|
return $item->get_id();
|
|
|
|
}
|
|
|
|
|
2016-06-21 19:04:49 +00:00
|
|
|
/**
|
|
|
|
* Update a line item for the order.
|
|
|
|
*
|
|
|
|
* Note this does not update order totals.
|
|
|
|
*
|
|
|
|
* @param object|int $item order item ID or item object.
|
|
|
|
* @param WC_Product $product
|
|
|
|
* @param array $args data to update.
|
|
|
|
* @return int updated order item ID
|
2016-08-24 15:02:19 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-06-21 19:04:49 +00:00
|
|
|
*/
|
2017-03-09 11:41:25 +00:00
|
|
|
public function update_product( $item, $product, $args ) {
|
2017-03-15 16:36:53 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::update_product', '3.0', 'an interaction with the WC_Order_Item_Product class' );
|
2016-06-21 19:04:49 +00:00
|
|
|
if ( is_numeric( $item ) ) {
|
|
|
|
$item = $this->get_item( $item );
|
|
|
|
}
|
|
|
|
if ( ! is_object( $item ) || ! $item->is_type( 'line_item' ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( ! $this->get_id() ) {
|
|
|
|
$this->save(); // Order must exist
|
|
|
|
}
|
|
|
|
|
|
|
|
// BW compatibility with old args
|
|
|
|
if ( isset( $args['totals'] ) ) {
|
|
|
|
foreach ( $args['totals'] as $key => $value ) {
|
|
|
|
if ( 'tax' === $key ) {
|
|
|
|
$args['total_tax'] = $value;
|
|
|
|
} elseif ( 'tax_data' === $key ) {
|
|
|
|
$args['taxes'] = $value;
|
|
|
|
} else {
|
|
|
|
$args[ $key ] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 10:10:52 +00:00
|
|
|
// Handle qty if set.
|
2016-06-21 19:04:49 +00:00
|
|
|
if ( isset( $args['qty'] ) ) {
|
|
|
|
if ( $product->backorders_require_notification() && $product->is_on_backorder( $args['qty'] ) ) {
|
2017-07-25 03:54:03 +00:00
|
|
|
$item->add_meta_data( apply_filters( 'woocommerce_backordered_item_meta_name', __( 'Backordered', 'woocommerce' ), $item ), $args['qty'] - max( 0, $product->get_stock_quantity() ), true );
|
2016-06-21 19:04:49 +00:00
|
|
|
}
|
2016-11-16 12:17:00 +00:00
|
|
|
$args['subtotal'] = $args['subtotal'] ? $args['subtotal'] : wc_get_price_excluding_tax( $product, array( 'qty' => $args['qty'] ) );
|
|
|
|
$args['total'] = $args['total'] ? $args['total'] : wc_get_price_excluding_tax( $product, array( 'qty' => $args['qty'] ) );
|
2016-06-21 19:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$item->set_order_id( $this->get_id() );
|
2016-08-25 12:05:27 +00:00
|
|
|
$item->set_props( $args );
|
2016-06-21 19:04:49 +00:00
|
|
|
$item->save();
|
|
|
|
do_action( 'woocommerce_order_edit_product', $this->get_id(), $item->get_id(), $args, $product );
|
|
|
|
|
|
|
|
return $item->get_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update coupon for order. Note this does not update order totals.
|
|
|
|
* @param object|int $item
|
|
|
|
* @param array $args
|
|
|
|
* @return int updated order item ID
|
2016-08-24 15:02:19 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-06-21 19:04:49 +00:00
|
|
|
*/
|
|
|
|
public function update_coupon( $item, $args ) {
|
2017-03-15 16:36:53 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::update_coupon', '3.0', 'an interaction with the WC_Order_Item_Coupon class' );
|
2016-06-21 19:04:49 +00:00
|
|
|
if ( is_numeric( $item ) ) {
|
|
|
|
$item = $this->get_item( $item );
|
|
|
|
}
|
|
|
|
if ( ! is_object( $item ) || ! $item->is_type( 'coupon' ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( ! $this->get_id() ) {
|
|
|
|
$this->save(); // Order must exist
|
|
|
|
}
|
|
|
|
|
|
|
|
// BW compatibility for old args
|
|
|
|
if ( isset( $args['discount_amount'] ) ) {
|
|
|
|
$args['discount'] = $args['discount_amount'];
|
|
|
|
}
|
|
|
|
if ( isset( $args['discount_amount_tax'] ) ) {
|
|
|
|
$args['discount_tax'] = $args['discount_amount_tax'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$item->set_order_id( $this->get_id() );
|
2016-08-25 12:05:27 +00:00
|
|
|
$item->set_props( $args );
|
2016-06-21 19:04:49 +00:00
|
|
|
$item->save();
|
|
|
|
|
|
|
|
do_action( 'woocommerce_order_update_coupon', $this->get_id(), $item->get_id(), $args );
|
|
|
|
|
|
|
|
return $item->get_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update shipping method for order.
|
|
|
|
*
|
|
|
|
* Note this does not update the order total.
|
|
|
|
*
|
|
|
|
* @param object|int $item
|
|
|
|
* @param array $args
|
|
|
|
* @return int updated order item ID
|
2016-08-24 15:02:19 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-06-21 19:04:49 +00:00
|
|
|
*/
|
|
|
|
public function update_shipping( $item, $args ) {
|
2017-03-15 16:36:53 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::update_shipping', '3.0', 'an interaction with the WC_Order_Item_Shipping class' );
|
2016-06-21 19:04:49 +00:00
|
|
|
if ( is_numeric( $item ) ) {
|
|
|
|
$item = $this->get_item( $item );
|
|
|
|
}
|
|
|
|
if ( ! is_object( $item ) || ! $item->is_type( 'shipping' ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( ! $this->get_id() ) {
|
|
|
|
$this->save(); // Order must exist
|
|
|
|
}
|
|
|
|
|
|
|
|
// BW compatibility for old args
|
|
|
|
if ( isset( $args['cost'] ) ) {
|
|
|
|
$args['total'] = $args['cost'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$item->set_order_id( $this->get_id() );
|
2016-08-25 12:05:27 +00:00
|
|
|
$item->set_props( $args );
|
2016-06-21 19:04:49 +00:00
|
|
|
$item->save();
|
|
|
|
$this->calculate_shipping();
|
|
|
|
|
|
|
|
do_action( 'woocommerce_order_update_shipping', $this->get_id(), $item->get_id(), $args );
|
|
|
|
|
|
|
|
return $item->get_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update fee for order.
|
|
|
|
*
|
|
|
|
* Note this does not update order totals.
|
|
|
|
*
|
|
|
|
* @param object|int $item
|
|
|
|
* @param array $args
|
|
|
|
* @return int updated order item ID
|
2016-08-24 15:02:19 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-06-21 19:04:49 +00:00
|
|
|
*/
|
|
|
|
public function update_fee( $item, $args ) {
|
2017-03-15 16:36:53 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::update_fee', '3.0', 'an interaction with the WC_Order_Item_Fee class' );
|
2016-06-21 19:04:49 +00:00
|
|
|
if ( is_numeric( $item ) ) {
|
|
|
|
$item = $this->get_item( $item );
|
|
|
|
}
|
|
|
|
if ( ! is_object( $item ) || ! $item->is_type( 'fee' ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( ! $this->get_id() ) {
|
|
|
|
$this->save(); // Order must exist
|
|
|
|
}
|
|
|
|
|
|
|
|
$item->set_order_id( $this->get_id() );
|
2016-08-25 12:05:27 +00:00
|
|
|
$item->set_props( $args );
|
2016-06-21 19:04:49 +00:00
|
|
|
$item->save();
|
|
|
|
|
|
|
|
do_action( 'woocommerce_order_update_fee', $this->get_id(), $item->get_id(), $args );
|
|
|
|
|
|
|
|
return $item->get_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update tax line on order.
|
|
|
|
* Note this does not update order totals.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0
|
2016-06-21 19:04:49 +00:00
|
|
|
* @param object|int $item
|
|
|
|
* @param array $args
|
|
|
|
* @return int updated order item ID
|
2016-08-24 15:02:19 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-06-21 19:04:49 +00:00
|
|
|
*/
|
|
|
|
public function update_tax( $item, $args ) {
|
2017-03-15 16:36:53 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::update_tax', '3.0', 'an interaction with the WC_Order_Item_Tax class' );
|
2016-06-21 19:04:49 +00:00
|
|
|
if ( is_numeric( $item ) ) {
|
|
|
|
$item = $this->get_item( $item );
|
|
|
|
}
|
|
|
|
if ( ! is_object( $item ) || ! $item->is_type( 'tax' ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ( ! $this->get_id() ) {
|
|
|
|
$this->save(); // Order must exist
|
|
|
|
}
|
|
|
|
|
|
|
|
$item->set_order_id( $this->get_id() );
|
2016-08-25 12:05:27 +00:00
|
|
|
$item->set_props( $args );
|
2016-06-21 19:04:49 +00:00
|
|
|
$item->save();
|
|
|
|
|
|
|
|
do_action( 'woocommerce_order_update_tax', $this->get_id(), $item->get_id(), $args );
|
|
|
|
|
|
|
|
return $item->get_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a product (either product or variation).
|
2020-07-16 20:13:08 +00:00
|
|
|
* @deprecated 4.4.0
|
2016-06-21 19:04:49 +00:00
|
|
|
* @param object $item
|
|
|
|
* @return WC_Product|bool
|
|
|
|
*/
|
|
|
|
public function get_product_from_item( $item ) {
|
2020-07-16 20:13:08 +00:00
|
|
|
wc_deprecated_function( 'WC_Abstract_Legacy_Order::get_product_from_item', '4.4.0', '$item->get_product()' );
|
2016-06-21 19:04:49 +00:00
|
|
|
if ( is_callable( array( $item, 'get_product' ) ) ) {
|
|
|
|
$product = $item->get_product();
|
|
|
|
} else {
|
|
|
|
$product = false;
|
|
|
|
}
|
|
|
|
return apply_filters( 'woocommerce_get_product_from_item', $product, $item, $this );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the customer address.
|
|
|
|
* @param array $address Address data.
|
|
|
|
* @param string $type billing or shipping.
|
|
|
|
*/
|
|
|
|
public function set_address( $address, $type = 'billing' ) {
|
|
|
|
foreach ( $address as $key => $value ) {
|
|
|
|
update_post_meta( $this->get_id(), "_{$type}_" . $key, $value );
|
|
|
|
if ( is_callable( array( $this, "set_{$type}_{$key}" ) ) ) {
|
|
|
|
$this->{"set_{$type}_{$key}"}( $value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set an order total.
|
|
|
|
* @param float $amount
|
|
|
|
* @param string $total_type
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function legacy_set_total( $amount, $total_type = 'total' ) {
|
|
|
|
if ( ! in_array( $total_type, array( 'shipping', 'tax', 'shipping_tax', 'total', 'cart_discount', 'cart_discount_tax' ) ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( $total_type ) {
|
|
|
|
case 'total' :
|
|
|
|
$amount = wc_format_decimal( $amount, wc_get_price_decimals() );
|
|
|
|
$this->set_total( $amount );
|
|
|
|
update_post_meta( $this->get_id(), '_order_total', $amount );
|
|
|
|
break;
|
|
|
|
case 'cart_discount' :
|
|
|
|
$amount = wc_format_decimal( $amount );
|
|
|
|
$this->set_discount_total( $amount );
|
|
|
|
update_post_meta( $this->get_id(), '_cart_discount', $amount );
|
|
|
|
break;
|
|
|
|
case 'cart_discount_tax' :
|
|
|
|
$amount = wc_format_decimal( $amount );
|
|
|
|
$this->set_discount_tax( $amount );
|
|
|
|
update_post_meta( $this->get_id(), '_cart_discount_tax', $amount );
|
|
|
|
break;
|
|
|
|
case 'shipping' :
|
|
|
|
$amount = wc_format_decimal( $amount );
|
|
|
|
$this->set_shipping_total( $amount );
|
|
|
|
update_post_meta( $this->get_id(), '_order_shipping', $amount );
|
|
|
|
break;
|
|
|
|
case 'shipping_tax' :
|
|
|
|
$amount = wc_format_decimal( $amount );
|
|
|
|
$this->set_shipping_tax( $amount );
|
|
|
|
update_post_meta( $this->get_id(), '_order_shipping_tax', $amount );
|
|
|
|
break;
|
|
|
|
case 'tax' :
|
|
|
|
$amount = wc_format_decimal( $amount );
|
|
|
|
$this->set_cart_tax( $amount );
|
|
|
|
update_post_meta( $this->get_id(), '_order_tax', $amount );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-14 18:55:56 +00:00
|
|
|
* Magic __isset method for backwards compatibility. Handles legacy properties which could be accessed directly in the past.
|
|
|
|
*
|
2016-06-21 19:04:49 +00:00
|
|
|
* @param string $key
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function __isset( $key ) {
|
|
|
|
$legacy_props = array( 'completed_date', 'id', 'order_type', 'post', 'status', 'post_status', 'customer_note', 'customer_message', 'user_id', 'customer_user', 'prices_include_tax', 'tax_display_cart', 'display_totals_ex_tax', 'display_cart_ex_tax', 'order_date', 'modified_date', 'cart_discount', 'cart_discount_tax', 'order_shipping', 'order_shipping_tax', 'order_total', 'order_tax', 'billing_first_name', 'billing_last_name', 'billing_company', 'billing_address_1', 'billing_address_2', 'billing_city', 'billing_state', 'billing_postcode', 'billing_country', 'billing_phone', 'billing_email', 'shipping_first_name', 'shipping_last_name', 'shipping_company', 'shipping_address_1', 'shipping_address_2', 'shipping_city', 'shipping_state', 'shipping_postcode', 'shipping_country', 'customer_ip_address', 'customer_user_agent', 'payment_method_title', 'payment_method', 'order_currency' );
|
|
|
|
return $this->get_id() ? ( in_array( $key, $legacy_props ) || metadata_exists( 'post', $this->get_id(), '_' . $key ) ) : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Magic __get method for backwards compatibility.
|
2017-02-14 18:55:56 +00:00
|
|
|
*
|
2016-06-21 19:04:49 +00:00
|
|
|
* @param string $key
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __get( $key ) {
|
2017-03-15 16:36:53 +00:00
|
|
|
wc_doing_it_wrong( $key, 'Order properties should not be accessed directly.', '3.0' );
|
2016-06-21 19:04:49 +00:00
|
|
|
|
|
|
|
if ( 'completed_date' === $key ) {
|
2017-03-10 16:30:06 +00:00
|
|
|
return $this->get_date_completed() ? gmdate( 'Y-m-d H:i:s', $this->get_date_completed()->getOffsetTimestamp() ) : '';
|
2016-06-21 19:04:49 +00:00
|
|
|
} elseif ( 'paid_date' === $key ) {
|
2017-03-10 16:30:06 +00:00
|
|
|
return $this->get_date_paid() ? gmdate( 'Y-m-d H:i:s', $this->get_date_paid()->getOffsetTimestamp() ) : '';
|
2016-06-21 19:04:49 +00:00
|
|
|
} elseif ( 'modified_date' === $key ) {
|
2017-03-10 16:30:06 +00:00
|
|
|
return $this->get_date_modified() ? gmdate( 'Y-m-d H:i:s', $this->get_date_modified()->getOffsetTimestamp() ) : '';
|
2016-06-21 19:04:49 +00:00
|
|
|
} elseif ( 'order_date' === $key ) {
|
2017-03-10 16:30:06 +00:00
|
|
|
return $this->get_date_created() ? gmdate( 'Y-m-d H:i:s', $this->get_date_created()->getOffsetTimestamp() ) : '';
|
2016-06-21 19:04:49 +00:00
|
|
|
} elseif ( 'id' === $key ) {
|
|
|
|
return $this->get_id();
|
|
|
|
} elseif ( 'post' === $key ) {
|
|
|
|
return get_post( $this->get_id() );
|
2017-01-03 22:03:23 +00:00
|
|
|
} elseif ( 'status' === $key ) {
|
|
|
|
return $this->get_status();
|
|
|
|
} elseif ( 'post_status' === $key ) {
|
2017-02-22 11:53:34 +00:00
|
|
|
return get_post_status( $this->get_id() );
|
2016-06-21 19:04:49 +00:00
|
|
|
} elseif ( 'customer_message' === $key || 'customer_note' === $key ) {
|
|
|
|
return $this->get_customer_note();
|
|
|
|
} elseif ( in_array( $key, array( 'user_id', 'customer_user' ) ) ) {
|
|
|
|
return $this->get_customer_id();
|
|
|
|
} elseif ( 'tax_display_cart' === $key ) {
|
|
|
|
return get_option( 'woocommerce_tax_display_cart' );
|
|
|
|
} elseif ( 'display_totals_ex_tax' === $key ) {
|
|
|
|
return 'excl' === get_option( 'woocommerce_tax_display_cart' );
|
|
|
|
} elseif ( 'display_cart_ex_tax' === $key ) {
|
|
|
|
return 'excl' === get_option( 'woocommerce_tax_display_cart' );
|
|
|
|
} elseif ( 'cart_discount' === $key ) {
|
2017-02-02 08:45:09 +00:00
|
|
|
return $this->get_total_discount();
|
2016-06-21 19:04:49 +00:00
|
|
|
} elseif ( 'cart_discount_tax' === $key ) {
|
|
|
|
return $this->get_discount_tax();
|
|
|
|
} elseif ( 'order_tax' === $key ) {
|
|
|
|
return $this->get_cart_tax();
|
|
|
|
} elseif ( 'order_shipping_tax' === $key ) {
|
|
|
|
return $this->get_shipping_tax();
|
|
|
|
} elseif ( 'order_shipping' === $key ) {
|
|
|
|
return $this->get_shipping_total();
|
|
|
|
} elseif ( 'order_total' === $key ) {
|
|
|
|
return $this->get_total();
|
|
|
|
} elseif ( 'order_type' === $key ) {
|
|
|
|
return $this->get_type();
|
|
|
|
} elseif ( 'order_currency' === $key ) {
|
|
|
|
return $this->get_currency();
|
|
|
|
} elseif ( 'order_version' === $key ) {
|
|
|
|
return $this->get_version();
|
|
|
|
} elseif ( is_callable( array( $this, "get_{$key}" ) ) ) {
|
|
|
|
return $this->{"get_{$key}"}();
|
|
|
|
} else {
|
|
|
|
return get_post_meta( $this->get_id(), '_' . $key, true );
|
2017-03-09 11:54:27 +00:00
|
|
|
}
|
2016-06-21 19:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-14 12:25:39 +00:00
|
|
|
* has_meta function for order items. This is different to the WC_Data
|
|
|
|
* version and should be removed in future versions.
|
2016-06-21 19:04:49 +00:00
|
|
|
*
|
2020-07-16 20:13:08 +00:00
|
|
|
* @deprecated 3.0
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
|
|
|
* @param int $order_item_id
|
|
|
|
*
|
2016-06-21 19:04:49 +00:00
|
|
|
* @return array of meta data.
|
|
|
|
*/
|
|
|
|
public function has_meta( $order_item_id ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
2017-03-15 16:36:53 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::has_meta( $order_item_id )', '3.0', 'WC_Order_item::get_meta_data' );
|
2016-06-21 19:04:49 +00:00
|
|
|
|
|
|
|
return $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value, meta_id, order_item_id
|
|
|
|
FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = %d
|
|
|
|
ORDER BY meta_id", absint( $order_item_id ) ), ARRAY_A );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display meta data belonging to an item.
|
|
|
|
* @param array $item
|
|
|
|
*/
|
|
|
|
public function display_item_meta( $item ) {
|
2017-03-23 15:32:02 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::display_item_meta', '3.0', 'wc_display_item_meta' );
|
2016-06-21 19:04:49 +00:00
|
|
|
$product = $item->get_product();
|
|
|
|
$item_meta = new WC_Order_Item_Meta( $item, $product );
|
|
|
|
$item_meta->display();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display download links for an order item.
|
|
|
|
* @param array $item
|
|
|
|
*/
|
|
|
|
public function display_item_downloads( $item ) {
|
2017-03-23 15:32:02 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::display_item_downloads', '3.0', 'wc_display_item_downloads' );
|
2016-06-21 19:04:49 +00:00
|
|
|
$product = $item->get_product();
|
|
|
|
|
|
|
|
if ( $product && $product->exists() && $product->is_downloadable() && $this->is_download_permitted() ) {
|
|
|
|
$download_files = $this->get_item_downloads( $item );
|
|
|
|
$i = 0;
|
|
|
|
$links = array();
|
|
|
|
|
|
|
|
foreach ( $download_files as $download_id => $file ) {
|
|
|
|
$i++;
|
2017-03-09 11:41:25 +00:00
|
|
|
/* translators: 1: current item count */
|
2016-06-21 19:04:49 +00:00
|
|
|
$prefix = count( $download_files ) > 1 ? sprintf( __( 'Download %d', 'woocommerce' ), $i ) : __( 'Download', 'woocommerce' );
|
2019-09-10 05:10:16 +00:00
|
|
|
$links[] = '<small class="download-url">' . esc_html( $prefix ) . ': <a href="' . esc_url( $file['download_url'] ) . '" target="_blank">' . esc_html( $file['name'] ) . '</a></small>' . "\n";
|
2016-06-21 19:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
echo '<br/>' . implode( '<br/>', $links );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Download URL.
|
|
|
|
*
|
|
|
|
* @param int $product_id
|
|
|
|
* @param int $download_id
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_download_url( $product_id, $download_id ) {
|
2017-03-23 15:32:02 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::get_download_url', '3.0', 'WC_Order_Item_Product::get_item_download_url' );
|
2016-06-21 19:04:49 +00:00
|
|
|
return add_query_arg( array(
|
|
|
|
'download_file' => $product_id,
|
|
|
|
'order' => $this->get_order_key(),
|
|
|
|
'email' => urlencode( $this->get_billing_email() ),
|
|
|
|
'key' => $download_id,
|
|
|
|
), trailingslashit( home_url() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the downloadable files for an item in this order.
|
|
|
|
*
|
|
|
|
* @param array $item
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_item_downloads( $item ) {
|
2017-03-23 15:32:02 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::get_item_downloads', '3.0', 'WC_Order_Item_Product::get_item_downloads' );
|
2017-03-09 11:41:25 +00:00
|
|
|
|
2017-03-09 11:54:27 +00:00
|
|
|
if ( ! $item instanceof WC_Order_Item ) {
|
|
|
|
if ( ! empty( $item['variation_id'] ) ) {
|
|
|
|
$product_id = $item['variation_id'];
|
|
|
|
} elseif ( ! empty( $item['product_id'] ) ) {
|
|
|
|
$product_id = $item['product_id'];
|
|
|
|
} else {
|
|
|
|
return array();
|
|
|
|
}
|
2017-03-09 11:41:25 +00:00
|
|
|
|
|
|
|
// Create a 'virtual' order item to allow retrieving item downloads when
|
|
|
|
// an array of product_id is passed instead of actual order item.
|
|
|
|
$item = new WC_Order_Item_Product();
|
|
|
|
$item->set_product( wc_get_product( $product_id ) );
|
|
|
|
$item->set_order_id( $this->get_id() );
|
|
|
|
}
|
|
|
|
|
2016-08-03 11:52:45 +00:00
|
|
|
return $item->get_item_downloads();
|
2016-06-21 19:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets shipping total. Alias of WC_Order::get_shipping_total().
|
2017-03-15 16:36:53 +00:00
|
|
|
* @deprecated 3.0.0 since this is an alias only.
|
2016-06-21 19:04:49 +00:00
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function get_total_shipping() {
|
|
|
|
return $this->get_shipping_total();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get order item meta.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @deprecated 3.0.0
|
2016-06-21 19:04:49 +00:00
|
|
|
* @param mixed $order_item_id
|
|
|
|
* @param string $key (default: '')
|
|
|
|
* @param bool $single (default: false)
|
|
|
|
* @return array|string
|
|
|
|
*/
|
|
|
|
public function get_item_meta( $order_item_id, $key = '', $single = false ) {
|
2017-03-23 15:32:02 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::get_item_meta', '3.0', 'wc_get_order_item_meta' );
|
2016-06-21 19:04:49 +00:00
|
|
|
return get_metadata( 'order_item', $order_item_id, $key, $single );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all item meta data in array format in the order it was saved. Does not group meta by key like get_item_meta().
|
|
|
|
*
|
|
|
|
* @param mixed $order_item_id
|
|
|
|
* @return array of objects
|
|
|
|
*/
|
|
|
|
public function get_item_meta_array( $order_item_id ) {
|
2017-03-23 15:32:02 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::get_item_meta_array', '3.0', 'WC_Order_Item::get_meta_data() (note the format has changed)' );
|
2016-06-21 19:04:49 +00:00
|
|
|
$item = $this->get_item( $order_item_id );
|
|
|
|
$meta_data = $item->get_meta_data();
|
|
|
|
$item_meta_array = array();
|
|
|
|
|
|
|
|
foreach ( $meta_data as $meta ) {
|
2016-08-22 18:37:34 +00:00
|
|
|
$item_meta_array[ $meta->id ] = $meta;
|
2016-06-21 19:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $item_meta_array;
|
|
|
|
}
|
|
|
|
|
2019-05-14 09:19:48 +00:00
|
|
|
/**
|
|
|
|
* Get coupon codes only.
|
|
|
|
*
|
|
|
|
* @deprecated 3.7.0 - Replaced with better named method to reflect the actual data being returned.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_used_coupons() {
|
|
|
|
wc_deprecated_function( 'get_used_coupons', '3.7', 'WC_Abstract_Order::get_coupon_codes' );
|
|
|
|
return $this->get_coupon_codes();
|
|
|
|
}
|
|
|
|
|
2016-06-21 19:04:49 +00:00
|
|
|
/**
|
|
|
|
* Expand item meta into the $item array.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @deprecated 3.0.0 Item meta no longer expanded due to new order item
|
2016-06-21 19:04:49 +00:00
|
|
|
* classes. This function now does nothing to avoid data breakage.
|
|
|
|
* @param array $item before expansion.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function expand_item_meta( $item ) {
|
2017-03-23 15:32:02 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::expand_item_meta', '3.0' );
|
2016-06-21 19:04:49 +00:00
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the order object. Called from the constructor.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @deprecated 3.0.0 Logic moved to constructor
|
2016-06-21 19:04:49 +00:00
|
|
|
* @param int|object|WC_Order $order Order to init.
|
|
|
|
*/
|
|
|
|
protected function init( $order ) {
|
2017-03-23 15:32:02 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::init', '3.0', 'Logic moved to constructor' );
|
2016-06-21 19:04:49 +00:00
|
|
|
if ( is_numeric( $order ) ) {
|
2017-02-23 16:19:19 +00:00
|
|
|
$this->set_id( $order );
|
2016-06-21 19:04:49 +00:00
|
|
|
} elseif ( $order instanceof WC_Order ) {
|
2017-02-23 16:19:19 +00:00
|
|
|
$this->set_id( absint( $order->get_id() ) );
|
2016-06-21 19:04:49 +00:00
|
|
|
} elseif ( isset( $order->ID ) ) {
|
2017-02-23 16:19:19 +00:00
|
|
|
$this->set_id( absint( $order->ID ) );
|
2016-06-21 19:04:49 +00:00
|
|
|
}
|
2017-02-23 16:19:19 +00:00
|
|
|
$this->set_object_read( false );
|
|
|
|
$this->data_store->read( $this );
|
2016-06-21 19:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an order from the database.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @deprecated 3.0
|
2016-06-21 19:04:49 +00:00
|
|
|
* @param int $id (default: 0).
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function get_order( $id = 0 ) {
|
2017-03-23 15:32:02 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::get_order', '3.0' );
|
2017-03-09 11:41:25 +00:00
|
|
|
|
2016-06-21 19:04:49 +00:00
|
|
|
if ( ! $id ) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-09 11:41:25 +00:00
|
|
|
|
|
|
|
$result = get_post( $id );
|
|
|
|
|
|
|
|
if ( $result ) {
|
2016-06-21 19:04:49 +00:00
|
|
|
$this->populate( $result );
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-09 11:41:25 +00:00
|
|
|
|
2016-06-21 19:04:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populates an order from the loaded post data.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @deprecated 3.0
|
2016-06-21 19:04:49 +00:00
|
|
|
* @param mixed $result
|
|
|
|
*/
|
|
|
|
public function populate( $result ) {
|
2017-03-23 15:32:02 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::populate', '3.0' );
|
2017-02-23 16:19:19 +00:00
|
|
|
$this->set_id( $result->ID );
|
|
|
|
$this->set_object_read( false );
|
|
|
|
$this->data_store->read( $this );
|
2016-06-21 19:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancel the order and restore the cart (before payment).
|
2017-03-15 16:36:53 +00:00
|
|
|
* @deprecated 3.0.0 Moved to event handler.
|
2016-06-21 19:04:49 +00:00
|
|
|
* @param string $note (default: '') Optional note to add.
|
|
|
|
*/
|
|
|
|
public function cancel_order( $note = '' ) {
|
2017-03-23 15:32:02 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::cancel_order', '3.0', 'WC_Order::update_status' );
|
2016-06-21 19:04:49 +00:00
|
|
|
WC()->session->set( 'order_awaiting_payment', false );
|
|
|
|
$this->update_status( 'cancelled', $note );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Record sales.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @deprecated 3.0.0
|
2016-06-21 19:04:49 +00:00
|
|
|
*/
|
|
|
|
public function record_product_sales() {
|
2017-03-23 15:32:02 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::record_product_sales', '3.0', 'wc_update_total_sales_counts' );
|
2016-06-21 19:04:49 +00:00
|
|
|
wc_update_total_sales_counts( $this->get_id() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Increase applied coupon counts.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @deprecated 3.0.0
|
2016-06-21 19:04:49 +00:00
|
|
|
*/
|
|
|
|
public function increase_coupon_usage_counts() {
|
2017-03-23 15:32:02 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::increase_coupon_usage_counts', '3.0', 'wc_update_coupon_usage_counts' );
|
2016-06-21 19:04:49 +00:00
|
|
|
wc_update_coupon_usage_counts( $this->get_id() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decrease applied coupon counts.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @deprecated 3.0.0
|
2016-06-21 19:04:49 +00:00
|
|
|
*/
|
|
|
|
public function decrease_coupon_usage_counts() {
|
2017-03-23 15:32:02 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::decrease_coupon_usage_counts', '3.0', 'wc_update_coupon_usage_counts' );
|
2016-06-21 19:04:49 +00:00
|
|
|
wc_update_coupon_usage_counts( $this->get_id() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reduce stock levels for all line items in the order.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @deprecated 3.0.0
|
2016-06-21 19:04:49 +00:00
|
|
|
*/
|
|
|
|
public function reduce_order_stock() {
|
2017-03-23 15:32:02 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::reduce_order_stock', '3.0', 'wc_reduce_stock_levels' );
|
2016-06-21 19:04:49 +00:00
|
|
|
wc_reduce_stock_levels( $this->get_id() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the stock notifications.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @deprecated 3.0.0 No longer needs to be called directly.
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
|
|
|
* @param $product
|
|
|
|
* @param $new_stock
|
|
|
|
* @param $qty_ordered
|
2016-06-21 19:04:49 +00:00
|
|
|
*/
|
|
|
|
public function send_stock_notifications( $product, $new_stock, $qty_ordered ) {
|
2017-03-23 15:32:02 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::send_stock_notifications', '3.0' );
|
2016-06-21 19:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output items for display in html emails.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @deprecated 3.0.0 Moved to template functions.
|
2016-06-21 19:04:49 +00:00
|
|
|
* @param array $args Items args.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function email_order_items_table( $args = array() ) {
|
2017-03-23 15:32:02 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::email_order_items_table', '3.0', 'wc_get_email_order_items' );
|
2016-06-21 19:04:49 +00:00
|
|
|
return wc_get_email_order_items( $this, $args );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get currency.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @deprecated 3.0.0
|
2016-06-21 19:04:49 +00:00
|
|
|
*/
|
|
|
|
public function get_order_currency() {
|
2017-03-23 15:32:02 +00:00
|
|
|
wc_deprecated_function( 'WC_Order::get_order_currency', '3.0', 'WC_Order::get_currency' );
|
2016-08-03 11:32:27 +00:00
|
|
|
return apply_filters( 'woocommerce_get_order_currency', $this->get_currency(), $this );
|
2016-06-21 19:04:49 +00:00
|
|
|
}
|
|
|
|
}
|