Compare commits

..

3 Commits

Author SHA1 Message Date
3wc
e7f5673a19 Fix coffee list rotation
All checks were successful
continuous-integration/drone/push Build is passing
2024-07-05 19:01:24 -04:00
3wc
0852986599 Revert "testing renewal fix, issue 3270"
This reverts commit 7563c632f5.
2024-07-05 18:55:32 -04:00
3wc
b3f48f098c Wider area for "origin" to avoid line breaks 2024-07-05 18:50:15 -04:00
2 changed files with 51 additions and 46 deletions

View File

@ -63,13 +63,6 @@ add_filter(
* 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)
{
// 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")
if (stripos($order_item->get_name(), "subscription") !== false) {
// 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) {
continue;
}
@ -93,24 +89,26 @@ function process_coffee_items($order, &$order_coffees, &$seen_subscriptions)
for ($x = 0; $x < $item_quantity; $x++) {
$coffee_list_id = $coffee_list->ID;
// Initialize the seen count for this coffee list if not set
if (!isset($seen_subscriptions[$coffee_list_id])) {
$seen_subscriptions[$coffee_list_id] = 0;
}
// Check if this coffee list has been seen before in this order
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];
// Select the coffee using the calculated index
$coffee = $coffees_in_list[$index % count($coffees_in_list)];
$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;
// 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
array_push($order_coffees, [
"coffee_list" => $coffee_list,
"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);
// 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)
{
// 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()]);
// If there are previous orders related to the subscription
if (sizeof($last_orders) > 0) {
// Get the ID of the most recent previous order
$last_order_id = max($last_orders);
$last_order = wc_get_order($last_order_id);
// Get the processed coffees from the most recent previous order
$last_order_coffees = get_field("coffees", $last_order_id);
@ -156,31 +152,43 @@ function action_wcs_renewal_order_created($order, $subscription)
$order_coffees = [];
$seen_subscriptions = [];
// Initialize $seen_subscriptions based on the last order's coffees
foreach ($last_order_coffees as $last_order_coffee) {
$coffee_list_id = $last_order_coffee["coffee_list"]->ID;
if (!isset($seen_subscriptions[$coffee_list_id])) {
// Find the position of the last coffee in its list
// Get the latest coffee seen in each coffee list from the previous order
foreach (array_reverse($last_order_coffees) as $last_order_coffee) {
if (
array_key_exists(
$last_order_coffee["coffee_list"]->ID,
$seen_subscriptions
)
) {
continue;
}
$position_in_list = array_search(
$last_order_coffee["coffee"]->ID,
get_field("coffees", $last_order_coffee["coffee_list"]->ID)
$last_order_coffee["coffee_list"]->coffees
);
// Set the next position (or 0 if not found)
$seen_subscriptions[$coffee_list_id] = $position_in_list !== false ? $position_in_list + 1 : 0;
if ($position_in_list === false) {
// 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($order, $order_coffees, $seen_subscriptions);
// Process coffee items based on the coffees in the last order
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_field("coffees", $order_coffees, $order->get_ID());
}
}
// 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_action("add_meta_boxes", "mont58_coffee_label_add_sidepanel");

View File

@ -76,7 +76,6 @@ if(!is_user_logged_in()){
min-height: 3mm;
font-variation-settings: 'wdth' 70, 'wght' 600;
margin-inline: auto;
width: 90%;
}
.label .product-details .origin.non-coffee {
@ -119,8 +118,6 @@ if(!is_user_logged_in()){
<body>
<div class="container">
<?php
$j = 0;
foreach ($_GET["order_ids"] as $order_id) {
$order = wc_get_order($order_id);
$first_name = $order->get_billing_first_name();
@ -133,7 +130,7 @@ if(!is_user_logged_in()){
$order_subscription_coffees = get_field("coffees", $order_id);
$j = 0; // Reset $j for each order (claude suggested this)
$j = 0;
foreach ($items as $item) {
// checking if product is a coffee