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