Revert "testing renewal fix, issue 3270"
This reverts commit 7563c632f5
.
This commit is contained in:
parent
b3f48f098c
commit
0852986599
@ -63,13 +63,6 @@ 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
|
||||||
@ -77,9 +70,12 @@ 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", $order_item->get_product_id());
|
$coffee_list = get_field(
|
||||||
|
"coffee_list",
|
||||||
|
$order_item->get_product_id()
|
||||||
|
);
|
||||||
|
|
||||||
// Skip this item if no coffee list is found
|
// TODO log or display this error
|
||||||
if (!$coffee_list) {
|
if (!$coffee_list) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -93,24 +89,26 @@ 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;
|
||||||
|
|
||||||
// Initialize the seen count for this coffee list if not set
|
// Check if this coffee list has been seen before in this order
|
||||||
if (!isset($seen_subscriptions[$coffee_list_id])) {
|
if (array_key_exists($coffee_list_id, $seen_subscriptions)) {
|
||||||
|
// Increment the count of times this coffee list has been encountered
|
||||||
|
$seen_subscriptions[$coffee_list_id]++;
|
||||||
|
// Calculate the index to select a coffee from the list
|
||||||
|
$index = $seen_subscriptions[$coffee_list_id];
|
||||||
|
$coffee =
|
||||||
|
$coffees_in_list[$index % sizeof($coffees_in_list)];
|
||||||
|
} else {
|
||||||
|
// If this coffee list is encountered for the first time in this order
|
||||||
$seen_subscriptions[$coffee_list_id] = 0;
|
$seen_subscriptions[$coffee_list_id] = 0;
|
||||||
|
// Select the last coffee from the list
|
||||||
|
$coffee = array_pop(array_reverse($coffees_in_list));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate the index to select a coffee from the list
|
|
||||||
$index = $seen_subscriptions[$coffee_list_id];
|
|
||||||
// Select the coffee using the calculated index
|
|
||||||
$coffee = $coffees_in_list[$index % count($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]++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -133,22 +131,20 @@ 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("ids", ["parent", "renewal"]);
|
$last_orders = wcs_get_subscription($subscription)->get_related_orders(
|
||||||
|
"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);
|
||||||
|
|
||||||
@ -156,31 +152,43 @@ function action_wcs_renewal_order_created($order, $subscription)
|
|||||||
$order_coffees = [];
|
$order_coffees = [];
|
||||||
$seen_subscriptions = [];
|
$seen_subscriptions = [];
|
||||||
|
|
||||||
// Initialize $seen_subscriptions based on the last order's coffees
|
// Get the latest coffee seen in each coffee list from the previous order
|
||||||
foreach ($last_order_coffees as $last_order_coffee) {
|
foreach (array_reverse($last_order_coffees) as $last_order_coffee) {
|
||||||
$coffee_list_id = $last_order_coffee["coffee_list"]->ID;
|
if (
|
||||||
if (!isset($seen_subscriptions[$coffee_list_id])) {
|
array_key_exists(
|
||||||
// Find the position of the last coffee in its list
|
$last_order_coffee["coffee_list"]->ID,
|
||||||
$position_in_list = array_search(
|
$seen_subscriptions
|
||||||
$last_order_coffee["coffee"]->ID,
|
)
|
||||||
get_field("coffees", $last_order_coffee["coffee_list"]->ID)
|
) {
|
||||||
);
|
continue;
|
||||||
// Set the next position (or 0 if not found)
|
|
||||||
$seen_subscriptions[$coffee_list_id] = $position_in_list !== false ? $position_in_list + 1 : 0;
|
|
||||||
}
|
}
|
||||||
|
$position_in_list = array_search(
|
||||||
|
$last_order_coffee["coffee"]->ID,
|
||||||
|
$last_order_coffee["coffee_list"]->coffees
|
||||||
|
);
|
||||||
|
if (!$position_in_list) {
|
||||||
|
// ignore this, can't find it in list anymore
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$seen_subscriptions[
|
||||||
|
$last_order_coffee["coffee_list"]->ID
|
||||||
|
] = $position_in_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process coffee items for the new renewal order
|
// Process coffee items based on the coffees in the last order
|
||||||
process_coffee_items($order, $order_coffees, $seen_subscriptions);
|
process_coffee_items($last_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("wcs_renewal_order_created", "action_wcs_renewal_order_created", 10, 2);
|
add_action(
|
||||||
|
"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");
|
||||||
|
Loading…
Reference in New Issue
Block a user