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

284 lines 7.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 }
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 = [$post_ids];
65 }
66
67 $this->initalize();
68
69 $shipping_request = ['credentials' => [], 'orders' => [], '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 = [];
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 = wc_get_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_id );
105
106 //change carrier_uc if it has been set in the interface.
107 if ( isset( $transport_class->settings['service_carrier'] ) ) {
108 $carrier_uc = $transport_class->settings['service_carrier'];
109 }
110
111 if ( empty( $shipping_request['credentials'][$carrier_uc] ) ) {
112 $shipping_request = $this->set_shipping_request_credentials( $carrier_uc, $transport_class, $shipping_request );
113 // Move on to next post if the function returned empty values
114 if ( empty( $shipping_request['credentials'][$carrier_uc] ) && empty( $transport_class->settings['use_mk_contract'] ) ) {
115 continue;
116 }
117 }
118
119 $sr_order = $this->set_sr_order_data(
120 $order,
121 $post_id,
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
133 $identifier = get_post_meta( $post_id, '_lp_express_cart_identifier', true );
134 $sr_order['lpExpressShipmentDetails']['lpExpressCartIdentifier'] = $identifier;
135 }
136
137 $shipping_request['orders'][] = $sr_order;
138 }
139 }
140
141 $shipping_request['credentials'] = array_values( $shipping_request['credentials'] );
142
143 if ( empty( $shipping_request['orders'] ) ) {
144 return;
145 }
146
147 $MK = \MakeCommerce::get_api();
148
149 if ( !$MK ) {
150 return;
151 }
152
153 try {
154 $response = $MK->createLabels( $shipping_request );
155 } catch ( \Exception $e ) {
156
157 echo $e->getMessage();
158 exit();
159 }
160
161 if ( empty( $response->labelUrl ) ) {
162 return;
163 }
164
165 if ( $ajax ) {
166 return $response->labelUrl;
167 }
168
169 $mk_err = false;
170 if ( !empty( $missing_ids ) ) {
171 $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 ) );
172 }
173
174 $sendback = add_query_arg( ['post_type' => 'shop_order', 'mk_pdf' => urlencode( $response->labelUrl ), 'mk_err' => urlencode( $mk_err ) ], '' );
175
176 wp_redirect( esc_url_raw( $sendback ) );
177
178 exit();
179 }
180
181 /**
182 * Prints labels using ajax function in admin
183 *
184 * @since 3.0.0
185 */
186 public function print() {
187
188 if ( !current_user_can( 'manage_woocommerce' ) ) {
189 die();
190 }
191
192 echo $this->create_labels( [intval( $_POST['id'] )], true );
193
194 die();
195 }
196
197 /**
198 * Prints labels for all selected orders
199 *
200 * @since 3.0.0
201 */
202 public function bulk_print() {
203
204 $post_ids = array_map( 'absint', ( array )$_REQUEST['post'] );
205
206 $this->create_labels( $post_ids );
207 }
208
209 /**
210 * Adds bulk shipment register and printing actions to order view
211 *
212 * @since 3.0.0
213 */
214 public function bulk_print_and_register() {
215
216 global $post_type;
217
218 //Should this even be done using JS???
219 if ( 'shop_order' === $post_type ) {
220
221 $args = [
222 'shipments_text' => __( 'Register parcel machine shipments', 'wc_makecommerce_domain' ),
223 'labels_text' => __( 'Print parcel machine labels', 'wc_makecommerce_domain' )
224 ];
225
226 if ( !empty( $_REQUEST['mk_err'] ) ) {
227 $args["error"] = htmlspecialchars( $_REQUEST['mk_err'] );
228 }
229
230 if ( !empty( $_REQUEST['mk_pdf'] ) ) {
231 $args["pdf"] = $_REQUEST['mk_pdf'];
232 }
233
234 \MakeCommerce::mc_enqueue_script(
235 'MC_LABEL_BULK_ACTIONS',
236 dirname( __FILE__ ) . '/js/label_bulk_actions.js',
237 $args,
238 [ 'jquery' ]
239 );
240 }
241 }
242
243 /**
244 * Registers shipments in bulk (admin action)
245 *
246 * @since 3.0.0
247 */
248 public function bulk_register() {
249
250 $post_ids = array_map( 'absint', ( array )$_REQUEST['post'] );
251
252 if ( !empty($post_ids) ) {
253 $this->register_shipment( $post_ids );
254 }
255 }
256
257 /**
258 * Supporting function for print_labels. Creates needed javasript for printing button
259 *
260 * @since 3.0.0
261 */
262 public function print_button( $post_id ) {
263
264 if ( !get_post_meta( $post_id, '_parcel_machine_shipment_id', true ) ) {
265 return;
266 }
267
268 echo '
269 <li class="wide">
270 <a id="print_parcel_machine_label" href="#" class="button">'. __( 'Print parcel label', 'wc_makecommerce_domain' ) .'</a>
271 <img class="mc_loading" src="' . site_url( '/wp-admin/images/loading.gif' ).'">
272 </li>';
273
274 \MakeCommerce::mc_enqueue_script(
275 'MC_LABEL_BUTTON',
276 dirname( __FILE__ ) . '/js/print_label.js',
277 [
278 'post_id' => $post_id,
279 'error' => __( 'Something went wrong generating a label for this order. Please try again! Contact us, if the problem persists.', 'wc_makecommerce_domain' )
280 ],
281 [ 'jquery' ]
282 );
283 }
284 }