-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache subscription last order date #819
base: trunk
Are you sure you want to change the base?
Changes from 11 commits
2838dff
58b1b27
da940a7
7c39ca2
e910960
e84d69a
c0447f4
205f602
af8d2c4
58f14d6
e51e8c9
150be3d
ef28be8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,22 @@ public static function init() { | |
add_filter( 'woocommerce_order_query_args', array( __CLASS__, 'map_order_query_args_for_subscriptions' ) ); | ||
|
||
add_filter( 'woocommerce_orders_table_query_clauses', [ __CLASS__, 'filter_orders_query_by_parent_orders' ], 10, 2 ); | ||
|
||
add_action( 'woocommerce_before_delete_order', [ __CLASS__, 'delete_order_update_order_related_subscriptions_last_order_date_created' ], 10, 2 ); | ||
|
||
$cache_manager = new WCS_Object_Data_Cache_Manager( | ||
'subscription', | ||
[ | ||
'parent_id', | ||
] | ||
); | ||
$cache_manager->init(); | ||
|
||
add_action( 'wcs_update_post_meta_caches', [ __CLASS__, 'update_subscription_last_order_date_parent_id_changes' ], 10, 5 ); | ||
|
||
add_action( 'wcs_orders_add_relation', [ __CLASS__, 'add_relation_update_order_related_subscriptions_last_order_date_created' ], 10, 3 ); | ||
|
||
add_action( 'wcs_orders_delete_relation', [ __CLASS__, 'delete_relation_update_order_related_subscriptions_last_order_date_created' ], 10, 3 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function name seems weird to me. Also it does not actually delete the order just updates it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @leonardola the |
||
} | ||
|
||
/* | ||
|
@@ -2396,6 +2412,97 @@ public static function get_meta( $order, $meta_key, $default = 0 ) { | |
return $meta_value; | ||
} | ||
|
||
/** | ||
* Update subscription cached last_order_date_created metadata when deleting a child order. | ||
* | ||
* @param int $id The deleted order ID. | ||
* @param WC_Order $order The deleted order object. | ||
*/ | ||
public static function delete_order_update_order_related_subscriptions_last_order_date_created( $id, $order ) { | ||
if ( $order->get_created_via() !== 'subscription' ) { | ||
return; | ||
} | ||
|
||
self::update_order_related_subscriptions_last_order_date_created( $order, [ 'trash' ] ); | ||
} | ||
|
||
/** | ||
* Update subscription cached last_order_date_created metadata when adding a child order. | ||
* | ||
* @param WC_Order $order The order to link with the subscription. | ||
* @param WC_Order $subscription The order or subscription to link the order to. | ||
* @param string $relation_type The relationship between the subscription and the order. Must be 'renewal', 'switch' or 'resubscribe' unless custom relationships are implemented. | ||
*/ | ||
public static function add_relation_update_order_related_subscriptions_last_order_date_created( $order, $subscription, $relation_type ) { | ||
self::update_order_related_subscriptions_last_order_date_created( $order ); | ||
} | ||
|
||
/** | ||
* Update subscription cached last_order_date_created metadata when deleting a child order relation. | ||
* | ||
* @param WC_Order $order An order that may be linked with subscriptions. | ||
* @param WC_Order $subscription A subscription or order to unlink the order with, if a relation exists. | ||
* @param string $relation_type The relationship between the subscription and the order. Must be 'renewal', 'switch' or 'resubscribe' unless custom relationships are implemented. | ||
*/ | ||
public static function delete_relation_update_order_related_subscriptions_last_order_date_created( $order, $subscription, $relation_type ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is also not deleting anything is it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function name comes from |
||
$last_order_date_created = $subscription->get_time( 'last_order_date_created', 'gmt' ); | ||
|
||
$subscription->set_last_order_date_created( $last_order_date_created ); | ||
$subscription->save(); | ||
|
||
self::update_order_related_subscriptions_last_order_date_created( $order ); | ||
} | ||
|
||
/** | ||
* Update all subscription cached last_order_date_created metadata related to the order. | ||
* | ||
* @param WC_Order $order The order object. | ||
* @param array $exclude_statuses The order statuses to exclude. | ||
*/ | ||
private static function update_order_related_subscriptions_last_order_date_created( $order, $exclude_statuses = [] ) { | ||
$subscription_ids = wcs_get_subscription_ids_for_order( $order ); | ||
|
||
foreach ( $subscription_ids as $subscription_id ) { | ||
$subscription = wcs_get_subscription( $subscription_id ); | ||
$last_order_date_created = $subscription->get_time( 'last_order_date_created', 'gmt', $exclude_statuses ); | ||
|
||
$subscription->set_last_order_date_created( $last_order_date_created ); | ||
$subscription->save(); | ||
} | ||
} | ||
|
||
/** | ||
* Update subscription cached last_order_date_created metadata when manually updating parent id. | ||
* | ||
* @param string $type The type of update to check. Only 'add' or 'delete' should be used. | ||
* @param int $object_id The object the meta is being changed on. | ||
* @param string $meta_key The object meta key being changed. | ||
* @param mixed $meta_value The meta value. | ||
* @param mixed $prev_value The previous value stored in the database. Optional. | ||
*/ | ||
public static function update_subscription_last_order_date_parent_id_changes( $type, $object_id, $key, $new_value, $previous_value ) { | ||
if ( 'parent_id' !== $key || empty( $previous_value ) || empty( $new_value ) ) { | ||
return; | ||
} | ||
|
||
$previous_subscription = wcs_get_subscription( $previous_value ); | ||
$new_subscription = wcs_get_subscription( $new_value ); | ||
|
||
if ( ! $previous_subscription || ! $new_subscription ) { | ||
return; | ||
} | ||
|
||
// Switch last order date on both subscriptions. | ||
$previous_last_order_date_created = $previous_subscription->get_time( 'last_order_date_created' ); | ||
$new_last_order_date_created = $new_subscription->get_time( 'last_order_date_created' ); | ||
|
||
$new_subscription->set_last_order_date_created( $previous_last_order_date_created ); | ||
$new_subscription->save(); | ||
|
||
$previous_subscription->set_last_order_date_created( $new_last_order_date_created ); | ||
$previous_subscription->save(); | ||
} | ||
|
||
/** | ||
* Prints the HTML for the admin dropdown for order types to Woocommerce -> Orders screen. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -277,6 +277,11 @@ function wcs_create_order_from_subscription( $subscription, $type ) { | |
throw new Exception( sprintf( __( 'There was an error fetching the new order (%1$s) for subscription %2$d.', 'woocommerce-subscriptions' ), $type, $subscription->get_id() ) ); | ||
} | ||
|
||
// Update the subscription last_order_date_created on every time | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make the comment a one liner or change to /* */ style There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed on ef28be8. |
||
// a child order is created. | ||
$subscription->set_last_order_date_created( $new_order->get_date_created()->getTimestamp() ); | ||
$subscription->save(); | ||
|
||
/** | ||
* Filters the new order created from the subscription. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function name seems weird to me. Also it does not actually delete the order just update it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@leonardola it deletes the order relation, not the order itself.