Order.php
3 years ago
OrderRefund.php
3 years ago
OrderTraits.php
9 months ago
ThemeUpgrader.php
4 years ago
ThemeUpgraderSkin.php
4 years ago
Order.php
155 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WC Admin Order |
| 4 | * |
| 5 | * WC Admin Order class that adds some functionality on top of general WooCommerce WC_Order. |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin\Overrides; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore as CustomersDataStore; |
| 13 | use Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore as OrdersStatsDataStore; |
| 14 | |
| 15 | /** |
| 16 | * WC_Order subclass. |
| 17 | */ |
| 18 | class Order extends \WC_Order { |
| 19 | /** |
| 20 | * Order traits. |
| 21 | */ |
| 22 | use OrderTraits; |
| 23 | |
| 24 | /** |
| 25 | * Holds refund amounts and quantities for the order. |
| 26 | * |
| 27 | * @var void|array |
| 28 | */ |
| 29 | protected $refunded_line_items; |
| 30 | |
| 31 | /** |
| 32 | * Caches the customer ID. |
| 33 | * |
| 34 | * @var int |
| 35 | */ |
| 36 | public $customer_id = null; |
| 37 | |
| 38 | /** |
| 39 | * Get only core class data in array format. |
| 40 | * |
| 41 | * @return array |
| 42 | */ |
| 43 | public function get_data_without_line_items() { |
| 44 | return array_merge( |
| 45 | array( |
| 46 | 'id' => $this->get_id(), |
| 47 | ), |
| 48 | $this->data, |
| 49 | array( |
| 50 | 'number' => $this->get_order_number(), |
| 51 | 'meta_data' => $this->get_meta_data(), |
| 52 | ) |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get order line item data by type. |
| 58 | * |
| 59 | * @param string $type Order line item type. |
| 60 | * @return array|bool Array of line items on success, boolean false on failure. |
| 61 | */ |
| 62 | public function get_line_item_data( $type ) { |
| 63 | $type_to_items = array( |
| 64 | 'line_items' => 'line_item', |
| 65 | 'tax_lines' => 'tax', |
| 66 | 'shipping_lines' => 'shipping', |
| 67 | 'fee_lines' => 'fee', |
| 68 | 'coupon_lines' => 'coupon', |
| 69 | ); |
| 70 | |
| 71 | if ( isset( $type_to_items[ $type ] ) ) { |
| 72 | return $this->get_items( $type_to_items[ $type ] ); |
| 73 | } |
| 74 | |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Add filter(s) required to hook this class to substitute WC_Order. |
| 80 | */ |
| 81 | public static function add_filters() { |
| 82 | add_filter( 'woocommerce_order_class', array( __CLASS__, 'order_class_name' ), 10, 3 ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Filter function to swap class WC_Order for this one in cases when it's suitable. |
| 87 | * |
| 88 | * @param string $classname Name of the class to be created. |
| 89 | * @param string $order_type Type of order object to be created. |
| 90 | * @param number $order_id Order id to create. |
| 91 | * |
| 92 | * @return string |
| 93 | */ |
| 94 | public static function order_class_name( $classname, $order_type, $order_id ) { |
| 95 | // @todo - Only substitute class when necessary (during sync). |
| 96 | if ( 'WC_Order' === $classname ) { |
| 97 | return '\Automattic\WooCommerce\Admin\Overrides\Order'; |
| 98 | } else { |
| 99 | return $classname; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get the customer ID used for reports in the customer lookup table. |
| 105 | * |
| 106 | * @return int |
| 107 | */ |
| 108 | public function get_report_customer_id() { |
| 109 | if ( is_null( $this->customer_id ) ) { |
| 110 | $this->customer_id = CustomersDataStore::get_or_create_customer_from_order( $this ); |
| 111 | } |
| 112 | |
| 113 | return $this->customer_id; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Returns true if the customer has made an earlier order. |
| 118 | * |
| 119 | * @return bool |
| 120 | */ |
| 121 | public function is_returning_customer() { |
| 122 | return OrdersStatsDataStore::is_returning_customer( $this, $this->get_report_customer_id() ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get the customer's first name. |
| 127 | */ |
| 128 | public function get_customer_first_name() { |
| 129 | if ( $this->get_user_id() ) { |
| 130 | return get_user_meta( $this->get_user_id(), 'first_name', true ); |
| 131 | } |
| 132 | |
| 133 | if ( '' !== $this->get_billing_first_name( 'edit' ) ) { |
| 134 | return $this->get_billing_first_name( 'edit' ); |
| 135 | } else { |
| 136 | return $this->get_shipping_first_name( 'edit' ); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Get the customer's last name. |
| 142 | */ |
| 143 | public function get_customer_last_name() { |
| 144 | if ( $this->get_user_id() ) { |
| 145 | return get_user_meta( $this->get_user_id(), 'last_name', true ); |
| 146 | } |
| 147 | |
| 148 | if ( '' !== $this->get_billing_last_name( 'edit' ) ) { |
| 149 | return $this->get_billing_last_name( 'edit' ); |
| 150 | } else { |
| 151 | return $this->get_shipping_last_name( 'edit' ); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 |