Rework lookup table data store sync methods to return a meaningful status.

This commit is contained in:
Jeff Stieler 2019-01-29 14:36:02 -07:00
parent 9b1f6999aa
commit 9f3c95ae63
4 changed files with 41 additions and 18 deletions

View File

@ -315,19 +315,21 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im
*
* @since 3.5.0
* @param int $order_id Order ID.
* @return void
* @return int|bool Returns -1 if order won't be processed, or a boolean indicating processing success.
*/
public static function sync_order_coupons( $order_id ) {
global $wpdb;
$order = wc_get_order( $order_id );
if ( ! $order ) {
return;
return -1;
}
$coupon_items = $order->get_items( 'coupon' );
$num_updated = 0;
foreach ( $coupon_items as $coupon_item ) {
$wpdb->replace(
$result = $wpdb->replace(
$wpdb->prefix . self::TABLE_NAME,
array(
'order_id' => $order_id,
@ -342,7 +344,11 @@ class WC_Admin_Reports_Coupons_Data_Store extends WC_Admin_Reports_Data_Store im
'%s',
)
);
$num_updated += intval( $result );
}
return ( count( $coupon_items ) === $num_updated );
}
}

View File

@ -360,18 +360,19 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto
* Add order information to the lookup table when orders are created or modified.
*
* @param int $post_id Post ID.
* @return int|bool Returns -1 if order won't be processed, or a boolean indicating processing success.
*/
public static function sync_order( $post_id ) {
if ( 'shop_order' !== get_post_type( $post_id ) ) {
return;
return -1;
}
$order = wc_get_order( $post_id );
if ( ! $order ) {
return;
return -1;
}
self::update( $order );
return self::update( $order );
}
/**
@ -388,14 +389,14 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto
* Update the database with stats data.
*
* @param WC_Order $order Order to update row for.
* @return int|bool|null Number or rows modified or false on failure.
* @return int|bool Returns -1 if order won't be processed, or a boolean indicating processing success.
*/
public static function update( $order ) {
global $wpdb;
$table_name = $wpdb->prefix . self::TABLE_NAME;
if ( ! $order->get_id() || ! $order->get_date_created() ) {
return false;
return -1;
}
$data = array(
@ -450,7 +451,9 @@ class WC_Admin_Reports_Orders_Stats_Data_Store extends WC_Admin_Reports_Data_Sto
}
// Update or add the information to the DB.
return $wpdb->replace( $table_name, $data, $format );
$result = $wpdb->replace( $table_name, $data, $format );
return ( 1 === $result );
}
/**

View File

@ -319,7 +319,7 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i
*
* @since 3.5.0
* @param int $order_id Order ID.
* @return void
* @return int|bool Returns -1 if order won't be processed, or a boolean indicating processing success.
*/
public static function sync_order_products( $order_id ) {
global $wpdb;
@ -328,10 +328,13 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i
// This hook gets called on refunds as well, so return early to avoid errors.
if ( ! $order || 'shop_order_refund' === $order->get_type() ) {
return;
return -1;
}
foreach ( $order->get_items() as $order_item ) {
$order_items = $order->get_items();
$num_updated = 0;
foreach ( $order_items as $order_item ) {
$order_item_id = $order_item->get_id();
$quantity_refunded = $order->get_item_quantity_refunded( $order_item );
$amount_refunded = $order->get_item_amount_refunded( $order_item );
@ -355,13 +358,13 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i
$net_revenue = $order_item->get_subtotal( 'edit' ) - $amount_refunded;
if ( $quantity_refunded >= $order_item->get_quantity( 'edit' ) ) {
$wpdb->delete(
$result = $wpdb->delete(
$wpdb->prefix . self::TABLE_NAME,
array( 'order_item_id' => $order_item_id ),
array( '%d' )
); // WPCS: cache ok, DB call ok.
} else {
$wpdb->replace(
$result = $wpdb->replace(
$wpdb->prefix . self::TABLE_NAME,
array(
'order_item_id' => $order_item_id,
@ -398,7 +401,11 @@ class WC_Admin_Reports_Products_Data_Store extends WC_Admin_Reports_Data_Store i
)
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
}
$num_updated += intval( $result );
}
return ( count( $order_items ) === $num_updated );
}
}

View File

@ -255,17 +255,20 @@ class WC_Admin_Reports_Taxes_Data_Store extends WC_Admin_Reports_Data_Store impl
* Create or update an entry in the wc_order_tax_lookup table for an order.
*
* @param int $order_id Order ID.
* @return void
* @return int|bool Returns -1 if order won't be processed, or a boolean indicating processing success.
*/
public static function sync_order_taxes( $order_id ) {
global $wpdb;
$order = wc_get_order( $order_id );
if ( ! $order ) {
return;
return -1;
}
foreach ( $order->get_items( 'tax' ) as $tax_item ) {
$wpdb->replace(
$tax_items = $order->get_items( 'tax' );
$num_updated = 0;
foreach ( $tax_items as $tax_item ) {
$result = $wpdb->replace(
$wpdb->prefix . self::TABLE_NAME,
array(
'order_id' => $order->get_id(),
@ -284,7 +287,11 @@ class WC_Admin_Reports_Taxes_Data_Store extends WC_Admin_Reports_Data_Store impl
'%f',
)
);
$num_updated += intval( $result );
}
return ( count( $tax_items ) === $num_updated );
}
}