/* translators: %1$d order ID. */ __( 'Unable to load the post record for order %1$d', 'woocommerce' ), $order_id ), array( 'exception_code' => $e->getCode(), 'exception_msg' => $e->getMessage(), 'origin' => __METHOD__, ) ); } } } return $cpt_orders; } /** * Computes whether post has been updated after last order. Tries to do it as efficiently as possible. * * @param \WC_Abstract_Order $order Order object. * @param \WC_Abstract_Order $post_order Order object read from posts table. * * @return bool True if post is different than order. */ private function is_post_different_from_order( $order, $post_order ): bool { if ( ArrayUtil::deep_compare_array_diff( $order->get_base_data(), $post_order->get_base_data(), false ) ) { return true; } $meta_diff = $this->get_diff_meta_data_between_orders( $order, $post_order ); if ( ! empty( $meta_diff ) ) { return true; } return false; } /** * Migrate meta data from post to order. * * @param \WC_Abstract_Order $order Order object. * @param \WC_Abstract_Order $post_order Order object read from posts table. * * @return array List of meta data that was migrated. */ private function migrate_meta_data_from_post_order( \WC_Abstract_Order &$order, \WC_Abstract_Order $post_order ) { $diff = $this->get_diff_meta_data_between_orders( $order, $post_order, true ); $order->save_meta_data(); return $diff; } /** * Helper function to compute diff between metadata of post and cot data for an order. * * Also provides an option to sync the metadata as well, since we are already computing the diff. * * @param \WC_Abstract_Order $order1 Order object read from posts. * @param \WC_Abstract_Order $order2 Order object read from COT. * @param bool $sync Whether to also sync the meta data. * * @return array Difference between post and COT meta data. */ private function get_diff_meta_data_between_orders( \WC_Abstract_Order &$order1, \WC_Abstract_Order $order2, $sync = false ): array { $order1_meta = ArrayUtil::select( $order1->get_meta_data(), 'get_data', ArrayUtil::SELECT_BY_OBJECT_METHOD ); $order2_meta = ArrayUtil::select( $order2->get_meta_data(), 'get_data', ArrayUtil::SELECT_BY_OBJECT_METHOD ); $order1_meta_by_key = ArrayUtil::select_as_assoc( $order1_meta, 'key', ArrayUtil::SELECT_BY_ARRAY_KEY ); $order2_meta_by_key = ArrayUtil::select_as_assoc( $order2_meta, 'key', ArrayUtil::SELECT_BY_ARRAY_KEY ); $diff = array(); foreach ( $order1_meta_by_key as $key => $value ) { if ( in_array( $key, $this->internal_meta_keys, true ) ) { // These should have already been verified in the base data comparison. continue; } $order1_values = ArrayUtil::select( $value, 'value', ArrayUtil::SELECT_BY_ARRAY_KEY ); if ( ! array_key_exists( $key, $order2_meta_by_key ) ) { $sync && $order1->delete_meta_data( $key ); $diff[ $key ] = $order1_values; unset( $order2_meta_by_key[ $key ] ); continue; } $order2_values = ArrayUtil::select( $order2_meta_by_key[ $key ], 'value', ArrayUtil::SELECT_BY_ARRAY_KEY ); $new_diff = ArrayUtil::deep_assoc_array_diff( $order1_values, $order2_values ); if ( ! empty( $new_diff ) && $sync ) { if ( count( $order2_values ) > 1 ) { $sync && $order1->delete_meta_data( $key ); foreach ( $order2_values as $post_order_value ) { $sync && $order1->add_meta_data( $key, $post_order_value, false ); } } else { $sync && $order1->update_meta_data( $key, $order2_values[0] ); } $diff[ $key ] = $new_diff; unset( $order2_meta_by_key[ $key ] ); } } foreach ( $order2_meta_by_key as $key => $value ) { if ( array_key_exists( $key, $order1_meta_by_key ) || in_array( $key, $this->internal_meta_keys, true ) ) { continue; } $order2_values = ArrayUtil::select( $value, 'value', ArrayUtil::SELECT_BY_ARRAY_KEY ); foreach ( $order2_values as $meta_value ) { $sync && $order1->add_meta_data( $key, $meta_value ); } $diff[ $key ] = $order2_values; } return $diff; } /** * Log difference between post and COT data for an order. * * @param array $diff Difference between post and COT data. * * @return void */ private function log_diff( array $diff ): void { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r -- This is a log function. $this->error_logger->notice( 'Diff found: ' . print_r( $diff, true ) ); } /** * Migrate post record from a given order object. * * @param \WC_Abstract_Order $order Order object. * @param \WC_Abstract_Order $post_order Order object read from posts. * * @return void */ private function migrate_post_record( \WC_Abstract_Order &$order, \WC_Abstract_Order $post_order ): void { $this->migrate_meta_data_from_post_order( $order, $post_order ); $post_order_base_data = $post_order->get_base_data(); foreach ( $post_order_base_data as $key => $value ) { $this->set_order_prop( $order, $key, $value ); } $this->persist_updates( $order, false ); } /** * Sets order properties based on a row from the database. * * @param \WC_Abstract_Order $order The order object. * @param object $order_data A row of order data from the database. */ private function set_order_props_from_data( &$order, $order_data ) { foreach ( $this->get_all_order_column_mappings() as $table_name => $column_mapping ) { foreach ( $column_mapping as $column_name => $prop_details ) { if ( ! isset( $prop_details['name'] ) ) { continue; } $prop_value = $order_data->{$prop_details['name']}; if ( is_null( $prop_value ) ) { continue; } if ( 'date' === $prop_details['type'] ) { $prop_value = $this->string_to_timestamp( $prop_value ); } $this->set_order_prop( $order, $prop_details['name'], $prop_value ); } } } /** * Set order prop if a setter exists in either the order object or in the data store. * * @param \WC_Abstract_Order $order Order object. * @param string $prop_name Property name. * @param mixed $prop_value Property value. * * @return bool True if the property was set, false otherwise. */ private function set_order_prop( \WC_Abstract_Order $order, string $prop_name, $prop_value ) { $prop_setter_function_name = "set_{$prop_name}"; if ( is_callable( array( $order, $prop_setter_function_name ) ) ) { return $order->{$prop_setter_function_name}( $prop_value ); } elseif ( is_callable( array( $this, $prop_setter_function_name ) ) ) { return $this->{$prop_setter_function_name}( $order, $prop_value, false ); } return false; } /** * Return order data for a single order ID. * * @param int $id Order ID. * * @return object|\WP_Error DB order object or WP_Error. */ private function get_order_data_for_id( $id ) { $results = $this->get_order_data_for_ids( array( $id ) ); return is_array( $results ) && count( $results ) > 0 ? $results[ $id ] : $results; } /** * Return order data for multiple IDs. * * @param array $ids List of order IDs. * * @return \stdClass[]|object|null DB Order objects or error. */ protected function get_order_data_for_ids( $ids ) { if ( ! $ids ) { return array(); } global $wpdb; if ( empty( $ids ) ) { return array(); } $order_table_query = $this->get_order_table_select_statement(); $id_placeholder = implode( ', ', array_fill( 0, count( $ids ), '%d' ) ); $order_meta_table = self::get_meta_table_name(); // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $order_table_query is autogenerated and should already be prepared. $table_data = $wpdb->get_results( $wpdb->prepare( "$order_table_query WHERE wc_order.id in ( $id_placeholder )", $ids ) ); // phpcs:enable $meta_data_query = $this->get_order_meta_select_statement(); $order_data = array(); $meta_data = $wpdb->get_results( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $meta_data_query and $order_meta_table is autogenerated and should already be prepared. $id_placeholder is already prepared. "$meta_data_query WHERE $order_meta_table.order_id in ( $id_placeholder )", $ids ) ); foreach ( $table_data as $table_datum ) { $order_data[ $table_datum->id ] = $table_datum; $order_data[ $table_datum->id ]->meta_data = array(); } foreach ( $meta_data as $meta_datum ) { // phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- Not a meta query. $order_data[ $meta_datum->order_id ]->meta_data[] = (object) array( 'meta_id' => $meta_datum->id, 'meta_key' => $meta_datum->meta_key, 'meta_value' => $meta_datum->meta_value, ); // phpcs:enable } return $order_data; } /** * Helper method to generate combined select statement. * * @return string Select SQL statement to fetch order. */ private function get_order_table_select_statement() { $order_table = $this::get_orders_table_name(); $order_table_alias = 'wc_order'; $select_clause = $this->generate_select_clause_for_props( $order_table_alias, $this->order_column_mapping ); $billing_address_table_alias = $this->get_address_table_alias( 'billing' ); $shipping_address_table_alias = $this->get_address_table_alias( 'shipping' ); $op_data_table_alias = $this->get_op_table_alias(); $billing_address_clauses = $this->join_billing_address_table_to_order_query( $order_table_alias, $billing_address_table_alias ); $shipping_address_clauses = $this->join_shipping_address_table_to_order_query( $order_table_alias, $shipping_address_table_alias ); $operational_data_clauses = $this->join_operational_data_table_to_order_query( $order_table_alias, $op_data_table_alias ); return " SELECT $select_clause, {$billing_address_clauses['select']}, {$shipping_address_clauses['select']}, {$operational_data_clauses['select']} FROM $order_table $order_table_alias LEFT JOIN {$billing_address_clauses['join']} LEFT JOIN {$shipping_address_clauses['join']} LEFT JOIN {$operational_data_clauses['join']} "; } /** * Helper function to generate select statement for fetching metadata in bulk. * * @return string Select SQL statement to fetch order metadata. */ private function get_order_meta_select_statement() { $order_meta_table = self::get_meta_table_name(); return " SELECT $order_meta_table.id, $order_meta_table.order_id, $order_meta_table.meta_key, $order_meta_table.meta_value FROM $order_meta_table "; } /** * Helper method to generate join query for billing addresses in wc_address table. * * @param string $order_table_alias Alias for order table to use in join. * @param string $address_table_alias Alias for address table to use in join. * * @return array Select and join statements for billing address table. */ private function join_billing_address_table_to_order_query( $order_table_alias, $address_table_alias ) { return $this->join_address_table_order_query( 'billing', $order_table_alias, $address_table_alias ); } /** * Helper method to generate join query for shipping addresses in wc_address table. * * @param string $order_table_alias Alias for order table to use in join. * @param string $address_table_alias Alias for address table to use in join. * * @return array Select and join statements for shipping address table. */ private function join_shipping_address_table_to_order_query( $order_table_alias, $address_table_alias ) { return $this->join_address_table_order_query( 'shipping', $order_table_alias, $address_table_alias ); } /** * Helper method to generate join and select query for address table. * * @param string $address_type Type of address. Typically will be `billing` or `shipping`. * @param string $order_table_alias Alias of order table to use. * @param string $address_table_alias Alias for address table to use. * * @return array Select and join statements for address table. */ private function join_address_table_order_query( $address_type, $order_table_alias, $address_table_alias ) { global $wpdb; $address_table = $this::get_addresses_table_name(); $column_props_map = 'billing' === $address_type ? $this->billing_address_column_mapping : $this->shipping_address_column_mapping; $clauses = $this->generate_select_and_join_clauses( $order_table_alias, $address_table, $address_table_alias, $column_props_map ); // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $clauses['join'] and $address_table_alias are hardcoded. $clauses['join'] = $wpdb->prepare( "{$clauses['join']} AND $address_table_alias.address_type = %s", $address_type ); // phpcs:enable return array( 'select' => $clauses['select'], 'join' => $clauses['join'], ); } /** * Helper method to join order operational data table. * * @param string $order_table_alias Alias to use for order table. * @param string $operational_table_alias Alias to use for operational data table. * * @return array Select and join queries for operational data table. */ private function join_operational_data_table_to_order_query( $order_table_alias, $operational_table_alias ) { $operational_data_table = $this::get_operational_data_table_name(); return $this->generate_select_and_join_clauses( $order_table_alias, $operational_data_table, $operational_table_alias, $this->operational_data_column_mapping ); } /** * Helper method to generate join and select clauses. * * @param string $order_table_alias Alias for order table. * @param string $table Table to join. * @param string $table_alias Alias for table to join. * @param array[] $column_props_map Column to prop map for table to join. * * @return array Select and join queries. */ private function generate_select_and_join_clauses( $order_table_alias, $table, $table_alias, $column_props_map ) { // Add aliases to column names so they will be unique when fetching. $select_clause = $this->generate_select_clause_for_props( $table_alias, $column_props_map ); $join_clause = "$table $table_alias ON $table_alias.order_id = $order_table_alias.id"; return array( 'select' => $select_clause, 'join' => $join_clause, ); } /** * Helper method to generate select clause for props. * * @param string $table_alias Alias for table. * @param array[] $props Props to column mapping for table. * * @return string Select clause. */ private function generate_select_clause_for_props( $table_alias, $props ) { $select_clauses = array(); foreach ( $props as $column_name => $prop_details ) { $select_clauses[] = isset( $prop_details['name'] ) ? "$table_alias.$column_name as {$prop_details['name']}" : "$table_alias.$column_name as {$table_alias}_$column_name"; } return implode( ', ', $select_clauses ); } /** * Persists order changes to the database. * * @param \WC_Abstract_Order $order The order. * @param bool $force_all_fields Force saving all fields to DB and just changed. * * @throws \Exception If order data is not valid. * * @since 6.8.0 */ protected function persist_order_to_db( &$order, bool $force_all_fields = false ) { $context = ( 0 === absint( $order->get_id() ) ) ? 'create' : 'update'; $data_sync = wc_get_container()->get( DataSynchronizer::class ); if ( 'create' === $context ) { $post_id = wp_insert_post( array( 'post_type' => $data_sync->data_sync_is_enabled() ? $order->get_type() : $data_sync::PLACEHOLDER_ORDER_POST_TYPE, 'post_status' => 'draft', ) ); if ( ! $post_id ) { throw new \Exception( __( 'Could not create order in posts table.', 'woocommerce' ) ); } $order->set_id( $post_id ); } $only_changes = ! $force_all_fields && 'update' === $context; // Figure out what needs to be updated in the database. $db_updates = $this->get_db_rows_for_order( $order, $context, $only_changes ); // Persist changes. foreach ( $db_updates as $update ) { // Make sure 'data' and 'format' entries match before passing to $wpdb. ksort( $update['data'] ); ksort( $update['format'] ); $result = $this->database_util->insert_on_duplicate_key_update( $update['table'], $update['data'], array_values( $update['format'] ) ); if ( false === $result ) { // translators: %s is a table name. throw new \Exception( sprintf( __( 'Could not persist order to database table "%s".', 'woocommerce' ), $update['table'] ) ); } } $changes = $order->get_changes(); $this->update_address_index_meta( $order, $changes ); } /** * Generates an array of rows with all the details required to insert or update an order in the database. * * @param \WC_Abstract_Order $order The order. * @param string $context The context: 'create' or 'update'. * @param boolean $only_changes Whether to consider only changes in the order for generating the rows. * * @return array * @throws \Exception When invalid data is found for the given context. * * @since 6.8.0 */ protected function get_db_rows_for_order( \WC_Abstract_Order $order, string $context = 'create', bool $only_changes = false ): array { $result = array(); $row = $this->get_db_row_from_order( $order, $this->order_column_mapping, $only_changes ); if ( 'create' === $context && ! $row ) { throw new \Exception( 'No data for new record.' ); // This shouldn't occur. } if ( $row ) { $result[] = array( 'table' => self::get_orders_table_name(), 'data' => array_merge( $row['data'], array( 'id' => $order->get_id() ) ), 'format' => array_merge( $row['format'], array( 'id' => '%d' ) ), ); } // wc_order_operational_data. $row = $this->get_db_row_from_order( $order, $this->operational_data_column_mapping, $only_changes ); if ( $row ) { $result[] = array( 'table' => self::get_operational_data_table_name(), 'data' => array_merge( $row['data'], array( 'order_id' => $order->get_id() ) ), 'format' => array_merge( $row['format'], array( 'order_id' => '%d' ) ), ); } // wc_order_addresses. foreach ( array( 'billing', 'shipping' ) as $address_type ) { $row = $this->get_db_row_from_order( $order, $this->{$address_type . '_address_column_mapping'}, $only_changes ); if ( $row ) { $result[] = array( 'table' => self::get_addresses_table_name(), 'data' => array_merge( $row['data'], array( 'order_id' => $order->get_id(), 'address_type' => $address_type, ) ), 'format' => array_merge( $row['format'], array( 'order_id' => '%d', 'address_type' => '%s', ) ), ); } } /** * Allow third parties to include rows that need to be inserted/updated in custom tables when persisting an order. * * @since 6.8.0 * * @param array Array of rows to be inserted/updated when persisting an order. Each entry should be an array with * keys 'table', 'data' (the row), 'format' (row format), 'where' and 'where_format'. * @param \WC_Order The order object. * @param string The context of the operation: 'create' or 'update'. */ $ext_rows = apply_filters( 'woocommerce_orders_table_datastore_extra_db_rows_for_order', array(), $order, $context ); return array_merge( $result, $ext_rows ); } /** * Produces an array with keys 'row' and 'format' that can be passed to `$wpdb->update()` as the `$data` and * `$format` parameters. Values are taken from the order changes array and properly formatted for inclusion in the * database. * * @param \WC_Abstract_Order $order Order. * @param array $column_mapping Table column mapping. * @param bool $only_changes Whether to consider only changes in the order object or all fields. * @return array * * @since 6.8.0 */ protected function get_db_row_from_order( $order, $column_mapping, $only_changes = false ) { $changes = $only_changes ? $order->get_changes() : array_merge( $order->get_data(), $order->get_changes() ); $changes['type'] = $order->get_type(); // Make sure 'status' is correct. if ( array_key_exists( 'status', $column_mapping ) ) { $changes['status'] = $this->get_post_status( $order ); } $row = array(); $row_format = array(); foreach ( $column_mapping as $column => $details ) { if ( ! isset( $details['name'] ) || ! array_key_exists( $details['name'], $changes ) ) { continue; } $row[ $column ] = $this->database_util->format_object_value_for_db( $changes[ $details['name'] ], $details['type'] ); $row_format[ $column ] = $this->database_util->get_wpdb_format_for_type( $details['type'] ); } if ( ! $row ) { return false; } return array( 'data' => $row, 'format' => $row_format, ); } //phpcs:disable Squiz.Commenting, Generic.Commenting /** * Method to delete an order from the database. * * @param \WC_Abstract_Order $order Order object. * @param array $args Array of args to pass to the delete method. * * @return void */ public function delete( &$order, $args = array() ) { $order_id = $order->get_id(); if ( ! $order_id ) { return; } if ( ! empty( $args['force_delete'] ) ) { /** * Fires immediately before an order is deleted from the database. * * @since 7.1.0 * * @param int $order_id ID of the order about to be deleted. * @param WC_Order $order Instance of the order that is about to be deleted. */ do_action( 'woocommerce_before_delete_order', $order_id, $order ); $this->delete_order_data_from_custom_order_tables( $order_id ); $order->set_id( 0 ); // If this datastore method is called while the posts table is authoritative, refrain from deleting post data. if ( $order->get_data_store()->get_current_class_name() !== self::class ) { return; } // Delete the associated post, which in turn deletes order items, etc. through {@see WC_Post_Data}. // Once we stop creating posts for orders, we should do the cleanup here instead. wp_delete_post( $order_id ); do_action( 'woocommerce_delete_order', $order_id ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment } else { /** * Fires immediately before an order is trashed. * * @since 7.1.0 * * @param int $order_id ID of the order about to be deleted. * @param WC_Order $order Instance of the order that is about to be deleted. */ do_action( 'woocommerce_before_trash_order', $order_id, $order ); $this->trash_order( $order ); do_action( 'woocommerce_trash_order', $order_id ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment } } /** * Trashes an order. * * @param WC_Order $order The order object * @return void */ public function trash_order( $order ) { global $wpdb; if ( 'trash' === $order->get_status( 'edit' ) ) { return; } $trash_metadata = array( '_wp_trash_meta_status' => 'wc-' . $order->get_status( 'edit' ), '_wp_trash_meta_time' => time(), ); foreach ( $trash_metadata as $meta_key => $meta_value ) { $this->add_meta( $order, (object) array( 'key' => $meta_key, 'value' => $meta_value, ) ); } $wpdb->update( self::get_orders_table_name(), array( 'status' => 'trash', 'date_updated_gmt' => current_time( 'Y-m-d H:i:s', true ), ), array( 'id' => $order->get_id() ), array( '%s', '%s' ), array( '%d' ) ); $order->set_status( 'trash' ); $data_synchronizer = wc_get_container()->get( DataSynchronizer::class ); if ( $data_synchronizer->data_sync_is_enabled() ) { wp_trash_post( $order->get_id() ); } } /** * Attempts to restore the specified order back to its original status (after having been trashed). * * @param WC_Order $order The order to be untrashed. * * @return bool If the operation was successful. */ public function untrash_order( WC_Order $order ): bool { $id = $order->get_id(); $status = $order->get_status(); if ( 'trash' !== $status ) { wc_get_logger()->warning( sprintf( /* translators: 1: order ID, 2: order status */ __( 'Order %1$d cannot be restored from the trash: it has already been restored to status "%2$s".', 'woocommerce' ), $id, $status ) ); return false; } $previous_status = $order->get_meta( '_wp_trash_meta_status' ); $valid_statuses = wc_get_order_statuses(); $previous_state_is_invalid = ! array_key_exists( $previous_status, $valid_statuses ); $pending_is_valid_status = array_key_exists( 'wc-pending', $valid_statuses ); if ( $previous_state_is_invalid && $pending_is_valid_status ) { // If the previous status is no longer valid, let's try to restore it to "pending" instead. wc_get_logger()->warning( sprintf( /* translators: 1: order ID, 2: order status */ __( 'The previous status of order %1$d ("%2$s") is invalid. It has been restored to "pending" status instead.', 'woocommerce' ), $id, $previous_status ) ); $previous_status = 'pending'; } elseif ( $previous_state_is_invalid ) { // If we cannot restore to pending, we should probably stand back and let the merchant intervene some other way. wc_get_logger()->warning( sprintf( /* translators: 1: order ID, 2: order status */ __( 'The previous status of order %1$d ("%2$s") is invalid. It could not be restored.', 'woocommerce' ), $id, $previous_status ) ); return false; } /** * Fires before an order is restored from the trash. * * @since 7.2.0 * * @param int $order_id Order ID. * @param string $previous_status The status of the order before it was trashed. */ do_action( 'woocommerce_untrash_order', $order->get_id(), $previous_status ); $order->set_status( $previous_status ); $order->save(); // Was the status successfully restored? Let's clean up the meta and indicate success... if ( 'wc-' . $order->get_status() === $previous_status ) { $order->delete_meta_data( '_wp_trash_meta_status' ); $order->delete_meta_data( '_wp_trash_meta_time' ); $order->delete_meta_data( '_wp_trash_meta_comments_status' ); $order->save_meta_data(); $data_synchronizer = wc_get_container()->get( DataSynchronizer::class ); if ( $data_synchronizer->data_sync_is_enabled() ) { //The previous $order->save() will have forced a sync to the posts table, //this implies that the post status is not "trash" anymore, and thus //wp_untrash_post would do nothing. wp_update_post( array( 'ID' => $id, 'post_status' => 'trash', ) ); wp_untrash_post( $id ); } return true; } // ...Or log a warning and bail. wc_get_logger()->warning( sprintf( /* translators: 1: order ID, 2: order status */ __( 'Something went wrong when trying to restore order %d from the trash. It could not be restored.', 'woocommerce' ), $id ) ); return false; } /** * Deletes order data from custom order tables. * * @param int $order_id The order ID. * @return void */ public function delete_order_data_from_custom_order_tables( $order_id ) { global $wpdb; // Delete COT-specific data. foreach ( $this->get_all_table_names() as $table ) { $wpdb->delete( $table, ( self::get_orders_table_name() === $table ) ? array( 'id' => $order_id ) : array( 'order_id' => $order_id ), array( '%d' ) ); } } /** * Method to create an order in the database. * * @param \WC_Order $order Order object. */ public function create( &$order ) { if ( '' === $order->get_order_key() ) { $order->set_order_key( wc_generate_order_key() ); } $this->persist_save( $order ); /** * Fires when a new order is created. * * @since 2.7.0 * * @param int Order ID. * @param \WC_Order Order object. */ do_action( 'woocommerce_new_order', $order->get_id(), $order ); } /** * Helper method responsible for persisting new data to order table. * * This should not contain and specific meta or actions, so that it can be used other order types safely. * * @param \WC_Order $order Order object. * * @return void * * @throws \Exception When unable to save data. */ protected function persist_save( &$order, bool $force_all_fields = false, $backfill = true ) { $order->set_version( Constants::get_constant( 'WC_VERSION' ) ); $order->set_currency( $order->get_currency() ? $order->get_currency() : get_woocommerce_currency() ); if ( ! $order->get_date_created( 'edit' ) ) { $order->set_date_created( time() ); } $this->update_order_meta( $order ); $this->persist_order_to_db( $order, $force_all_fields ); $order->save_meta_data(); $order->apply_changes(); if ( $backfill ) { $this->maybe_backfill_post_record( $order ); } $this->clear_caches( $order ); } /** * Method to update an order in the database. * * @param \WC_Order $order */ public function update( &$order ) { // Before updating, ensure date paid is set if missing. if ( ! $order->get_date_paid( 'edit' ) && version_compare( $order->get_version( 'edit' ), '3.0', '<' ) && $order->has_status( apply_filters( 'woocommerce_payment_complete_order_status', $order->needs_processing() ? 'processing' : 'completed', $order->get_id(), $order ) ) // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment ) { $order->set_date_paid( $order->get_date_created( 'edit' ) ); } if ( null === $order->get_date_created( 'edit' ) ) { $order->set_date_created( time() ); } $order->set_version( Constants::get_constant( 'WC_VERSION' ) ); // Fetch changes. $changes = $order->get_changes(); $this->persist_updates( $order ); // Update download permissions if necessary. if ( array_key_exists( 'billing_email', $changes ) || array_key_exists( 'customer_id', $changes ) ) { $data_store = \WC_Data_Store::load( 'customer-download' ); $data_store->update_user_by_order_id( $order->get_id(), $order->get_customer_id(), $order->get_billing_email() ); } // Mark user account as active. if ( array_key_exists( 'customer_id', $changes ) ) { wc_update_user_last_active( $order->get_customer_id() ); } $order->apply_changes(); $this->clear_caches( $order ); do_action( 'woocommerce_update_order', $order->get_id(), $order ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment } /** * Proxy to udpating order meta. Here for backward compatibility reasons. * * @param \WC_Order $order Order object. * * @return void */ protected function update_post_meta( &$order ) { $this->update_order_meta( $order ); } /** * Helper method that is responsible for persisting order updates to the database. * * This is expected to be reused by other order types, and should not contain any specific metadata updates or actions. * * @param \WC_Order $order Order object. * * @return array $changes Array of changes. * * @throws \Exception When unable to persist order. */ protected function persist_updates( &$order, $backfill = true ) { // Fetch changes. $changes = $order->get_changes(); if ( ! isset( $changes['date_modified'] ) ) { $order->set_date_modified( time() ); } if ( $backfill ) { $this->maybe_backfill_post_record( $order ); } $this->persist_order_to_db( $order ); $order->save_meta_data(); return $changes; } /** * Helper function to decide whether to backfill post record. * * @param \WC_Abstract_Order $order Order object. * * @return void */ private function maybe_backfill_post_record( $order ) { $data_sync = wc_get_container()->get( DataSynchronizer::class ); if ( $data_sync->data_sync_is_enabled() ) { $this->backfill_post_record( $order ); } } /** * Helper method that updates post meta based on an order object. * Mostly used for backwards compatibility purposes in this datastore. * * @param \WC_Order $order Order object. * * @since 7.0.0 */ public function update_order_meta( &$order ) { $changes = $order->get_changes(); $this->update_address_index_meta( $order, $changes ); } /** * Helper function to update billing and shipping address metadata. * @param \WC_Abstract_Order $order Order Object * @param array $changes Array of changes. * * @return void */ private function update_address_index_meta( $order, $changes ) { // If address changed, store concatenated version to make searches faster. foreach ( array( 'billing', 'shipping' ) as $address_type ) { if ( isset( $changes[ $address_type ] ) ) { $order->update_meta_data( "_{$address_type}_address_index", implode( ' ', $order->get_address( $address_type ) ) ); } } } /** * Return array of coupon_code => meta_key for coupon which have usage limit and have tentative keys. * Pass $coupon_id if key for only one of the coupon is needed. * * @param WC_Order $order Order object. * @param int $coupon_id If passed, will return held key for that coupon. * * @return array|string Key value pair for coupon code and meta key name. If $coupon_id is passed, returns meta_key for only that coupon. */ public function get_coupon_held_keys( $order, $coupon_id = null ) { $held_keys = $order->get_meta( '_coupon_held_keys' ); if ( $coupon_id ) { return isset( $held_keys[ $coupon_id ] ) ? $held_keys[ $coupon_id ] : null; } return $held_keys; } /** * Return array of coupon_code => meta_key for coupon which have usage limit per customer and have tentative keys. * * @param WC_Order $order Order object. * @param int $coupon_id If passed, will return held key for that coupon. * * @return mixed */ public function get_coupon_held_keys_for_users( $order, $coupon_id = null ) { $held_keys_for_user = $order->get_meta( '_coupon_held_keys_for_users' ); if ( $coupon_id ) { return isset( $held_keys_for_user[ $coupon_id ] ) ? $held_keys_for_user[ $coupon_id ] : null; } return $held_keys_for_user; } /** * Add/Update list of meta keys that are currently being used by this order to hold a coupon. * This is used to figure out what all meta entries we should delete when order is cancelled/completed. * * @param WC_Order $order Order object. * @param array $held_keys Array of coupon_code => meta_key. * @param array $held_keys_for_user Array of coupon_code => meta_key for held coupon for user. * * @return mixed */ public function set_coupon_held_keys( $order, $held_keys, $held_keys_for_user ) { if ( is_array( $held_keys ) && 0 < count( $held_keys ) ) { $order->update_meta_data( '_coupon_held_keys', $held_keys ); } if ( is_array( $held_keys_for_user ) && 0 < count( $held_keys_for_user ) ) { $order->update_meta_data( '_coupon_held_keys_for_users', $held_keys_for_user ); } } /** * Release all coupons held by this order. * * @param WC_Order $order Current order object. * @param bool $save Whether to delete keys from DB right away. Could be useful to pass `false` if you are building a bulk request. */ public function release_held_coupons( $order, $save = true ) { $coupon_held_keys = $this->get_coupon_held_keys( $order ); if ( is_array( $coupon_held_keys ) ) { foreach ( $coupon_held_keys as $coupon_id => $meta_key ) { $coupon = new \WC_Coupon( $coupon_id ); $coupon->delete_meta_data( $meta_key ); $coupon->save_meta_data(); } } $order->delete_meta_data( '_coupon_held_keys' ); $coupon_held_keys_for_users = $this->get_coupon_held_keys_for_users( $order ); if ( is_array( $coupon_held_keys_for_users ) ) { foreach ( $coupon_held_keys_for_users as $coupon_id => $meta_key ) { $coupon = new \WC_Coupon( $coupon_id ); $coupon->delete_meta_data( $meta_key ); $coupon->save_meta_data(); } } $order->delete_meta_data( '_coupon_held_keys_for_users' ); if ( $save ) { $order->save_meta_data(); } } public function query( $query_vars ) { if ( ! isset( $query_vars['paginate'] ) || ! $query_vars['paginate'] ) { $query_vars['no_found_rows'] = true; } if ( isset( $query_vars['anonymized'] ) ) { $query_vars['meta_query'] = $query_vars['meta_query'] ?? array(); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query if ( $query_vars['anonymized'] ) { $query_vars['meta_query'][] = array( 'key' => '_anonymized', 'value' => 'yes', ); } else { $query_vars['meta_query'][] = array( 'key' => '_anonymized', 'compare' => 'NOT EXISTS', ); } } try { $query = new OrdersTableQuery( $query_vars ); } catch ( \Exception $e ) { $query = (object) array( 'orders' => array(), 'found_orders' => 0, 'max_num_pages' => 0, ); } if ( isset( $query_vars['return'] ) && 'ids' === $query_vars['return'] ) { $orders = $query->orders; } else { $orders = WC()->order_factory->get_orders( $query->orders ); } if ( isset( $query_vars['paginate'] ) && $query_vars['paginate'] ) { return (object) array( 'orders' => $orders, 'total' => $query->found_orders, 'max_num_pages' => $query->max_num_pages, ); } return $orders; } public function get_order_item_type( $order, $order_item_id ) { return 'line_item'; } //phpcs:enable Squiz.Commenting, Generic.Commenting /** * Get the SQL needed to create all the tables needed for the custom orders table feature. * * @return string */ public function get_database_schema() { global $wpdb; $collate = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : ''; $orders_table_name = $this->get_orders_table_name(); $addresses_table_name = $this->get_addresses_table_name(); $operational_data_table_name = $this->get_operational_data_table_name(); $meta_table = $this->get_meta_table_name(); $sql = " CREATE TABLE $orders_table_name ( id bigint(20) unsigned, status varchar(20) null, currency varchar(10) null, type varchar(20) null, tax_amount decimal(26,8) null, total_amount decimal(26,8) null, customer_id bigint(20) unsigned null, billing_email varchar(320) null, date_created_gmt datetime null, date_updated_gmt datetime null, parent_order_id bigint(20) unsigned null, payment_method varchar(100) null, payment_method_title text null, transaction_id varchar(100) null, ip_address varchar(100) null, user_agent text null, customer_note text null, PRIMARY KEY (id), KEY status (status), KEY date_created (date_created_gmt), KEY customer_id_billing_email (customer_id, billing_email), KEY billing_email (billing_email), KEY type_status (type, status), KEY parent_order_id (parent_order_id), KEY date_updated (date_updated_gmt) ) $collate; CREATE TABLE $addresses_table_name ( id bigint(20) unsigned auto_increment primary key, order_id bigint(20) unsigned NOT NULL, address_type varchar(20) null, first_name text null, last_name text null, company text null, address_1 text null, address_2 text null, city text null, state text null, postcode text null, country text null, email varchar(320) null, phone varchar(100) null, KEY order_id (order_id), UNIQUE KEY address_type_order_id (address_type, order_id), KEY email (email), KEY phone (phone) ) $collate; CREATE TABLE $operational_data_table_name ( id bigint(20) unsigned auto_increment primary key, order_id bigint(20) unsigned NULL, created_via varchar(100) NULL, woocommerce_version varchar(20) NULL, prices_include_tax tinyint(1) NULL, coupon_usages_are_counted tinyint(1) NULL, download_permission_granted tinyint(1) NULL, cart_hash varchar(100) NULL, new_order_email_sent tinyint(1) NULL, order_key varchar(100) NULL, order_stock_reduced tinyint(1) NULL, date_paid_gmt datetime NULL, date_completed_gmt datetime NULL, shipping_tax_amount decimal(26, 8) NULL, shipping_total_amount decimal(26, 8) NULL, discount_tax_amount decimal(26, 8) NULL, discount_total_amount decimal(26, 8) NULL, recorded_sales tinyint(1) NULL, UNIQUE KEY order_id (order_id), UNIQUE KEY order_key (order_key) ) $collate; CREATE TABLE $meta_table ( id bigint(20) unsigned auto_increment primary key, order_id bigint(20) unsigned null, meta_key varchar(255), meta_value text null, KEY meta_key_value (meta_key, meta_value(100)), KEY order_id_meta_key_meta_value (order_id, meta_key, meta_value(100)) ) $collate; "; return $sql; } /** * Returns an array of meta for an object. * * @param WC_Data $object WC_Data object. * @return array */ public function read_meta( &$object ) { $raw_meta_data = $this->data_store_meta->read_meta( $object ); return $this->filter_raw_meta_data( $object, $raw_meta_data ); } /** * Deletes meta based on meta ID. * * @param WC_Data $object WC_Data object. * @param stdClass $meta (containing at least ->id). */ public function delete_meta( &$object, $meta ) { return $this->data_store_meta->delete_meta( $object, $meta ); } /** * Add new piece of meta. * * @param WC_Data $object WC_Data object. * @param stdClass $meta (containing ->key and ->value). * @return int meta ID */ public function add_meta( &$object, $meta ) { return $this->data_store_meta->add_meta( $object, $meta ); } /** * Update meta. * * @param WC_Data $object WC_Data object. * @param stdClass $meta (containing ->id, ->key and ->value). */ public function update_meta( &$object, $meta ) { return $this->data_store_meta->update_meta( $object, $meta ); } }