$location */ public static function write( WC_Order $order, array $location ): void { $order->update_meta_data( self::META_LOCATION_ID, (int) $location['id'] ); $order->update_meta_data( self::META_LOCATION_NAME, (string) $location['locationName'] ); $order->update_meta_data( self::META_LOCATION_ADDRESS, ! empty( $location['addressActive'] ) ? (string) $location['addressText'] : '' ); $order->update_meta_data( self::META_LOCATION_ESTIMATE, ! empty( $location['estimatePickupTimeActive'] ) ? (string) $location['estimatePickupTimeText'] : '' ); $order->update_meta_data( self::META_LOCATION_INSTRUCTIONS, ! empty( $location['instructionsActive'] ) ? (string) $location['instructionText'] : '' ); // Explicit strings survive a database roundtrip without turning false // into an empty value indistinguishable from absent legacy metadata. $order->update_meta_data( self::META_SHOW_ON_THANK_YOU, ! empty( $location['showOnThankYouPage'] ) ? '1' : '0' ); $order->update_meta_data( self::META_SHOW_IN_EMAILS, ! empty( $location['showInEmails'] ) ? '1' : '0' ); } public static function clear( WC_Order $order ): void { foreach ( [ self::META_LOCATION_ID, self::META_LOCATION_NAME, self::META_LOCATION_ADDRESS, self::META_LOCATION_ESTIMATE, self::META_LOCATION_INSTRUCTIONS, self::META_SHOW_ON_THANK_YOU, self::META_SHOW_IN_EMAILS ] as $key ) { $order->delete_meta_data( $key ); } } /** * @return array|null */ public static function from_order( WC_Order $order ): ?array { $location_id = absint( (string) $order->get_meta( self::META_LOCATION_ID, true ) ); $location_name = trim( (string) $order->get_meta( self::META_LOCATION_NAME, true ) ); if ( '' === $location_name && $location_id > 0 ) { $active_location = LocalPickupResolver::get_active_location( $location_id ); if ( is_array( $active_location ) ) { return $active_location; } } if ( '' === $location_name ) { return null; } return [ 'id' => $location_id, 'locationName' => $location_name, 'addressActive' => '' !== trim( (string) $order->get_meta( self::META_LOCATION_ADDRESS, true ) ), 'addressText' => trim( (string) $order->get_meta( self::META_LOCATION_ADDRESS, true ) ), 'estimatePickupTimeActive' => '' !== trim( (string) $order->get_meta( self::META_LOCATION_ESTIMATE, true ) ), 'estimatePickupTimeText' => trim( (string) $order->get_meta( self::META_LOCATION_ESTIMATE, true ) ), 'instructionsActive' => '' !== trim( (string) $order->get_meta( self::META_LOCATION_INSTRUCTIONS, true ) ), 'instructionText' => trim( (string) $order->get_meta( self::META_LOCATION_INSTRUCTIONS, true ) ), 'showOnThankYouPage' => self::read_bool_meta( $order, self::META_SHOW_ON_THANK_YOU, true ), 'showInEmails' => self::read_bool_meta( $order, self::META_SHOW_IN_EMAILS, true ), ]; } private static function read_bool_meta( WC_Order $order, string $meta_key, bool $default ): bool { $value = $order->get_meta( $meta_key, true ); if ( ! $order->meta_exists( $meta_key ) ) { return $default; } return rest_sanitize_boolean( $value ); } }