PluginProbe
PostNL for WooCommerce / 3.1.7
PostNL for WooCommerce v3.1.7
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / includes / compatibility / class-wc-order-compatibility.php

class-wc-order-compatibility.php in PostNL for WooCommerce 3.1.7, at includes/compatibility/class-wc-order-compatibility.php

388 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Derived from SkyVerge WooCommerce Plugin Framework https://github.com/skyverge/wc-plugin-framework/
4 */
5
6 namespace WPO\WC\PostNL\Compatibility;
7
8 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
9
10 if ( ! class_exists( '\\WPO\\WC\\PostNL\\Compatibility\\Order' ) ) :
11
12 /**
13 * WooCommerce order compatibility class.
14 *
15 * @since 4.6.0-dev
16 */
17 class Order extends Data {
18
19
20 /** @var array mapped compatibility properties, as `$new_prop => $old_prop` */
21 protected static $compat_props = array(
22 'date_completed' => 'completed_date',
23 'date_paid' => 'paid_date',
24 'date_modified' => 'modified_date',
25 'date_created' => 'order_date',
26 'customer_id' => 'customer_user',
27 'discount' => 'cart_discount',
28 'discount_tax' => 'cart_discount_tax',
29 'shipping_total' => 'total_shipping',
30 'type' => 'order_type',
31 'currency' => 'order_currency',
32 'version' => 'order_version',
33 );
34
35
36 /**
37 * Backports WC_Order::get_id() method to pre-2.6.0
38 *
39 * @since 4.2.0
40 * @param \WC_Order $order order object
41 * @return string|int order ID
42 */
43 public static function get_id( $order ) {
44
45 if ( method_exists( $order, 'get_id' ) ) {
46
47 return $order->get_id();
48
49 } else {
50
51 return isset($order->id) ? $order->id : false;
52 }
53 }
54
55
56 /**
57 * Gets an order property.
58 *
59 * @since 4.6.0-dev
60 * @param \WC_Order $object the order object
61 * @param string $prop the property name
62 * @param string $context if 'view' then the value will be filtered
63 * @return mixed
64 */
65 public static function get_prop( $object, $prop, $context = 'edit', $compat_props = array() ) {
66
67 // backport a few specific properties to pre-3.0
68 if ( WC_Core::is_wc_version_lt_3_0() ) {
69
70 // converge the shipping total prop for the raw context
71 if ( 'shipping_total' === $prop && 'view' !== $context ) {
72
73 $prop = 'order_shipping';
74
75 // get the post_parent and bail early
76 } elseif ( 'parent_id' === $prop ) {
77
78 return $object->post->post_parent;
79 }
80 }
81
82 $value = parent::get_prop( $object, $prop, $context, self::$compat_props );
83
84 // 3.0+ date getters return a DateTime object, where previously MySQL date strings were returned
85 if ( WC_Core::is_wc_version_lt_3_0() && in_array( $prop, array( 'date_completed', 'date_paid', 'date_modified', 'date_created' ), true ) && !empty( $value ) ) {
86 if ( is_numeric( $value ) ) {
87 $value = new WC_DateTime( "@{$value}", new \DateTimeZone( 'UTC' ) );
88 $value->setTimezone( new \DateTimeZone( wc_timezone_string() ) );
89 } else {
90 $value = new WC_DateTime( $value, new \DateTimeZone( wc_timezone_string() ) );
91 $value->setTimezone( new \DateTimeZone( wc_timezone_string() ) );
92 }
93 }
94
95 return $value;
96 }
97
98
99 /**
100 * Sets an order's properties.
101 *
102 * Note that this does not save any data to the database.
103 *
104 * @since 4.6.0-dev
105 * @param \WC_Order $object the order object
106 * @param array $props the new properties as $key => $value
107 * @return \WC_Order
108 */
109 public static function set_props( $object, $props, $compat_props = array() ) {
110
111 return parent::set_props( $object, $props, self::$compat_props );
112 }
113
114 /**
115 * Backports WC_Order::set_address_prop() to pre-3.0
116 * Saves by default.
117 *
118 * @since 4.6.0-dev
119 * @param \WC_Order $order the order object
120 * @param string $prop Name of prop to set.
121 * @param string $address Name of address to set. billing or shipping.
122 * @param mixed $value Value of the prop.
123 * @param bool $save whether to save the order/property
124 * @return \WC_Order
125 */
126 public static function set_address_prop( \WC_Order $order, $prop, $address = 'billing', $value, $save = true ) {
127 if ( WC_Core::is_wc_version_gte_3_0() ) {
128 if ( is_callable( array( $order, "set_{$address}_{$prop}" ) ) ) {
129 $order->{"set_{$address}_{$prop}"}( $value );
130 if ($save === true) {
131 $order->save();
132 }
133 }
134 } else {
135 // wc 2.6 or older
136 if ($save === true) {
137 // store directly in postmeta
138 update_post_meta( $order->id, "_{$address}_{$prop}", $value );
139 } else {
140 // only change property in the order
141 $order->$prop = $value;
142 }
143 }
144
145 return $order;
146 }
147
148 /**
149 * Implements WC_Order::get_item_meta for 3.0+
150 * @param \WC_Order $order the order object
151 * @param int $item_id the item id
152 * @param int $key the meta key
153 * @param boolean $single single or multiple
154 * @return mixed item meta
155 */
156 public static function get_item_meta( $object, $item_id, $key = '', $single = false ) {
157 if (function_exists('wc_get_order_item_meta')) {
158 $item_meta = wc_get_order_item_meta( $item_id, $key, $single );
159 } else {
160 $item_meta = $object->get_item_meta( $item_id, $key, $single );
161 }
162 return $item_meta;
163 }
164
165 /**
166 * Backports WC_Order::get_status() to pre-3.0.0
167 *
168 * @since 4.6.0-dev
169 * @param \WC_Order $order the order object
170 * @return string order status
171 */
172 public static function get_status( \WC_Order $order ) {
173
174 if ( method_exists( $order, 'get_status' ) ) {
175 return $order->get_status();
176 } else {
177 return $order->status;
178 }
179 }
180
181 /**
182 * Order item CRUD compatibility method to add a coupon to an order.
183 *
184 * @since 4.6.0-dev
185 * @param \WC_Order $order the order object
186 * @param array $code the coupon code
187 * @param int $discount the discount amount.
188 * @param int $discount_tax the discount tax amount.
189 * @return int the order item ID
190 */
191 public static function add_coupon( \WC_Order $order, $code = array(), $discount = 0, $discount_tax = 0 ) {
192
193 if ( WC_Core::is_wc_version_gte_3_0() ) {
194
195 $item = new \WC_Order_Item_Coupon();
196
197 $item->set_props( array(
198 'code' => $code,
199 'discount' => $discount,
200 'discount_tax' => $discount_tax,
201 'order_id' => $order->get_id(),
202 ) );
203
204 $item->save();
205
206 $order->add_item( $item );
207
208 return $item->get_id();
209
210 } else {
211
212 return $order->add_coupon( $code, $discount, $discount_tax );
213 }
214 }
215
216
217 /**
218 * Order item CRUD compatibility method to add a fee to an order.
219 *
220 * @since 4.6.0-dev
221 * @param \WC_Order $order the order object
222 * @param object $fee the fee to add
223 * @return int|\WC_Order_Item the order item ID
224 */
225 public static function add_fee( \WC_Order $order, $fee ) {
226
227 if ( WC_Core::is_wc_version_gte_3_0() ) {
228
229 $item = new \WC_Order_Item_Fee();
230
231 $item->set_props( array(
232 'name' => $fee->name,
233 'tax_class' => $fee->taxable ? $fee->tax_class : 0,
234 'total' => $fee->amount,
235 'total_tax' => $fee->tax,
236 'taxes' => array(
237 'total' => $fee->tax_data,
238 ),
239 'order_id' => $order->get_id(),
240 ) );
241
242 $item->save();
243
244 $order->add_item( $item );
245
246 return $item->get_id();
247
248 } else {
249
250 return $order->add_fee( $fee );
251 }
252 }
253
254
255 /**
256 * Order item CRUD compatibility method to update an order coupon.
257 *
258 * @since 4.6.0-dev
259 * @param \WC_Order $order the order object
260 * @param int|\WC_Order_Item $item the order item ID
261 * @param array $args {
262 * The coupon item args.
263 *
264 * @type string $code the coupon code
265 * @type float $discount the coupon discount amount
266 * @type float $discount_tax the coupon discount tax amount
267 * }
268 * @return int|bool the order item ID or false on failure
269 */
270 public static function update_coupon( \WC_Order $order, $item, $args ) {
271
272 if ( WC_Core::is_wc_version_gte_3_0() ) {
273
274 if ( is_numeric( $item ) ) {
275 $item = $order->get_item( $item );
276 }
277
278 if ( ! is_object( $item ) || ! $item->is_type( 'coupon' ) ) {
279 return false;
280 }
281
282 if ( ! $order->get_id() ) {
283 $order->save();
284 }
285
286 $item->set_order_id( $order->get_id() );
287 $item->set_props( $args );
288 $item->save();
289
290 return $item->get_id();
291
292 } else {
293
294 // convert 3.0+ args for backwards compatibility
295 if ( isset( $args['discount'] ) ) {
296 $args['discount_amount'] = $args['discount'];
297 }
298 if ( isset( $args['discount_tax'] ) ) {
299 $args['discount_amount_tax'] = $args['discount_tax'];
300 }
301
302 return $order->update_coupon( $item, $args );
303 }
304 }
305
306
307 /**
308 * Order item CRUD compatibility method to update an order fee.
309 *
310 * @since 4.6.0-dev
311 * @param \WC_Order $order the order object
312 * @param int $item the order item ID
313 * @param array $args {
314 * The fee item args.
315 *
316 * @type string $name the fee name
317 * @type string $tax_class the fee's tax class
318 * @type float $line_total the fee total amount
319 * @type float $line_tax the fee tax amount
320 * }
321 * @return int|bool the order item ID or false on failure
322 */
323 public static function update_fee( \WC_Order $order, $item, $args ) {
324
325 if ( WC_Core::is_wc_version_gte_3_0() ) {
326
327 if ( is_numeric( $item ) ) {
328 $item = $order->get_item( $item );
329 }
330
331 if ( ! is_object( $item ) || ! $item->is_type( 'fee' ) ) {
332 return false;
333 }
334
335 if ( ! $order->get_id() ) {
336 $order->save();
337 }
338
339 $item->set_order_id( $order->get_id() );
340 $item->set_props( $args );
341 $item->save();
342
343 return $item->get_id();
344
345 } else {
346
347 return $order->update_fee( $item, $args );
348 }
349 }
350
351
352 /**
353 * Backports wc_reduce_stock_levels() to pre-3.0.0
354 *
355 * @since 4.6.0-dev
356 * @param \WC_Order $order the order object
357 */
358 public static function reduce_stock_levels( \WC_Order $order ) {
359
360 if ( WC_Core::is_wc_version_gte_3_0() ) {
361 wc_reduce_stock_levels( $order->get_id() );
362 } else {
363 $order->reduce_order_stock();
364 }
365 }
366
367
368 /**
369 * Backports wc_update_total_sales_counts() to pre-3.0.0
370 *
371 * @since 4.6.0-dev
372 * @param \WC_Order $order the order object
373 */
374 public static function update_total_sales_counts( \WC_Order $order ) {
375
376 if ( WC_Core::is_wc_version_gte_3_0() ) {
377 wc_update_total_sales_counts( $order->get_id() );
378 } else {
379 $order->record_product_sales();
380 }
381 }
382
383
384 }
385
386
387 endif; // Class exists check
388