| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sendy\WooCommerce\Modules\Orders; |
| 4 |
|
| 5 |
use GuzzleHttp\Exception\GuzzleException; |
| 6 |
use Sendy\Api\ApiException; |
| 7 |
use Sendy\WooCommerce\Repositories\Shipments; |
| 8 |
use Sendy\WooCommerce\Utils\View; |
| 9 |
|
| 10 |
class OrderList |
| 11 |
{ |
| 12 |
private Shipments $shipments; |
| 13 |
|
| 14 |
public function __construct() |
| 15 |
{ |
| 16 |
$this->shipments = new Shipments(); |
| 17 |
|
| 18 |
add_filter('manage_edit-shop_order_columns', [$this, 'add_sendy_column_headers'], 29); |
| 19 |
add_filter('manage_woocommerce_page_wc-orders_columns', [$this, 'add_sendy_column_headers'], 29); |
| 20 |
|
| 21 |
add_action('manage_shop_order_posts_custom_column', [$this, 'add_sendy_columns'], 29, 2); |
| 22 |
add_action('manage_woocommerce_page_wc-orders_custom_column', [$this, 'add_sendy_columns'], 29, 2); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Add the headers for the Sendy columns to the table with orders |
| 27 |
*/ |
| 28 |
public function add_sendy_column_headers(array $columns): array |
| 29 |
{ |
| 30 |
$wc_actions = $columns['wc_actions'] ?? null; |
| 31 |
|
| 32 |
unset($columns['wc_actions']); |
| 33 |
|
| 34 |
$columns['sendy_shipping_method'] = esc_html__('Shipping method', 'sendy'); |
| 35 |
$columns['sendy_track_trace'] = esc_html__('Track and trace', 'sendy'); |
| 36 |
|
| 37 |
if ($wc_actions) { |
| 38 |
$columns['wc_actions'] = $wc_actions; |
| 39 |
} |
| 40 |
|
| 41 |
return $columns; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Add the content of the columns to the table with orders |
| 46 |
*/ |
| 47 |
public function add_sendy_columns($column, $order_id = null): void |
| 48 |
{ |
| 49 |
if (is_null($order_id)) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
if ($column === 'sendy_shipping_method') { |
| 54 |
$order = wc_get_order($order_id); |
| 55 |
|
| 56 |
echo wp_kses( |
| 57 |
View::fromTemplate('admin/orders/shipping_method.php')->render(['order' => $order]), |
| 58 |
View::ALLOWED_TAGS, |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
if ($column === 'sendy_track_trace') { |
| 63 |
$order = wc_get_order($order_id); |
| 64 |
|
| 65 |
$this->migrate_legacy_data($order); |
| 66 |
|
| 67 |
echo wp_kses( |
| 68 |
View::fromTemplate('admin/orders/track_trace.php')->render(['order' => $order]), |
| 69 |
View::ALLOWED_TAGS, |
| 70 |
); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Migrate the meta data from the legacy plug-in to the new structure |
| 76 |
*/ |
| 77 |
private function migrate_legacy_data(\WC_Order $order): void |
| 78 |
{ |
| 79 |
if ($order->meta_exists('sendy_shipment_id')) { |
| 80 |
try { |
| 81 |
$shipment = $this->shipments->get($order->get_meta('sendy_shipment_id')); |
| 82 |
|
| 83 |
$order->update_meta_data('_sendy_shipment_id', $shipment['uuid']); |
| 84 |
$order->update_meta_data('_sendy_packages', $shipment['packages']); |
| 85 |
$order->delete_meta_data('sendy_shipment_id'); |
| 86 |
} catch (ApiException|GuzzleException $e) { |
| 87 |
if ($e->getMessage() === 'Shipment data no longer exists') { |
| 88 |
$order->delete_meta_data('sendy_shipment_id'); |
| 89 |
} |
| 90 |
} finally { |
| 91 |
$order->save(); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|