Compare commits
No commits in common. "e7f5673a19590acb606c4a3c11dc302200e42809" and "7563c632f53e15ea1dc03833937f5c3ab3d9ac9a" have entirely different histories.
e7f5673a19
...
7563c632f5
@ -63,6 +63,13 @@ 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
|
||||
@ -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")
|
||||
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());
|
||||
|
||||
// TODO log or display this error
|
||||
// Skip this item if no coffee list is found
|
||||
if (!$coffee_list) {
|
||||
continue;
|
||||
}
|
||||
@ -89,26 +93,24 @@ function process_coffee_items($order, &$order_coffees, &$seen_subscriptions)
|
||||
for ($x = 0; $x < $item_quantity; $x++) {
|
||||
$coffee_list_id = $coffee_list->ID;
|
||||
|
||||
// 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];
|
||||
$coffee =
|
||||
$coffees_in_list[$index % sizeof($coffees_in_list)];
|
||||
} else {
|
||||
// If this coffee list is encountered for the first time in this order
|
||||
// Initialize the seen count for this coffee list if not set
|
||||
if (!isset($seen_subscriptions[$coffee_list_id])) {
|
||||
$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
|
||||
array_push($order_coffees, [
|
||||
"coffee_list" => $coffee_list,
|
||||
"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);
|
||||
|
||||
// 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);
|
||||
|
||||
@ -152,43 +156,31 @@ function action_wcs_renewal_order_created($order, $subscription)
|
||||
$order_coffees = [];
|
||||
$seen_subscriptions = [];
|
||||
|
||||
// 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;
|
||||
// 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
|
||||
$position_in_list = array_search(
|
||||
$last_order_coffee["coffee"]->ID,
|
||||
get_field("coffees", $last_order_coffee["coffee_list"]->ID)
|
||||
);
|
||||
// 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 === 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 based on the coffees in the last order
|
||||
process_coffee_items($last_order, $order_coffees, $seen_subscriptions);
|
||||
// Process coffee items for the new renewal order
|
||||
process_coffee_items($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");
|
||||
|
@ -76,6 +76,7 @@ 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 {
|
||||
@ -118,6 +119,8 @@ 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();
|
||||
@ -130,7 +133,7 @@ if(!is_user_logged_in()){
|
||||
|
||||
$order_subscription_coffees = get_field("coffees", $order_id);
|
||||
|
||||
$j = 0;
|
||||
$j = 0; // Reset $j for each order (claude suggested this)
|
||||
|
||||
foreach ($items as $item) {
|
||||
// checking if product is a coffee
|
||||
|
Loading…
Reference in New Issue
Block a user