PluginProbe
MakeCommerce for WooCommerce / trunk
MakeCommerce for WooCommerce vtrunk
4.1.0 4.0.8 trunk 1.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.2 All 96 releases
makecommerce / shipping / label.php

label.php in MakeCommerce for WooCommerce trunk, at shipping/label.php

316 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace MakeCommerce\Shipping;
4
5 /**
6 * All functionality that has to do with shipping labels and printing
7 *
8 * @since 3.0.0
9 */
10
11 class Label extends \MakeCommerce\Shipping {
12
13 private $loader;
14
15 //page format used for creating labels
16 public $page_format = "A4";
17
18 /**
19 * Constructs Label class, defines loader and hooks
20 *
21 * @since 3.0.0
22 */
23 public function __construct( \MakeCommerce\Loader $loader ) {
24
25 $this->loader = $loader;
26
27 $this->set_print_page_format();
28
29 $this->define_hooks();
30 }
31
32 /**
33 * Set label print page format
34 *
35 * @since 3.0.4
36 */
37 public function set_print_page_format() {
38
39 $this->page_format = get_option( 'mk_label_format', 'A4' );
40 }
41
42 /**
43 * Define all wordpress hooks needed for printing stuff and creating labels
44 *
45 * @since 3.0.0
46 */
47 public function define_hooks() {
48
49 $this->loader->add_action( 'woocommerce_order_actions_end', $this, 'print_button' );
50 $this->loader->add_action( 'wp_ajax_print_pml', $this, 'print' );
51 $this->loader->add_filter( 'admin_action_parcel_machine_print_labels', $this, 'bulk_print' );
52 $this->loader->add_filter( 'admin_footer', $this, 'bulk_print_and_register' );
53 $this->loader->add_filter( 'admin_action_parcel_machine_labels', $this, 'bulk_register' );
54 // HPOS bulk action handling
55 $this->loader->add_filter( 'handle_bulk_actions-woocommerce_page_wc-orders', $this, 'handle_bulk_action' );
56 }
57
58 /**
59 * Creates shipping label using MakeCommerce API and redirect to the page where it is available or return data
60 *
61 * @since 3.0.0
62 */
63 public function create_labels( $post_ids, $ajax = false, $hpos = false ) {
64
65 if ( !is_array( $post_ids ) ) {
66 $post_ids = [$post_ids];
67 }
68
69 $this->initalize();
70
71 $shipping_request = ['credentials' => [], 'orders' => [], 'printFormat' => $this->page_format];
72
73 foreach ( $this->shipping_methods as $shipping_method ) {
74 $shipping_classes_map[$shipping_method["method"]] = $shipping_method["class"];
75 }
76
77 $missing_ids = [];
78
79 foreach ( $post_ids as $post_id ) {
80 $order = wc_get_order( $post_id );
81 $shipment_id = $order->get_meta( '_parcel_machine_shipment_id', true );
82
83 if ( !$shipment_id ) {
84 $missing_ids[] = $post_id;
85 continue;
86 }
87
88 $shipping_methods = $order->get_shipping_methods();
89
90 if ( empty( $shipping_methods ) ) {
91 continue;
92 }
93
94 foreach ( $shipping_methods as $shipping_method ) {
95
96 $shipping_id = explode( ':', $shipping_method['method_id'] );
97 $shipping_class = $shipping_id[0];
98 $shipping_instance = !empty( $shipping_id[1] ) ? $shipping_id[1] : null;
99
100 if ( empty( $shipping_classes_map[$shipping_class] ) ) {
101 continue;
102 }
103
104 $transport_class = new $shipping_classes_map[$shipping_class]($shipping_instance);
105 $carrier_uc = mb_strtoupper( $transport_class->carrier_id );
106
107 //change carrier_uc if it has been set in the interface.
108 if ( isset( $transport_class->settings['service_carrier'] ) ) {
109 $carrier_uc = $transport_class->settings['service_carrier'];
110 }
111
112 if ( empty( $shipping_request['credentials'][$carrier_uc] ) ) {
113 $shipping_request = $this->set_shipping_request_credentials( $carrier_uc, $transport_class, $shipping_request );
114 // Move on to next post if the function returned empty values
115 if ( empty( $shipping_request['credentials'][$carrier_uc] ) && empty( $transport_class->settings['use_mk_contract'] ) ) {
116 continue;
117 }
118 }
119
120 $sr_order = $this->set_sr_order_data(
121 $order,
122 $transport_class,
123 $shipping_class,
124 $carrier_uc,
125 $shipment_id,
126 true,
127 );
128
129 if ( !$sr_order ) continue;
130
131 if ( $carrier_uc === 'LP_EXPRESS_LT' ) {
132 $identifier = $order->get_meta( '_lp_express_cart_identifier', true );
133 $sr_order['lpExpressShipmentDetails']['lpExpressCartIdentifier'] = $identifier;
134 }
135
136 $shipping_request['orders'][] = $sr_order;
137 }
138 }
139
140 $shipping_request['credentials'] = array_values( $shipping_request['credentials'] );
141
142 if ( empty( $shipping_request['orders'] ) ) {
143 return;
144 }
145
146 $MK = \MakeCommerce::get_api();
147
148 if ( !$MK ) {
149 return;
150 }
151
152 try {
153 $response = $MK->createLabels( $shipping_request );
154 } catch ( \Exception $e ) {
155
156 echo $e->getMessage();
157 exit();
158 }
159
160 if ( empty( $response->labelUrl ) ) {
161 return;
162 }
163
164 if ( $ajax ) {
165 return $response->labelUrl;
166 }
167
168 $mk_err = false;
169 if ( !empty( $missing_ids ) ) {
170 $mk_err = sprintf( __( 'Orders %s did not have shipment ID attached so no labels was printed! Please register those packages if you think this is an error!', 'wc_makecommerce_domain' ), join( ', ', $missing_ids ) );
171 }
172
173 if ( $hpos ) {
174 $sendback = add_query_arg( ['page' => 'wc-orders', 'mk_pdf' => urlencode( $response->labelUrl ), 'mk_err' => urlencode( $mk_err ) ], '' );
175 } else {
176 $sendback = add_query_arg( ['post_type' => 'shop_order', 'mk_pdf' => urlencode( $response->labelUrl ), 'mk_err' => urlencode( $mk_err ) ], '' );
177 }
178
179 wp_redirect( esc_url_raw( $sendback ) );
180
181 exit();
182 }
183
184 /**
185 * Prints labels using ajax function in admin
186 *
187 * @since 3.0.0
188 */
189 public function print() {
190
191 if ( !current_user_can( 'manage_woocommerce' ) ) {
192 die();
193 }
194
195 echo $this->create_labels( [intval( $_POST['id'] )], true );
196
197 die();
198 }
199
200 /**
201 * Prints labels for all selected orders
202 *
203 * @since 3.0.0
204 */
205 public function bulk_print() {
206
207 $post_ids = array_map( 'absint', ( array )$_REQUEST['post'] );
208
209 $this->create_labels( $post_ids );
210 }
211
212 /**
213 * Adds bulk shipment register and printing actions to order view
214 *
215 * @since 3.0.0
216 */
217 public function bulk_print_and_register() {
218
219 global $post_type;
220 $enqueue = false;
221
222 //Should this even be done using JS???
223 if ( 'shop_order' === $post_type ) {
224
225 $args = [
226 'shipments_text' => __( 'Register parcel machine shipments', 'wc_makecommerce_domain' ),
227 'labels_text' => __( 'Print parcel machine labels', 'wc_makecommerce_domain' )
228 ];
229 $enqueue = true;
230 }
231 if ( !empty( $_GET['page'] ) && $_GET['page'] == 'wc-orders' ) {
232 $args = [ 'hpos' => true ];
233 $enqueue = true;
234 }
235
236 if ( $enqueue ) {
237
238 if ( !empty( $_REQUEST['mk_err'] ) ) {
239 $args["error"] = htmlspecialchars( $_REQUEST['mk_err'] );
240 }
241
242 if ( !empty( $_REQUEST['mk_pdf'] ) ) {
243 $args["pdf"] = $_REQUEST['mk_pdf'];
244 }
245
246 \MakeCommerce::mc_enqueue_script(
247 'MC_LABEL_BULK_ACTIONS',
248 dirname( __FILE__ ) . '/js/label_bulk_actions.js',
249 $args,
250 [ 'jquery' ]
251 );
252 }
253 }
254
255 /**
256 * Registers shipments in bulk (admin action)
257 *
258 * @since 3.0.0
259 */
260 public function bulk_register() {
261
262 $post_ids = array_map( 'absint', ( array )$_REQUEST['post'] );
263
264 if ( !empty($post_ids) ) {
265 $this->register_shipment( $post_ids );
266 }
267 }
268
269 /**
270 * Supporting function for print_labels. Creates needed javasript for printing button
271 *
272 * @since 3.0.0
273 */
274 public function print_button( $post_id ) {
275 $order = wc_get_order( $post_id );
276
277 if ( !$order->get_meta( '_parcel_machine_shipment_id', true ) ) {
278 return;
279 }
280
281 echo '
282 <li class="wide">
283 <a id="print_parcel_machine_label" href="#" class="button">'. __( 'Print parcel label', 'wc_makecommerce_domain' ) .'</a>
284 <img class="mc_loading" src="' . site_url( '/wp-admin/images/loading.gif' ).'">
285 </li>';
286
287 \MakeCommerce::mc_enqueue_script(
288 'MC_LABEL_BUTTON',
289 dirname( __FILE__ ) . '/js/print_label.js',
290 [
291 'post_id' => $post_id,
292 'error' => __( 'Something went wrong generating a label for this order. Please try again! Contact us, if the problem persists.', 'wc_makecommerce_domain' )
293 ],
294 [ 'jquery' ]
295 );
296 }
297
298 /**
299 * Handles bulk actions when HPOS is enabled for WooCommerce
300 *
301 * @since 3.4.0
302 */
303 public function handle_bulk_action( $page ) {
304 $ids = array_map( 'absint', ( array )$_REQUEST['id'] );
305
306 if ( !empty( $ids ) ) {
307
308 if ( $_GET['action'] == 'parcel_machine_register_orders' ) {
309 $this->register_shipment( $ids );
310 } elseif ( $_GET['action'] == 'parcel_machine_print_labels') {
311 $this->create_labels( $ids, false, true );
312 }
313 }
314 }
315 }
316