class-wc-connect-shipping-label.php 18 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
<?php

if ( ! class_exists( 'WC_Connect_Shipping_Label' ) ) {

	class WC_Connect_Shipping_Label {

		/**
		 * @var WC_Connect_API_Client
		 */
		protected $api_client;

		/**
		 * @var WC_Connect_Service_Settings_Store
		 */
		protected $settings_store;

		/**
		 * @var WC_Connect_Service_Schemas_Store
		 */
		protected $service_schemas_store;

		/**
		 * @var WC_Connect_Account_Settings
		 */
		protected $account_settings;

		/**
		 * @var WC_Connect_Package_Settings
		 */
		protected $package_settings;

		/**
		 * @var WC_Connect_Continents
		 */
		protected $continents;

		/**
		 * @var array Supported countries by USPS, see: https://webpmt.usps.gov/pmt010.cfm
		 */
		private $supported_countries = array( 'US', 'AS', 'PR', 'VI', 'GU', 'MP', 'UM', 'FM', 'MH' );

		/**
		 * @var array Supported currencies
		 */
		private $supported_currencies = array( 'USD' );

		private $show_metabox = null;

		public function __construct(
			WC_Connect_API_Client $api_client,
			WC_Connect_Service_Settings_Store $settings_store,
			WC_Connect_Service_Schemas_Store $service_schemas_store,
			WC_Connect_Payment_Methods_Store $payment_methods_store
		) {
			$this->api_client            = $api_client;
			$this->settings_store        = $settings_store;
			$this->service_schemas_store = $service_schemas_store;
			$this->account_settings      = new WC_Connect_Account_Settings(
				$settings_store,
				$payment_methods_store
			);
			$this->package_settings      = new WC_Connect_Package_Settings(
				$settings_store,
				$service_schemas_store
			);
			$this->continents            = new WC_Connect_Continents();
		}

		public function get_item_data( WC_Order $order, $item ) {
			$product = WC_Connect_Utils::get_item_product( $order, $item );
			if ( ! $product || ! $product->needs_shipping() ) {
				return null;
			}
			$height = 0;
			$length = 0;
			$weight = $product->get_weight();
			$width  = 0;

			if ( $product->has_dimensions() ) {
				$height = $product->get_height();
				$length = $product->get_length();
				$width  = $product->get_width();
			}
			$parent_product_id = $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id();
			$product_data      = array(
				'height'     => (float) $height,
				'product_id' => $product->get_id(),
				'length'     => (float) $length,
				'quantity'   => 1,
				'weight'     => (float) $weight,
				'width'      => (float) $width,
				'name'       => $this->get_name( $product ),
				'url'        => get_edit_post_link( $parent_product_id, null ),
			);

			if ( $product->is_type( 'variation' ) ) {
				$product_data['attributes'] = wc_get_formatted_variation( $product, true );
			}

			return $product_data;
		}

		protected function get_packaging_from_shipping_method( $shipping_method ) {
			if ( ! $shipping_method || ! isset( $shipping_method['wc_connect_packages'] ) ) {
				return array();
			}

			$packages_data = $shipping_method['wc_connect_packages'];
			if ( ! $packages_data ) {
				return array();
			}

			// WC3 retrieves metadata as non-scalar values.
			if ( is_array( $packages_data ) ) {
				return $packages_data;
			}

			// WC2.6 stores non-scalar values as string, but doesn't deserialize it on retrieval.
			$packages = maybe_unserialize( $packages_data );
			if ( is_array( $packages ) ) {
				return $packages;
			}

			// legacy WCS stored the labels as JSON.
			$packages = json_decode( $packages_data, true );
			if ( $packages ) {
				return $packages;
			}

			$packages_data = $this->settings_store->try_recover_invalid_json_string( 'box_id', $packages_data );
			$packages      = json_decode( $packages_data, true );
			if ( $packages ) {
				return $packages;
			}

			return array();
		}

		protected function get_packaging_metadata( WC_Order $order ) {
			$shipping_methods = $order->get_shipping_methods();
			$shipping_method  = reset( $shipping_methods );
			$packaging        = $this->get_packaging_from_shipping_method( $shipping_method );

			if ( is_array( $packaging ) ) {
				return array_filter( $packaging );
			}

			return array();
		}

		protected function get_name( WC_Product $product ) {
			if ( $product->get_sku() ) {
				$identifier = $product->get_sku();
			} else {
				$identifier = '#' . $product->get_id();
			}
			return sprintf( '%s - %s', $identifier, $product->get_title() );
		}

		public function get_selected_packages( WC_Order $order ) {
			$packages = $this->get_packaging_metadata( $order );
			if ( ! $packages ) {
				$items  = $this->get_all_items( $order );
				$weight = array_sum( wp_list_pluck( $items, 'weight' ) );

				$packages = array(
					'default_box' => array(
						'id'     => 'default_box',
						'box_id' => 'not_selected',
						'height' => 0,
						'length' => 0,
						'weight' => $weight,
						'width'  => 0,
						'items'  => $items,
					),
				);
			}

			$formatted_packages = array();

			foreach ( $packages as $package_obj ) {
				$package                           = (array) $package_obj;
				$package_id                        = $package['id'];
				$formatted_packages[ $package_id ] = $package;

				foreach ( $package['items'] as $item_index => $item ) {
					$product_data = (array) $item;
					$product      = WC_Connect_Utils::get_item_product( $order, $product_data );

					if ( $product ) {
						$parent_product_id    = $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id();
						$product_data['name'] = $this->get_name( $product );
						$product_data['url']  = get_edit_post_link( $parent_product_id, null );
						if ( $product->is_type( 'variation' ) ) {
							$product_data['attributes'] = wc_get_formatted_variation( $product, true );
						}
						$customs_info = $product->get_meta( 'wc_connect_customs_info', true );
						if ( is_array( $customs_info ) ) {
							$product_data = array_merge( $product_data, $customs_info );
						}
					} else {
						$product_data['name'] = WC_Connect_Utils::get_product_name_from_order( $product_data['product_id'], $order );
					}
					$product_data['value'] = WC_Connect_Utils::get_product_price_from_order( $product_data['product_id'], $order );
					if ( ! isset( $product_data['value'] ) ) {
						$product_data['value'] = 0;
					}

					$formatted_packages[ $package_id ]['items'][ $item_index ] = $product_data;
				}
			}

			return $formatted_packages;
		}

		public function get_all_items( WC_Order $order ) {
			if ( $this->get_packaging_metadata( $order ) ) {
				return array();
			}

			$items = array();
			foreach ( $order->get_items() as $item ) {
				$item_data = $this->get_item_data( $order, $item );
				if ( null === $item_data ) {
					continue;
				}

				$refunded_qty = $order->get_qty_refunded_for_item( $item->get_id() );

				for ( $i = 0; $i < ( $item['qty'] - absint( $refunded_qty ) ); $i ++ ) {
					$items[] = $item_data;
				}
			}

			return $items;
		}

		public function get_selected_rates( WC_Order $order ) {
			$shipping_methods = $order->get_shipping_methods();
			$shipping_method  = reset( $shipping_methods );
			$packages         = $this->get_packaging_from_shipping_method( $shipping_method );
			$rates            = array();

			foreach ( $packages as $idx => $package_obj ) {
				$package = (array) $package_obj;
				// Abort if the package data is malformed
				if ( ! isset( $package['id'] ) || ! isset( $package['service_id'] ) ) {
					return array();
				}

				$rates[ $package['id'] ] = $package['service_id'];
			}

			return $rates;
		}

		protected function format_address_for_api( $address ) {
			// Combine first and last name.
			if ( ! isset( $address['name'] ) ) {
				$first_name = isset( $address['first_name'] ) ? trim( $address['first_name'] ) : '';
				$last_name  = isset( $address['last_name'] ) ? trim( $address['last_name'] ) : '';

				$address['name'] = $first_name . ' ' . $last_name;
			}

			// Rename address_1 to address.
			if ( ! isset( $address['address'] ) && isset( $address['address_1'] ) ) {
				$address['address'] = $address['address_1'];
			}

			// Remove now defunct keys.
			unset( $address['first_name'], $address['last_name'], $address['address_1'] );

			return $address;
		}

		protected function get_origin_address() {
			$origin = $this->format_address_for_api( $this->settings_store->get_origin_address() );

			return $origin;
		}

		protected function get_destination_address( WC_Order $order ) {
			$order_address = $order->get_address( 'shipping' );
			$destination   = $this->format_address_for_api( $order_address );

			return $destination;
		}

		protected function get_form_data( WC_Order $order ) {
			$order_id          = $order->get_id();
			$selected_packages = $this->get_selected_packages( $order );
			$is_packed         = ( false !== $this->get_packaging_metadata( $order ) );
			$origin            = $this->get_origin_address();
			$selected_rates    = $this->get_selected_rates( $order );
			$destination       = $this->get_destination_address( $order );

			if ( ! $destination['country'] ) {
				$destination['country'] = $origin['country'];
			}

			$origin_normalized      = (bool) WC_Connect_Options::get_option( 'origin_address', false );
			$destination_normalized = (bool) $order->get_meta( '_wc_connect_destination_normalized', true );

			$form_data = compact( 'is_packed', 'selected_packages', 'origin', 'destination', 'origin_normalized', 'destination_normalized' );

			$form_data['rates'] = array(
				'selected' => (object) $selected_rates,
			);

			$form_data['order_id'] = $order_id;

			return $form_data;
		}

		/**
		 * Check whether the given order is eligible for shipping label creation - the order has at least one product that is:
		 * - Shippable.
		 * - Non-refunded.
		 *
		 * @param WC_Order $order The order to check for shipping label creation eligibility.
		 * @return bool Whether the given order is eligible for shipping label creation.
		 */
		public function is_order_eligible_for_shipping_label_creation( WC_Order $order ) {
			// Set up a dictionary from product ID to quantity in the order, which will be updated by refunds and existing labels later.
			$quantities_by_product_id = array();
			foreach ( $order->get_items() as $item ) {
				$product = WC_Connect_Utils::get_item_product( $order, $item );
				if ( $product && $product->needs_shipping() ) {
					$product_id                              = $product->get_id();
					$current_quantity                        = array_key_exists( $product_id, $quantities_by_product_id ) ? $quantities_by_product_id[ $product_id ] : 0;
					$quantities_by_product_id[ $product_id ] = $current_quantity + $item->get_quantity();
				}
			}

			// A shipping label cannot be created without a shippable product.
			if ( empty( $quantities_by_product_id ) ) {
				return false;
			}

			// Update the quantity for each refunded product ID in the order.
			foreach ( $order->get_refunds() as $refund ) {
				foreach ( $refund->get_items() as $refunded_item ) {
					$product = WC_Connect_Utils::get_item_product( $order, $refunded_item );
					if ( ! is_a( $product, 'WC_Product' ) ) {
						continue;
					}

					$product_id = $product->get_id();
					if ( array_key_exists( $product_id, $quantities_by_product_id ) ) {
						$current_count                           = $quantities_by_product_id[ $product_id ];
						$quantities_by_product_id[ $product_id ] = $current_count - abs( $refunded_item->get_quantity() );
					}
				}
			}

			// The order is eligible for shipping label creation when there is at least one product with positive quantity.
			foreach ( $quantities_by_product_id as $product_id => $quantity ) {
				if ( $quantity > 0 ) {
					return true;
				}
			}

			return false;
		}

		/**
		 * Check whether the store is eligible for shipping label creation:
		 * - Store currency is supported.
		 * - Store country is supported.
		 *
		 * @return bool Whether the WC store is eligible for shipping label creation.
		 */
		public function is_store_eligible_for_shipping_label_creation() {
			$base_currency = get_woocommerce_currency();
			if ( ! $this->is_supported_currency( $base_currency ) ) {
				return false;
			}

			$base_location = wc_get_base_location();
			if ( ! $this->is_supported_country( $base_location['country'] ) ) {
				return false;
			}

			return true;
		}

		/**
		 * Check whether the given country code is supported for shipping labels.
		 *
		 * @param string $country_code Country code of the WC store.
		 * @return bool Whether the given country code is supported for shipping labels.
		 */
		private function is_supported_country( $country_code ) {
			return in_array( $country_code, $this->supported_countries, true );
		}

		/**
		 * Check whether the given currency code is supported for shipping labels.
		 *
		 * @param string $currency_code Currency code of the WC store.
		 * @return bool Whether the given country code is supported for shipping labels.
		 */
		private function is_supported_currency( $currency_code ) {
			return in_array( $currency_code, $this->supported_currencies, true );
		}

		public function is_dhl_express_available() {
			$dhl_express = $this->service_schemas_store->get_service_schema_by_id( 'dhlexpress' );

			return ! ! $dhl_express;
		}

		public function is_order_dhl_express_eligible() {
			if ( ! $this->is_dhl_express_available() ) {
				return false;
			}

			global $post;

			$order = WC_Connect_Compatibility::instance()->init_theorder_object( $post );
			if ( ! $order ) {
				return false;
			}

			$origin      = $this->get_origin_address();
			$destination = $this->get_destination_address( $order );

			return $origin['country'] !== $destination['country'];
		}

		/**
		 * Check if meta boxes should be displayed.
		 *
		 * @param WP_Post $post Post object.
		 * @return boolean
		 */
		public function should_show_meta_box( $post ) {
			if ( null === $this->show_metabox ) {
				$this->show_metabox = $this->calculate_should_show_meta_box( $post );
			}

			return $this->show_metabox;
		}

		/**
		 * Check if meta boxes should be displayed.
		 *
		 * @param WP_Post $post Post object.
		 * @return bool
		 */
		private function calculate_should_show_meta_box( $post ) {
			// not all users have the permission to manage shipping labels.
			// if a request is made to the JS backend and the user doesn't have permission, an error would be displayed.
			if ( ! WC_Connect_Functions::user_can_manage_labels() ) {
				return false;
			}

			$order = WC_Connect_Compatibility::instance()->init_theorder_object( $post );

			if ( ! $order ) {
				return false;
			}

			// If the shipping label is disabled, will remove the meta box.
			if ( ! $this->is_shipping_label_enabled() ) {
				return false;
			}

			// If the order already has purchased labels, show the meta-box no matter what.
			if ( $order->get_meta( 'wc_connect_labels', true ) ) {
				return true;
			}

			// Restrict showing the metabox to supported store countries and currencies.
			if ( ! $this->is_store_eligible_for_shipping_label_creation() ) {
				return false;
			}

			// If the order was created using WCS checkout rates, show the meta-box regardless of the products' state.
			if ( $this->get_packaging_metadata( $order ) ) {
				return true;
			}

			// At this point (no packaging data), only show if there's at least one existing and shippable product.
			foreach ( $order->get_items() as $item ) {
				$product = WC_Connect_Utils::get_item_product( $order, $item );
				if ( $product && $product->needs_shipping() ) {
					return true;
				}
			}

			return false;
		}

		/**
		 * Check whether shipping label feature is enabled from WC Services setting.
		 *
		 * @return bool True if shipping label is enabled from the settings.
		 */
		public function is_shipping_label_enabled() {
			$account_settings = $this->account_settings->get();

			if ( isset( $account_settings['formData']['enabled'] ) && is_bool( $account_settings['formData']['enabled'] ) ) {
				return $account_settings['formData']['enabled'];
			}

			return true;
		}

		public function get_label_payload( $post_order_or_id ) {
			$order = wc_get_order( $post_order_or_id );
			if ( ! is_a( $order, 'WC_Order' ) ) {
				return false;
			}

			$order_id = $order->get_id();
			$payload  = array(
				'orderId'            => $order_id,
				'paperSize'          => $this->settings_store->get_preferred_paper_size(),
				'formData'           => $this->get_form_data( $order ),
				'labelsData'         => $this->settings_store->get_label_order_meta_data( $order_id ),
				'storeOptions'       => $this->settings_store->get_store_options(),
				// for backwards compatibility, still disable the country dropdown for calypso users with older plugin versions.
				'canChangeCountries' => true,
			);

			return $payload;
		}

		/**
		 * Filter items needing shipping callback.
		 *
		 * @since  3.0.0
		 * @param  array $item Item to check for shipping.
		 * @return bool
		 */
		public function filter_items_needing_shipping( $item ) {
			$product = $item->get_product();
			return $product && $product->needs_shipping();
		}

		/**
		 * Reduce items to sum their quantities.
		 *
		 * @param  int   $sum  Current sum.
		 * @param  array $item Item to add to sum.
		 * @return int
		 */
		protected function reducer_items_quantity( $sum, $item ) {
			return $sum + $item->get_quantity();
		}

		public function meta_box( $post, $args ) {

			$connect_order_presenter = new WC_Connect_Order_Presenter();
			$order                   = WC_Connect_Compatibility::instance()->init_theorder_object( $post );
			$items                   = array_filter( $order->get_items(), array( $this, 'filter_items_needing_shipping' ) );
			$items_count             = array_reduce( $items, array( $this, 'reducer_items_quantity' ), 0 ) - absint( $order->get_item_count_refunded() );
			$payload                 = apply_filters(
				'wc_connect_meta_box_payload',
				array(
					'order'             => $connect_order_presenter->get_order_for_api( $order ),
					'accountSettings'   => $this->account_settings->get(),
					'packagesSettings'  => $this->package_settings->get(),
					'shippingLabelData' => $this->get_label_payload( $order->get_id() ),
					'continents'        => $this->continents->get(),
					'context'           => $args['args']['context'],
					'items'             => $items_count,
				),
				$args,
				$order,
				$this
			);

			do_action( 'enqueue_wc_connect_script', 'wc-connect-create-shipping-label', $payload );
		}
	}
}