get_total(); $shipment = array( 'consigneeName' => self::cut( $name, 255 ), 'consigneePhone' => $phone, 'districtId' => (int) $district_id, 'addressText' => self::cut( $address_text, self::MAX_TEXT ), 'streetName' => self::cut( $address['address_1'], 255 ), 'ref' => OrderMeta::ref( $order ), 'packageType' => (string) $settings['package_type'], 'weight' => self::weight_kg( $order, (float) $settings['default_weight_kg'] ), 'noOfPcs' => max( 1, (int) $order->get_item_count() ), 'contents' => self::cut( self::contents( $order ), self::MAX_TEXT ), 'cash' => self::cash( $order, (array) $settings['cod_gateways'] ), 'itemValue' => round( $total, 2 ), 'productId' => (int) $settings['product_id'], 'specialInstructions' => self::cut( (string) $order->get_customer_note(), self::MAX_TEXT ), ); $email = (string) $order->get_billing_email(); if ( is_email( $email ) ) { $shipment['consigneeEmail'] = $email; } if ( ! empty( $settings['shipment_service_ids'] ) ) { $shipment['shipmentServiceIds'] = array_values( array_map( 'intval', (array) $settings['shipment_service_ids'] ) ); } return $shipment; } /** * Cash the courier collects: the order total minus refunds for cash-on-delivery * payment methods, otherwise 0 (already paid). * * @param \WC_Order $order Order. * @param string[] $cod_gateways Payment method IDs treated as cash on delivery. * @return float */ public static function cash( \WC_Order $order, array $cod_gateways ) { if ( ! in_array( $order->get_payment_method(), $cod_gateways, true ) ) { return 0.0; } return round( max( 0.0, (float) $order->get_total() - (float) $order->get_total_refunded() ), 2 ); } /** * Total weight in kg from product weights; the default when none are set. * * @param \WC_Order $order Order. * @param float $default_kg Default weight in kg. * @return float */ public static function weight_kg( \WC_Order $order, $default_kg ) { $unit = (string) get_option( 'woocommerce_weight_unit', 'kg' ); $total = 0.0; foreach ( $order->get_items() as $item ) { $product = $item instanceof \WC_Order_Item_Product ? $item->get_product() : null; if ( $product && is_numeric( $product->get_weight() ) ) { $total += self::to_kg( (float) $product->get_weight(), $unit ) * (int) $item->get_quantity(); } } return $total > 0 ? round( $total, 2 ) : round( (float) $default_kg, 2 ); } /** * Converts a weight to kg. * * @param float $weight Weight. * @param string $unit WooCommerce unit: kg, g, lbs or oz. * @return float */ public static function to_kg( $weight, $unit ) { switch ( $unit ) { case 'g': return $weight / 1000; case 'lbs': return $weight * 0.45359237; case 'oz': return $weight * 0.028349523125; default: return $weight; } } /** * Delivery address: shipping when present, otherwise billing. Shipping phone falls back to billing phone. * * @param \WC_Order $order Order. * @return array */ public static function address( \WC_Order $order ) { $type = $order->has_shipping_address() ? 'shipping' : 'billing'; $fields = array( 'first_name', 'last_name', 'address_1', 'address_2', 'city', 'state', 'country' ); $out = array(); foreach ( $fields as $field ) { $getter = "get_{$type}_{$field}"; $out[ $field ] = self::cut( (string) $order->$getter(), 200 ); } if ( '' === $out['first_name'] && '' === $out['last_name'] ) { $out['first_name'] = trim( (string) $order->get_billing_first_name() ); $out['last_name'] = trim( (string) $order->get_billing_last_name() ); } $shipping_phone = trim( (string) $order->get_shipping_phone() ); $out['phone'] = '' !== $shipping_phone ? $shipping_phone : trim( (string) $order->get_billing_phone() ); return $out; } /** * Product names, comma-separated. * * @param \WC_Order $order Order. * @return string */ private static function contents( \WC_Order $order ) { $names = array(); foreach ( $order->get_items() as $item ) { $names[] = wp_strip_all_tags( $item->get_name() ); } return implode( ', ', array_filter( $names ) ); } /** * Governorate name for a state code, e.g. "EGC" → "Cairo". * * @param string $country Country code. * @param string $state State code. * @return string */ private static function state_name( $country, $state ) { if ( '' === $state || '' === $country || ! function_exists( 'WC' ) ) { return $state; } $states = WC()->countries->get_states( $country ); return is_array( $states ) && isset( $states[ $state ] ) ? html_entity_decode( $states[ $state ], ENT_QUOTES, 'UTF-8' ) : $state; } /** * Truncates to a character limit. * * @param string $text Text. * @param int $limit Maximum characters. * @return string */ private static function cut( $text, $limit ) { $text = trim( (string) $text ); return mb_strlen( $text, 'UTF-8' ) > $limit ? mb_substr( $text, 0, $limit, 'UTF-8' ) : $text; } }