Directly fetch order prop from DB for compatibility with CPT store.

This commit is contained in:
Vedanshu Jain 2023-02-15 17:23:43 +05:30
parent 9bdc7f3495
commit ae7d827f22
3 changed files with 70 additions and 26 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Directly fetch order prop from DB for compatibility with CPT store.

View File

@ -180,6 +180,8 @@ class OrdersTableDataStore extends \Abstract_WC_Order_Data_Store_CPT implements
/** /**
* Get the names of all the tables involved in the custom orders table feature. * Get the names of all the tables involved in the custom orders table feature.
* *
* See also : get_all_table_names_with_id.
*
* @return string[] * @return string[]
*/ */
public function get_all_table_names() { public function get_all_table_names() {
@ -191,6 +193,22 @@ class OrdersTableDataStore extends \Abstract_WC_Order_Data_Store_CPT implements
); );
} }
/**
* Similar to get_all_table_names, but also returns the table name along with the items table.
*
* @return array Names of the tables.
*/
public static function get_all_table_names_with_id() {
global $wpdb;
return array(
'orders' => self::get_orders_table_name(),
'addresses' => self::get_addresses_table_name(),
'operational_data' => self::get_operational_data_table_name(),
'meta' => self::get_meta_table_name(),
'items' => $wpdb->prefix . 'woocommerce_order_items',
);
}
/** /**
* Table column to WC_Order mapping for wc_orders table. * Table column to WC_Order mapping for wc_orders table.
* *
@ -554,8 +572,8 @@ class OrdersTableDataStore extends \Abstract_WC_Order_Data_Store_CPT implements
* @return bool Whether permissions are granted. * @return bool Whether permissions are granted.
*/ */
public function get_download_permissions_granted( $order ) { public function get_download_permissions_granted( $order ) {
$order = is_int( $order ) ? wc_get_order( $order ) : $order; $order_id = is_int( $order ) ? $order : $order->get_id();
return $order->get_download_permissions_granted(); return $this->get_field_value( $order_id, 'download_permission_granted', self::get_operational_data_table_name() );
} }
/** /**
@ -580,8 +598,8 @@ class OrdersTableDataStore extends \Abstract_WC_Order_Data_Store_CPT implements
* @return bool Whether sales are recorded. * @return bool Whether sales are recorded.
*/ */
public function get_recorded_sales( $order ) { public function get_recorded_sales( $order ) {
$order = is_int( $order ) ? wc_get_order( $order ) : $order; $order_id = is_int( $order ) ? $order : $order->get_id();
return $order->get_recorded_sales(); return $this->get_field_value( $order_id, 'recorded_sales', self::get_operational_data_table_name() );
} }
/** /**
@ -606,8 +624,8 @@ class OrdersTableDataStore extends \Abstract_WC_Order_Data_Store_CPT implements
* @return bool Whether coupon counts were updated. * @return bool Whether coupon counts were updated.
*/ */
public function get_recorded_coupon_usage_counts( $order ) { public function get_recorded_coupon_usage_counts( $order ) {
$order = is_int( $order ) ? wc_get_order( $order ) : $order; $order_id = is_int( $order ) ? $order : $order->get_id();
return $order->get_recorded_coupon_usage_counts(); return $this->get_field_value( $order_id, 'coupon_usages_are_counted', self::get_operational_data_table_name() );
} }
/** /**
@ -632,8 +650,8 @@ class OrdersTableDataStore extends \Abstract_WC_Order_Data_Store_CPT implements
* @return bool Whether email is sent. * @return bool Whether email is sent.
*/ */
public function get_email_sent( $order ) { public function get_email_sent( $order ) {
$order = is_int( $order ) ? wc_get_order( $order ) : $order; $order_id = is_int( $order ) ? $order : $order->get_id();
return $order->get_new_order_email_sent(); return $this->get_field_value( $order_id, 'new_order_email_sent', self::get_operational_data_table_name() );
} }
/** /**
@ -658,8 +676,7 @@ class OrdersTableDataStore extends \Abstract_WC_Order_Data_Store_CPT implements
* @return bool Whether email was sent. * @return bool Whether email was sent.
*/ */
public function get_new_order_email_sent( $order ) { public function get_new_order_email_sent( $order ) {
$order = is_int( $order ) ? wc_get_order( $order ) : $order; return $this->get_email_sent( $order );
return $order->get_new_order_email_sent();
} }
/** /**
@ -684,8 +701,8 @@ class OrdersTableDataStore extends \Abstract_WC_Order_Data_Store_CPT implements
* @return bool Whether stock was reduced. * @return bool Whether stock was reduced.
*/ */
public function get_stock_reduced( $order ) { public function get_stock_reduced( $order ) {
$order = is_int( $order ) ? wc_get_order( $order ) : $order; $order_id = is_int( $order ) ? $order : $order->get_id();
return $order->get_order_stock_reduced(); return $this->get_field_value( $order_id, 'order_stock_reduced', self::get_operational_data_table_name() );
} }
/** /**
@ -882,6 +899,31 @@ WHERE
// phpcs:enable // phpcs:enable
} }
/**
* Returns field value for an order directly from the database, skipping the value stored in order prop.
* Useful when you are not sure if the order prop is manipulated by a callee function without having to refresh the order.
*
* @param int $order_id Order ID.
* @param string $column_name Name of the column to fetch value from.
* @param string $table_name Name of the table to fetch value from.
*
* @return string|null Field value or null if not found.
*/
public function get_field_value( $order_id, $column_name, $table_name ) {
global $wpdb;
$where_clause = ' WHERE 1=1 ';
if ( self::get_addresses_table_name() === $table_name ) {
$address_type = explode( '_', $column_name )[0];
$column_name = str_replace( $address_type . '_', '', $column_name );
$where_clause .= $wpdb->prepare( ' AND type = %s', $address_type );
}
$select_clause = 'SELECT `' . esc_sql( $column_name ) . '` FROM `' . esc_sql( $table_name ) . '` ';
$where_clause .= $wpdb->prepare( ' AND order_id = %d', $order_id );
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Both select_clause and where_clause are escaped.
return $wpdb->get_var( "$select_clause $where_clause LIMIT 1" );
}
/** /**
* Search order data for a term and return matching order IDs. * Search order data for a term and return matching order IDs.
* *

View File

@ -164,6 +164,12 @@ class OrdersTableQuery {
*/ */
private $date_query = null; private $date_query = null;
/**
* Instance of the OrdersTableDataStore class.
*
* @var OrdersTableDataStore
*/
private $order_datastore = null;
/** /**
* Sets up and runs the query after processing arguments. * Sets up and runs the query after processing arguments.
@ -171,19 +177,11 @@ class OrdersTableQuery {
* @param array $args Array of query vars. * @param array $args Array of query vars.
*/ */
public function __construct( $args = array() ) { public function __construct( $args = array() ) {
global $wpdb; // Note that ideally we would inject this dependency via constructor, but that's not possible since this class needs to be backward compatible with WC_Order_Query class.
$this->order_datastore = wc_get_container()->get( OrdersTableDataStore::class );
$datastore = wc_get_container()->get( OrdersTableDataStore::class ); $this->tables = $this->order_datastore::get_all_table_names_with_id();
$this->mappings = $this->order_datastore->get_all_order_column_mappings();
// TODO: maybe OrdersTableDataStore::get_all_table_names() could return these keys/indices instead.
$this->tables = array(
'orders' => $datastore::get_orders_table_name(),
'addresses' => $datastore::get_addresses_table_name(),
'operational_data' => $datastore::get_operational_data_table_name(),
'meta' => $datastore::get_meta_table_name(),
'items' => $wpdb->prefix . 'woocommerce_order_items',
);
$this->mappings = $datastore->get_all_order_column_mappings();
$this->args = $args; $this->args = $args;
@ -333,7 +331,7 @@ class OrdersTableQuery {
$date_value = $this->args[ $date_key ]; $date_value = $this->args[ $date_key ];
$operator = '='; $operator = '=';
$dates = array(); $dates = array();
$timezone = in_array( $date_key, $gmt_date_keys, true ) ? '+0000' : wc_timezone_string(); $timezone = in_array( $date_key, $gmt_date_keys, true ) ? '+0000' : wc_timezone_string();
if ( is_string( $date_value ) && preg_match( self::REGEX_SHORTHAND_DATES, $date_value, $matches ) ) { if ( is_string( $date_value ) && preg_match( self::REGEX_SHORTHAND_DATES, $date_value, $matches ) ) {
$operator = in_array( $matches[2], $valid_operators, true ) ? $matches[2] : ''; $operator = in_array( $matches[2], $valid_operators, true ) ? $matches[2] : '';
@ -361,7 +359,7 @@ class OrdersTableQuery {
$operator_to_keys[] = 'before'; $operator_to_keys[] = 'before';
} }
$date_key = in_array( $date_key, $local_date_keys, true ) ? $local_to_gmt_date_keys[ $date_key ] : $date_key; $date_key = in_array( $date_key, $local_date_keys, true ) ? $local_to_gmt_date_keys[ $date_key ] : $date_key;
$date_queries[] = array_merge( $date_queries[] = array_merge(
array( array(
'column' => $date_key, 'column' => $date_key,