get_meta( OrderMeta::STATUS_AT ); $previous = (string) $order->get_meta( OrderMeta::STATUS ); if ( $at < $last_at || ( $at === $last_at && $status === $previous ) ) { return false; } $order->update_meta_data( OrderMeta::STATUS, $status ); $order->update_meta_data( OrderMeta::STATUS_AT, $at ); if ( 'CANCELED' === $status ) { $order->update_meta_data( OrderMeta::STATE, OrderMeta::CANCELED ); } $order->save(); if ( $status === $previous ) { return true; } $order->add_order_note( /* translators: 1: status, 2: AWB. */ sprintf( __( 'ABS Courier: shipment %2$s is now "%1$s".', 'abs-courier' ), Statuses::label( $status ), $order->get_meta( OrderMeta::AWB ) ) ); if ( 'DELIVERED' === $status && Settings::get( 'delivered_to_completed' ) && ! $order->has_status( array( 'completed', 'cancelled', 'refunded', 'failed' ) ) ) { $order->update_status( 'completed', __( 'ABS Courier reported the shipment delivered.', 'abs-courier' ) ); } return true; } /** * Job: checks open shipments with ABS. * * @return void */ public static function reconcile() { $api_key = Credentials::get(); if ( '' === $api_key ) { return; } $orders = self::open_orders(); if ( empty( $orders ) ) { return; } $client = new AbsClient( $api_key ); foreach ( array_chunk( $orders, self::TRACKING_BATCH ) as $chunk ) { try { self::apply_tracking( $client, $chunk ); } catch ( AbsError $e ) { Logger::error( 'Status reconciliation failed', array( 'type' => $e->type(), 'status' => $e->status(), ) ); return; } } } /** * Refreshes one order's status now (the "Refresh status" button). * * @param \WC_Order $order Order. * @return string The current status, or '' when ABS did not return it. * @throws AbsError When ABS cannot be reached. */ public static function refresh( \WC_Order $order ) { self::apply_tracking( new AbsClient( Credentials::get() ), array( $order ) ); return (string) wc_get_order( $order->get_id() )->get_meta( OrderMeta::STATUS ); } /** * Schedules reconciliation (idempotent). * * @return void */ public static function schedule() { if ( function_exists( 'as_has_scheduled_action' ) && ! as_has_scheduled_action( self::HOOK_RECONCILE, array(), Queue::GROUP ) && '' !== Credentials::get() ) { as_schedule_recurring_action( time() + self::INTERVAL, self::INTERVAL, self::HOOK_RECONCILE, array(), Queue::GROUP ); } } /** * Stops reconciliation. * * @return void */ public static function unschedule() { if ( function_exists( 'as_unschedule_all_actions' ) ) { as_unschedule_all_actions( self::HOOK_RECONCILE, array(), Queue::GROUP ); } } /** * Order linked to an AWB (works on HPOS and legacy storage). * * @param string $awb AWB. * @return \WC_Order|null */ public static function order_for_awb( $awb ) { if ( '' === (string) $awb ) { return null; } $orders = wc_get_orders( array( 'limit' => 1, 'meta_key' => OrderMeta::AWB, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key 'meta_value' => (string) $awb, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value ) ); return empty( $orders ) ? null : $orders[0]; } /** * Orders with a shipment that may still change: created in the last 60 days and not final. * * @return \WC_Order[] */ private static function open_orders() { $orders = wc_get_orders( array( 'limit' => self::MAX_ORDERS, 'meta_key' => OrderMeta::STATE, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key 'meta_value' => OrderMeta::CREATED, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value 'date_created' => '>' . ( time() - self::LOOKBACK_DAYS * DAY_IN_SECONDS ), 'orderby' => 'date', 'order' => 'DESC', ) ); return array_values( array_filter( $orders, static function ( $order ) { return '' !== (string) $order->get_meta( OrderMeta::AWB ) && ! in_array( (string) $order->get_meta( OrderMeta::STATUS ), Statuses::FINAL_STATUSES, true ); } ) ); } /** * Fetches tracking for up to 100 orders and applies it. * * @param AbsClient $client Client. * @param \WC_Order[] $orders Orders. * @return void * @throws AbsError On failure. */ private static function apply_tracking( AbsClient $client, array $orders ) { $by_awb = array(); foreach ( $orders as $order ) { $by_awb[ (string) $order->get_meta( OrderMeta::AWB ) ] = $order; } // PHP turns numeric-string keys into integers; ABS rejects the whole request unless every AWB is a string. $data = $client->post( 'tracking/bulk', array( 'awbs' => array_map( 'strval', array_keys( $by_awb ) ) ) ); foreach ( isset( $data['found'] ) && is_array( $data['found'] ) ? $data['found'] : array() as $row ) { $awb = isset( $row['awb'] ) ? (string) $row['awb'] : ''; if ( ! isset( $by_awb[ $awb ], $row['status'] ) ) { continue; } $history = isset( $row['shipmentHistory'] ) && is_array( $row['shipmentHistory'] ) ? $row['shipmentHistory'] : array(); $last = end( $history ); self::apply( $by_awb[ $awb ], (string) $row['status'], is_array( $last ) && isset( $last['date'] ) ? (string) $last['date'] : '' ); } } }