testing renewal fix, issue 3270
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
trav 2024-07-04 22:25:42 -04:00
parent cc541c9e7f
commit 7563c632f5

View File

@ -63,6 +63,13 @@ add_filter(
* License: GPL2 * License: GPL2
*/ */
/**
* Process coffee items for an order
*
* @param WC_Order $order The order to process
* @param array &$order_coffees Reference to array to store processed coffees
* @param array &$seen_subscriptions Reference to array tracking seen subscriptions
*/
function process_coffee_items($order, &$order_coffees, &$seen_subscriptions) function process_coffee_items($order, &$order_coffees, &$seen_subscriptions)
{ {
// Iterate through all items in the order // Iterate through all items in the order
@ -70,12 +77,9 @@ function process_coffee_items($order, &$order_coffees, &$seen_subscriptions)
// Check if the item is a subscription (based on name containing "subscription") // Check if the item is a subscription (based on name containing "subscription")
if (stripos($order_item->get_name(), "subscription") !== false) { if (stripos($order_item->get_name(), "subscription") !== false) {
// Get the coffee list associated with the subscription item // Get the coffee list associated with the subscription item
$coffee_list = get_field( $coffee_list = get_field("coffee_list", $order_item->get_product_id());
"coffee_list",
$order_item->get_product_id()
);
// TODO log or display this error // Skip this item if no coffee list is found
if (!$coffee_list) { if (!$coffee_list) {
continue; continue;
} }
@ -89,26 +93,24 @@ function process_coffee_items($order, &$order_coffees, &$seen_subscriptions)
for ($x = 0; $x < $item_quantity; $x++) { for ($x = 0; $x < $item_quantity; $x++) {
$coffee_list_id = $coffee_list->ID; $coffee_list_id = $coffee_list->ID;
// Check if this coffee list has been seen before in this order // Initialize the seen count for this coffee list if not set
if (array_key_exists($coffee_list_id, $seen_subscriptions)) { if (!isset($seen_subscriptions[$coffee_list_id])) {
// Increment the count of times this coffee list has been encountered $seen_subscriptions[$coffee_list_id] = 0;
$seen_subscriptions[$coffee_list_id]++; }
// Calculate the index to select a coffee from the list // Calculate the index to select a coffee from the list
$index = $seen_subscriptions[$coffee_list_id]; $index = $seen_subscriptions[$coffee_list_id];
$coffee = // Select the coffee using the calculated index
$coffees_in_list[$index % sizeof($coffees_in_list)]; $coffee = $coffees_in_list[$index % count($coffees_in_list)];
} else {
// If this coffee list is encountered for the first time in this order
$seen_subscriptions[$coffee_list_id] = 0;
// Select the last coffee from the list
$coffee = array_pop(array_reverse($coffees_in_list));
}
// Add the selected coffee with its associated coffee list to the order_coffees array // Add the selected coffee with its associated coffee list to the order_coffees array
array_push($order_coffees, [ array_push($order_coffees, [
"coffee_list" => $coffee_list, "coffee_list" => $coffee_list,
"coffee" => $coffee, "coffee" => $coffee,
]); ]);
// Increment the seen count for this coffee list
$seen_subscriptions[$coffee_list_id]++;
} }
} }
} }
@ -131,20 +133,22 @@ function action_woocommerce_new_order($order_id, $order)
add_action("woocommerce_new_order", "action_woocommerce_new_order", 10, 2); add_action("woocommerce_new_order", "action_woocommerce_new_order", 10, 2);
// Handle renewal order creation event // Handle renewal order creation event
/**
* Handle renewal order creation event
*
* @param WC_Order $order The new renewal order
* @param WC_Subscription $subscription The subscription object
*/
function action_wcs_renewal_order_created($order, $subscription) function action_wcs_renewal_order_created($order, $subscription)
{ {
// Retrieve related orders of the subscription excluding the current renewal order // Retrieve related orders of the subscription excluding the current renewal order
$last_orders = wcs_get_subscription($subscription)->get_related_orders( $last_orders = wcs_get_subscription($subscription)->get_related_orders("ids", ["parent", "renewal"]);
"ids",
["parent", "renewal"]
);
unset($last_orders[$order->get_ID()]); unset($last_orders[$order->get_ID()]);
// If there are previous orders related to the subscription // If there are previous orders related to the subscription
if (sizeof($last_orders) > 0) { if (sizeof($last_orders) > 0) {
// Get the ID of the most recent previous order // Get the ID of the most recent previous order
$last_order_id = max($last_orders); $last_order_id = max($last_orders);
$last_order = wc_get_order($last_order_id);
// Get the processed coffees from the most recent previous order // Get the processed coffees from the most recent previous order
$last_order_coffees = get_field("coffees", $last_order_id); $last_order_coffees = get_field("coffees", $last_order_id);
@ -152,43 +156,31 @@ function action_wcs_renewal_order_created($order, $subscription)
$order_coffees = []; $order_coffees = [];
$seen_subscriptions = []; $seen_subscriptions = [];
// Get the latest coffee seen in each coffee list from the previous order // Initialize $seen_subscriptions based on the last order's coffees
foreach (array_reverse($last_order_coffees) as $last_order_coffee) { foreach ($last_order_coffees as $last_order_coffee) {
if ( $coffee_list_id = $last_order_coffee["coffee_list"]->ID;
array_key_exists( if (!isset($seen_subscriptions[$coffee_list_id])) {
$last_order_coffee["coffee_list"]->ID, // Find the position of the last coffee in its list
$seen_subscriptions
)
) {
continue;
}
$position_in_list = array_search( $position_in_list = array_search(
$last_order_coffee["coffee"]->ID, $last_order_coffee["coffee"]->ID,
$last_order_coffee["coffee_list"]->coffees get_field("coffees", $last_order_coffee["coffee_list"]->ID)
); );
if (!$position_in_list) { // Set the next position (or 0 if not found)
// ignore this, can't find it in list anymore $seen_subscriptions[$coffee_list_id] = $position_in_list !== false ? $position_in_list + 1 : 0;
continue;
} }
$seen_subscriptions[
$last_order_coffee["coffee_list"]->ID
] = $position_in_list;
} }
// Process coffee items based on the coffees in the last order // Process coffee items for the new renewal order
process_coffee_items($last_order, $order_coffees, $seen_subscriptions); process_coffee_items($order, $order_coffees, $seen_subscriptions);
// Update the custom field "coffees" with the processed coffee data for the current renewal order // Update the custom field "coffees" with the processed coffee data for the current renewal order
update_field("coffees", $order_coffees, $order->get_ID()); update_field("coffees", $order_coffees, $order->get_ID());
} }
} }
// Hook the above function to the 'wcs_renewal_order_created' action // Hook the above function to the 'wcs_renewal_order_created' action
add_action( add_action("wcs_renewal_order_created", "action_wcs_renewal_order_created", 10, 2);
"wcs_renewal_order_created",
"action_wcs_renewal_order_created",
10,
2
);
// Add a sidepanel on WooCommerce order pages // Add a sidepanel on WooCommerce order pages
add_action("add_meta_boxes", "mont58_coffee_label_add_sidepanel"); add_action("add_meta_boxes", "mont58_coffee_label_add_sidepanel");