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