PluginProbe
Packeta / 1.3.1
Packeta v1.3.1
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / Order / BulkActions.php

BulkActions.php in Packeta 1.3.1, at src/Packetery/Module/Order/BulkActions.php

165 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class BulkActions
4 *
5 * @package Packetery\Order
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module\Order;
11
12 use PacketeryLatte\Engine;
13 use PacketeryNette\Http\Request;
14 use WC_Order;
15
16 /**
17 * Class BulkActions
18 *
19 * @package Packetery\Order
20 */
21 class BulkActions {
22 /**
23 * Latte engine.
24 *
25 * @var Engine
26 */
27 private $latteEngine;
28
29 /**
30 * HTTP request.
31 *
32 * @var Request
33 */
34 private $httpRequest;
35
36 /**
37 * OrderApi.
38 *
39 * @var PacketSubmitter
40 */
41 private $packetSubmitter;
42
43 /**
44 * BulkActions constructor.
45 *
46 * @param Engine $latteEngine Latte engine.
47 * @param Request $httpRequest HTTP request.
48 * @param PacketSubmitter $packetSubmitter Order API Client.
49 */
50 public function __construct( Engine $latteEngine, Request $httpRequest, PacketSubmitter $packetSubmitter ) {
51 $this->latteEngine = $latteEngine;
52 $this->httpRequest = $httpRequest;
53 $this->packetSubmitter = $packetSubmitter;
54 }
55
56 /**
57 * Adds custom actions to dropdown in admin order list.
58 *
59 * @param array $actions Array of action.
60 *
61 * @return array
62 */
63 public function addActions( array $actions ): array {
64 $actions['submit_to_api'] = __( 'Submit orders to Packeta', 'packeta' );
65 $actions[ LabelPrint::ACTION_PACKETA_LABELS ] = __( 'Print labels', 'packeta' );
66 $actions[ LabelPrint::ACTION_CARRIER_LABELS ] = __( 'Print carrier labels', 'packeta' );
67 $actions[ CollectionPrint::ACTION_PRINT_ORDER_COLLECTION ] = __( 'Print AWB', 'packeta' );
68
69 return $actions;
70 }
71
72 /**
73 * Executes the action for selected orders and returns url to redirect to.
74 *
75 * @param string $redirectTo Url.
76 * @param string $action Action id.
77 * @param array $postIds Order ids.
78 *
79 * @return string
80 */
81 public function handleActions( string $redirectTo, string $action, array $postIds ): string {
82 if ( CollectionPrint::ACTION_PRINT_ORDER_COLLECTION === $action ) {
83 set_transient( CollectionPrint::getOrderIdsTransientName(), $postIds );
84
85 return add_query_arg(
86 [
87 'page' => CollectionPrint::PAGE_SLUG,
88 ],
89 admin_url( 'admin.php' )
90 );
91 }
92
93 if ( in_array( $action, [ LabelPrint::ACTION_PACKETA_LABELS, LabelPrint::ACTION_CARRIER_LABELS ], true ) ) {
94 set_transient( LabelPrint::getOrderIdsTransientName(), $postIds, 60 * 60 );
95 set_transient( LabelPrint::getBackLinkTransientName(), $redirectTo, 60 * 60 );
96
97 return add_query_arg(
98 [
99 'page' => LabelPrint::MENU_SLUG,
100 LabelPrint::LABEL_TYPE_PARAM => $action,
101 ],
102 'admin.php'
103 );
104 }
105
106 if ( 'submit_to_api' === $action ) {
107 $resultsCounter = [
108 'success' => 0,
109 'ignored' => 0,
110 'errors' => 0,
111 ];
112 foreach ( $postIds as $postId ) {
113 $order = wc_get_order( $postId );
114 if ( is_a( $order, WC_Order::class ) ) {
115 $this->packetSubmitter->submitPacket( $order, $resultsCounter );
116 }
117 }
118
119 $queryArgs = $resultsCounter;
120 $queryArgs['submit_to_api'] = true;
121
122 return add_query_arg( $queryArgs, $redirectTo );
123 }
124
125 return $redirectTo;
126 }
127
128 /**
129 * Renders packets export result.
130 */
131 public function renderPacketsExportResult(): void {
132 $get = $this->httpRequest->getQuery();
133 if ( empty( $get['submit_to_api'] ) ) {
134 return;
135 }
136
137 $success = null;
138 if ( is_numeric( $get['success'] ) && $get['success'] > 0 ) {
139 $success = __( 'Shipments were submitted successfully.', 'packeta' );
140 }
141 $ignored = null;
142 if ( is_numeric( $get['ignored'] ) && $get['ignored'] > 0 ) {
143 $ignored = sprintf(
144 // translators: %s is count.
145 __( 'Some shipments (%s in total) were not submitted (these were submitted already or are not Packeta orders).', 'packeta' ),
146 $get['ignored']
147 );
148 }
149 $errors = null;
150 if ( is_numeric( $get['errors'] ) && $get['errors'] > 0 ) {
151 // translators: %s is count.
152 $errors = sprintf( __( 'Some shipments (%s in total) failed to be submitted to Packeta.', 'packeta' ), $get['errors'] );
153 } elseif ( isset( $get['errors'] ) ) {
154 $errors = $get['errors'];
155 }
156
157 $latteParams = [
158 'success' => $success,
159 'ignored' => $ignored,
160 'errors' => $errors,
161 ];
162 $this->latteEngine->render( PACKETERY_PLUGIN_DIR . '/template/order/export-result.latte', $latteParams );
163 }
164 }
165