PluginProbe
Sendy / 3.0.3
Sendy v3.0.3
3.4.6 3.4.7 trunk 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 All 32 releases
sendy / src / Modules / Orders / OrderList.php

OrderList.php in Sendy 3.0.3, at src/Modules/Orders/OrderList.php

100 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @param array $columns
29 * @return array
30 */
31 public function add_sendy_column_headers(array $columns): array
32 {
33 $wc_actions = $columns['wc_actions'] ?? null;
34
35 unset($columns['wc_actions']);
36
37 $columns['sendy_shipping_method'] = esc_html__('Shipping method', 'sendy');
38 $columns['sendy_track_trace'] = esc_html__('Track and trace', 'sendy');
39
40 if ($wc_actions) {
41 $columns['wc_actions'] = $wc_actions;
42 }
43
44 return $columns;
45 }
46
47 /**
48 * Add the content of the columns to the table with orders
49 *
50 * @param $column
51 * @param $order_id
52 * @return void
53 */
54 public function add_sendy_columns($column, $order_id = null): void
55 {
56 if (is_null($order_id)) {
57 return;
58 }
59
60 $order = wc_get_order($order_id);
61
62 $this->migrate_legacy_data($order);
63
64 if ($column === 'sendy_shipping_method') {
65 echo wp_kses(
66 View::fromTemplate('admin/orders/shipping_method.php')->render(['order' => $order]),
67 View::ALLOWED_TAGS
68 );
69 }
70
71 if ($column === 'sendy_track_trace') {
72 echo wp_kses(
73 View::fromTemplate('admin/orders/track_trace.php')->render(['order' => $order]),
74 View::ALLOWED_TAGS
75 );
76 }
77 }
78
79 /**
80 * Migrate the meta data from the legacy plug-in to the new structure
81 *
82 * @param \WC_Order $order
83 * @return void
84 * @throws GuzzleException
85 */
86 private function migrate_legacy_data(\WC_Order $order): void
87 {
88 if ($order->get_meta('sendy_shipment_id')) {
89 try {
90 $shipment = $this->shipments->get($order->get_meta('sendy_shipment_id'));
91
92 $order->update_meta_data('_sendy_shipment_id', $shipment['uuid']);
93 $order->update_meta_data('_sendy_packages', $shipment['packages']);
94 $order->update_meta_data('sendy_shipment_id', null);
95 } catch (ApiException $e) {
96 }
97 }
98 }
99 }
100