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

352 lines 10.0 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 ( !empty( $machine_id ) ) {
178 $sr_order['destination']['destinationId'] = $machine_id;
179 }
180
181 if ( $transport_class->type === 'cou' ) {
182
183 $sr_order['destination'] = array(
184 'postalCode' => $order->shipping_postcode,
185 'country' => $order->shipping_country,
186 'county' => $order->shipping_state,
187 'city' => $order->shipping_city,
188 'street' => $order->shipping_address_1 . ' ' . $order->shipping_address_2
189 );
190
191 if ( $shipping_class == "courier_smartpost" ) {
192
193 $delivery_time = get_post_meta( $post_id, '_delivery_time', true );
194
195 if ( $delivery_time ) {
196 $sr_order['destination']['timeWindow'] = $delivery_time;
197 }
198 }
199 }
200
201 if ( $m_service_type ) {
202 $sr_order['services'] = array( 'serviceType' => $m_service_type );
203 }
204
205 $shipping_request['orders'][] = $sr_order;
206 }
207 }
208
209 $shipping_request['credentials'] = array_values( $shipping_request['credentials'] );
210
211 if ( empty( $shipping_request['orders'] ) ) {
212 return;
213 }
214
215 $MK = \MakeCommerce::get_api();
216
217 if ( !$MK ) {
218 return;
219 }
220
221 try {
222 $response = $MK->createLabels( $shipping_request );
223 } catch ( \Exception $e ) {
224
225 echo $e->getMessage();
226 exit();
227 }
228
229 if ( empty( $response->labelUrl ) ) {
230 return;
231 }
232
233 if ( $ajax ) {
234 return $response->labelUrl;
235 }
236
237 $mk_err = false;
238 if ( !empty( $missing_ids ) ) {
239 $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 ) );
240 }
241
242 $sendback = add_query_arg( array( 'post_type' => 'shop_order', 'mk_pdf' => urlencode( $response->labelUrl ), 'mk_err' => urlencode( $mk_err ) ), '' );
243
244 wp_redirect( esc_url_raw( $sendback ) );
245
246 exit();
247 }
248
249 /**
250 * Prints labels using ajax function in admin
251 *
252 * @since 3.0.0
253 */
254 public function print() {
255
256 if ( !current_user_can( 'manage_woocommerce' ) ) {
257 die();
258 }
259
260 echo $this->create_labels( array( intval( $_POST['id'] ) ), true );
261
262 die();
263 }
264
265 /**
266 * Prints labels for all selected orders
267 *
268 * @since 3.0.0
269 */
270 public function bulk_print() {
271
272 $post_ids = array_map( 'absint', ( array )$_REQUEST['post'] );
273
274 $this->create_labels( $post_ids );
275 }
276
277 /**
278 * Adds bulk shipment register and printing actions to order view
279 *
280 * @since 3.0.0
281 */
282 public function bulk_print_and_register() {
283
284 global $post_type;
285
286 //Should this even be done using JS???
287 if ( 'shop_order' === $post_type ) {
288
289 $args = [
290 'shipments_text' => __( 'Register parcel machine shipments', 'wc_makecommerce_domain' ),
291 'labels_text' => __( 'Print parcel machine labels', 'wc_makecommerce_domain' )
292 ];
293
294 if ( !empty( $_REQUEST['mk_err'] ) ) {
295 $args["error"] = htmlspecialchars( $_REQUEST['mk_err'] );
296 }
297
298 if ( !empty( $_REQUEST['mk_pdf'] ) ) {
299 $args["pdf"] = $_REQUEST['mk_pdf'];
300 }
301
302 \MakeCommerce::mc_enqueue_script(
303 'MC_LABEL_BULK_ACTIONS',
304 dirname( __FILE__ ) . '/js/label_bulk_actions.js',
305 $args,
306 [ 'jquery' ]
307 );
308 }
309 }
310
311 /**
312 * Registers shipments in bulk (admin action)
313 *
314 * @since 3.0.0
315 */
316 public function bulk_register() {
317
318 $post_ids = array_map( 'absint', ( array )$_REQUEST['post'] );
319
320 if ( !empty($post_ids) ) {
321 $this->register_shipment( $post_ids );
322 }
323 }
324
325 /**
326 * Supporting function for print_labels. Creates needed javasript for printing button
327 *
328 * @since 3.0.0
329 */
330 public function print_button( $post_id ) {
331
332 if ( !get_post_meta( $post_id, '_parcel_machine_shipment_id', true ) ) {
333 return;
334 }
335
336 echo '
337 <li class="wide">
338 <a id="print_parcel_machine_label" href="#" class="button">'. __( 'Print parcel label', 'wc_makecommerce_domain' ) .'</a>
339 <img class="mc_loading" src="' . site_url( '/wp-admin/images/loading.gif' ).'">
340 </li>';
341
342 \MakeCommerce::mc_enqueue_script(
343 'MC_LABEL_BUTTON',
344 dirname( __FILE__ ) . '/js/print_label.js',
345 [
346 'post_id' => $post_id,
347 'error' => __( 'Something went wrong generating a label for this order. Please try again! Contact us, if the problem persists.', 'wc_makecommerce_domain' )
348 ],
349 [ 'jquery' ]
350 );
351 }
352 }