| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class UpdateOrderHook. |
| 4 |
* |
| 5 |
* @package Packetery |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Hooks; |
| 11 |
|
| 12 |
use Packetery\Module\Order; |
| 13 |
use Packetery\Module\Shipping\ShippingProvider; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class UpdateOrderHook. |
| 17 |
*/ |
| 18 |
class UpdateOrderHook { |
| 19 |
|
| 20 |
/** |
| 21 |
* Metabox Wrapper. |
| 22 |
* |
| 23 |
* @var Order\MetaboxesWrapper |
| 24 |
*/ |
| 25 |
private $metaboxesWrapper; |
| 26 |
|
| 27 |
/** |
| 28 |
* Order repository. |
| 29 |
* |
| 30 |
* @var Order\Repository |
| 31 |
*/ |
| 32 |
private $orderRepository; |
| 33 |
|
| 34 |
/** |
| 35 |
* Constructor. |
| 36 |
* |
| 37 |
* @param Order\MetaboxesWrapper $metaboxesWrapper Metaboxes Wrapper. |
| 38 |
* @param Order\Repository $orderRepository Order repository. |
| 39 |
*/ |
| 40 |
public function __construct( |
| 41 |
Order\MetaboxesWrapper $metaboxesWrapper, |
| 42 |
Order\Repository $orderRepository |
| 43 |
) { |
| 44 |
$this->metaboxesWrapper = $metaboxesWrapper; |
| 45 |
$this->orderRepository = $orderRepository; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Registers hooks. |
| 50 |
* |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
public function register(): void { |
| 54 |
add_action( 'woocommerce_update_order', [ $this, 'updateOrder' ] ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Runs multiple scripts when the WC function is triggered. |
| 59 |
* |
| 60 |
* @param int|mixed $wcOrderId Order ID. |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
public function updateOrder( $wcOrderId ): void { |
| 65 |
static $hasBeenRun; |
| 66 |
|
| 67 |
if ( isset( $hasBeenRun ) && $hasBeenRun === true ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
$wcOrder = $this->orderRepository->getWcOrderById( (int) $wcOrderId ); |
| 72 |
if ( $wcOrder === null ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
if ( ! ShippingProvider::wcOrderHasOurMethod( $wcOrder ) ) { |
| 77 |
$this->orderRepository->delete( (int) $wcOrderId ); |
| 78 |
$hasBeenRun = true; |
| 79 |
|
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
$this->metaboxesWrapper->saveFields( $wcOrderId ); |
| 84 |
|
| 85 |
$hasBeenRun = true; |
| 86 |
} |
| 87 |
} |
| 88 |
|