PluginProbe
MakeCommerce for WooCommerce / 3.2.0
MakeCommerce for WooCommerce v3.2.0
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 3.2.0, at shipping/label.php

358 lines 10.2 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 }
55
56 /**
57 * Creates shipping label using MakeCommerce API and redirect to the page where it is available or return data
58 *
59 * @since 3.0.0
60 */
61 public function create_labels( $post_ids, $ajax = false ) {
62
63 if ( !is_array( $post_ids ) ) {
64 $post_ids = array( $post_ids );
65 }
66
67 $this->initalize();
68
69 $shipping_request = array( 'credentials' => array(), 'orders' => array(), 'printFormat' => $this->page_format );
70
71 foreach ( $this->shipping_methods as $shipping_method ) {
72 $shipping_classes_map[$shipping_method["method"]] = $shipping_method["class"];
73 }
74
75 $missing_ids = array();
76
77 foreach ( $post_ids as $post_id ) {
78
79 $shipment_id = get_post_meta( $post_id, '_parcel_machine_shipment_id', true );
80
81 if ( !$shipment_id ) {
82 $missing_ids[] = $post_id;
83 continue;
84 }
85
86 $order = new \WC_Order( $post_id );
87 $shipping_methods = $order->get_shipping_methods();
88
89 if ( empty( $shipping_methods ) ) {
90 continue;
91 }
92
93 foreach ( $shipping_methods as $shipping_method ) {
94
95 $shipping_id = explode( ':', $shipping_method['method_id'] );
96 $shipping_class = $shipping_id[0];
97 $shipping_instance = !empty( $shipping_id[1] ) ? $shipping_id[1] : null;
98
99 if ( empty( $shipping_classes_map[$shipping_class] ) ) {
100 continue;
101 }
102
103 $transport_class = new $shipping_classes_map[$shipping_class]($shipping_instance);
104 $carrier_uc = mb_strtoupper( $transport_class->carrier );
105
106 //apt == parcelmachine
107 if ( $transport_class->type === 'apt' ) {
108
109 $parcel_machine = get_post_meta( $post_id, '_parcel_machine', true );
110
111 if ( !$parcel_machine ) {
112 continue;
113 }
114
115 list( $carrier, $machine_id ) = explode( '||', $parcel_machine );
116
117 if ( !$carrier || !$machine_id ) {
118 continue;
119 }
120 }
121
122 if ( empty( $shipping_request['credentials'][$carrier_uc] ) ) {
123
124 $api_user = $transport_class->settings['service_user'];
125 $api_password = $transport_class->settings['service_password'];
126
127 $use_mk_contract = false;
128
129 if ( isset( $transport_class->settings['use_mk_contract'] ) ) {
130 $use_mk_contract = $transport_class->settings['use_mk_contract'];
131 }
132
133 //change carrier_uc if it has been set in the interface.
134 if ( isset( $transport_class->settings['service_carrier'] ) ) {
135 $carrier_uc = $transport_class->settings['service_carrier'];
136 }
137
138 if ( ( !$api_user || !$api_password ) and ( !$use_mk_contract ) ) {
139
140 $this->loader->add_action( 'admin_notices', $this, 'mk_admin_error' );
141 continue;
142 }
143
144 if ( !$use_mk_contract ) {
145 $shipping_request['credentials'][$carrier_uc] = array( 'carrier' => $carrier_uc, 'username' => $api_user, 'password' => $api_password );
146 }
147 }
148
149 $sender = array(
150 'name' => $transport_class->settings['shop_name'],
151 'phone' => $transport_class->settings['shop_phone'],
152 'email' => $transport_class->settings['shop_email'],
153 'postalCode' => !empty( $transport_class->settings['shop_postal_code'] ) ? $transport_class->settings['shop_postal_code'] : '',
154 'country' => !empty( $transport_class->settings['shop_address_country'] ) ? $transport_class->settings['shop_address_country'] : '',
155 'city' => !empty( $transport_class->settings['shop_address_city'] ) ? $transport_class->settings['shop_address_city'] : '',
156 'street' => !empty( $transport_class->settings['shop_address_street'] ) ? $transport_class->settings['shop_address_street'] : ''
157 );
158
159 $shipping_information = $this->get_order_shipping_information( $order );
160
161 $sr_order = array(
162 'carrier' => $carrier_uc,
163 'orderId' => $order->get_id(),
164 'recipient' => array( 'name' => $shipping_information['recipient_name'], 'phone' => $shipping_information['phone'], 'email' => $shipping_information['email'] ),
165 'sender' => $sender,
166 'shipmentId' => $shipment_id
167 );
168
169 $m_service_type = null;
170
171 if ( $transport_class->carrier === 'omniva' && $transport_class->type === 'atp' ) {
172 $m_service_type = 'PA';
173 } else if ( !empty( $transport_class->settings['registerOnPaymentCode'] ) ) {
174 $m_service_type = $transport_class->settings['registerOnPaymentCode'];
175 }
176
177 if ( $carrier_uc === 'LP_EXPRESS_LT' ) {
178
179 $identifier = get_post_meta( $post_id, '_lp_express_cart_identifier', true );
180 $sr_order['lpExpressShipmentDetails']['lpExpressCartIdentifier'] = $identifier;
181 }
182
183 if ( $transport_class->type === 'cou' || $carrier_uc === 'LP_EXPRESS_LT' ) {
184
185 $sr_order['destination'] = array(
186 'postalCode' => $order->shipping_postcode,
187 'country' => $order->shipping_country,
188 'county' => $order->shipping_state,
189 'city' => $order->shipping_city,
190 'street' => $order->shipping_address_1 . ' ' . $order->shipping_address_2
191 );
192
193 if ( $shipping_class == "courier_smartpost" ) {
194
195 $delivery_time = get_post_meta( $post_id, '_delivery_time', true );
196
197 if ( $delivery_time ) {
198 $sr_order['destination']['timeWindow'] = $delivery_time;
199 }
200 }
201 }
202
203 if ( !empty( $machine_id ) ) {
204 $sr_order['destination']['destinationId'] = $machine_id;
205 }
206
207 if ( $m_service_type ) {
208 $sr_order['services'] = array( 'serviceType' => $m_service_type );
209 }
210
211 $shipping_request['orders'][] = $sr_order;
212 }
213 }
214
215 $shipping_request['credentials'] = array_values( $shipping_request['credentials'] );
216
217 if ( empty( $shipping_request['orders'] ) ) {
218 return;
219 }
220
221 $MK = \MakeCommerce::get_api();
222
223 if ( !$MK ) {
224 return;
225 }
226
227 try {
228 $response = $MK->createLabels( $shipping_request );
229 } catch ( \Exception $e ) {
230
231 echo $e->getMessage();
232 exit();
233 }
234
235 if ( empty( $response->labelUrl ) ) {
236 return;
237 }
238
239 if ( $ajax ) {
240 return $response->labelUrl;
241 }
242
243 $mk_err = false;
244 if ( !empty( $missing_ids ) ) {
245 $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 ) );
246 }
247
248 $sendback = add_query_arg( array( 'post_type' => 'shop_order', 'mk_pdf' => urlencode( $response->labelUrl ), 'mk_err' => urlencode( $mk_err ) ), '' );
249
250 wp_redirect( esc_url_raw( $sendback ) );
251
252 exit();
253 }
254
255 /**
256 * Prints labels using ajax function in admin
257 *
258 * @since 3.0.0
259 */
260 public function print() {
261
262 if ( !current_user_can( 'manage_woocommerce' ) ) {
263 die();
264 }
265
266 echo $this->create_labels( array( intval( $_POST['id'] ) ), true );
267
268 die();
269 }
270
271 /**
272 * Prints labels for all selected orders
273 *
274 * @since 3.0.0
275 */
276 public function bulk_print() {
277
278 $post_ids = array_map( 'absint', ( array )$_REQUEST['post'] );
279
280 $this->create_labels( $post_ids );
281 }
282
283 /**
284 * Adds bulk shipment register and printing actions to order view
285 *
286 * @since 3.0.0
287 */
288 public function bulk_print_and_register() {
289
290 global $post_type;
291
292 //Should this even be done using JS???
293 if ( 'shop_order' === $post_type ) {
294
295 $args = [
296 'shipments_text' => __( 'Register parcel machine shipments', 'wc_makecommerce_domain' ),
297 'labels_text' => __( 'Print parcel machine labels', 'wc_makecommerce_domain' )
298 ];
299
300 if ( !empty( $_REQUEST['mk_err'] ) ) {
301 $args["error"] = htmlspecialchars( $_REQUEST['mk_err'] );
302 }
303
304 if ( !empty( $_REQUEST['mk_pdf'] ) ) {
305 $args["pdf"] = $_REQUEST['mk_pdf'];
306 }
307
308 \MakeCommerce::mc_enqueue_script(
309 'MC_LABEL_BULK_ACTIONS',
310 dirname( __FILE__ ) . '/js/label_bulk_actions.js',
311 $args,
312 [ 'jquery' ]
313 );
314 }
315 }
316
317 /**
318 * Registers shipments in bulk (admin action)
319 *
320 * @since 3.0.0
321 */
322 public function bulk_register() {
323
324 $post_ids = array_map( 'absint', ( array )$_REQUEST['post'] );
325
326 if ( !empty($post_ids) ) {
327 $this->register_shipment( $post_ids );
328 }
329 }
330
331 /**
332 * Supporting function for print_labels. Creates needed javasript for printing button
333 *
334 * @since 3.0.0
335 */
336 public function print_button( $post_id ) {
337
338 if ( !get_post_meta( $post_id, '_parcel_machine_shipment_id', true ) ) {
339 return;
340 }
341
342 echo '
343 <li class="wide">
344 <a id="print_parcel_machine_label" href="#" class="button">'. __( 'Print parcel label', 'wc_makecommerce_domain' ) .'</a>
345 <img class="mc_loading" src="' . site_url( '/wp-admin/images/loading.gif' ).'">
346 </li>';
347
348 \MakeCommerce::mc_enqueue_script(
349 'MC_LABEL_BUTTON',
350 dirname( __FILE__ ) . '/js/print_label.js',
351 [
352 'post_id' => $post_id,
353 'error' => __( 'Something went wrong generating a label for this order. Please try again! Contact us, if the problem persists.', 'wc_makecommerce_domain' )
354 ],
355 [ 'jquery' ]
356 );
357 }
358 }