Run prettier

This commit is contained in:
3wc 2024-05-08 17:27:19 -03:00
parent d830e1c251
commit 985a3d07f9
3 changed files with 257 additions and 198 deletions

View File

@ -15,4 +15,4 @@ steps:
from_secret: drone_ssh_server.example.com from_secret: drone_ssh_server.example.com
when: when:
branch: branch:
- main - main

View File

@ -3,28 +3,32 @@
/** /**
* Equeue the child theme stylesheet. * Equeue the child theme stylesheet.
*/ */
function mont58coffee_enqueue_styles() { function mont58coffee_enqueue_styles()
{
wp_enqueue_style('child-style', wp_enqueue_style(
get_stylesheet_uri(), "child-style",
array( 'bridge' ), get_stylesheet_uri(),
wp_get_theme()->get( 'Version' ) ["bridge"],
); wp_get_theme()->get("Version")
);
} }
add_action( 'wp_enqueue_scripts', 'mont58coffee_enqueue_styles' ); add_action("wp_enqueue_scripts", "mont58coffee_enqueue_styles");
add_filter( 'get_terms', 'ts_get_subcategory_terms', 10, 3 ); add_filter("get_terms", "ts_get_subcategory_terms", 10, 3);
function ts_get_subcategory_terms( $terms, $taxonomies, $args ) { function ts_get_subcategory_terms($terms, $taxonomies, $args)
$new_terms = array(); {
// if it is a product category and on the shop page $new_terms = [];
if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() &&is_shop() ) { // if it is a product category and on the shop page
foreach( $terms as $key => $term ) { if (in_array("product_cat", $taxonomies) && !is_admin() && is_shop()) {
if ( !in_array( $term->slug, array( 'african-coffees' ) ) ) { //pass the slug name here foreach ($terms as $key => $term) {
$new_terms[] = $term; if (!in_array($term->slug, ["african-coffees"])) {
}} //pass the slug name here
$terms = $new_terms; $new_terms[] = $term;
} }
return $terms; }
$terms = $new_terms;
}
return $terms;
} }
/** /**
@ -36,13 +40,19 @@ return $terms;
* License: GPL v2 * License: GPL v2
*/ */
function eg_extend_subscription_period_intervals( $intervals ) { function eg_extend_subscription_period_intervals($intervals)
{
$intervals[10] = sprintf(
__("every %s", "my-text-domain"),
WC_Subscriptions::append_numeral_suffix(10)
);
$intervals[10] = sprintf( __( 'every %s', 'my-text-domain' ), WC_Subscriptions::append_numeral_suffix( 10 ) ); return $intervals;
return $intervals;
} }
add_filter( 'woocommerce_subscription_period_interval_strings', 'eg_extend_subscription_period_intervals' ); add_filter(
"woocommerce_subscription_period_interval_strings",
"eg_extend_subscription_period_intervals"
);
/** /**
* Snippet Name: Mont58 Coffee Label * Snippet Name: Mont58 Coffee Label
@ -51,149 +61,188 @@ add_filter( 'woocommerce_subscription_period_interval_strings', 'eg_extend_subsc
* Author: Autonomic Co-op * Author: Autonomic Co-op
* Author URI: https://autonomic.zone/ * Author URI: https://autonomic.zone/
* License: GPL2 * License: GPL2
*/ */
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 {
foreach ($order->get_items() as $order_item) { // Iterate through all items in the order
// Check if the item is a subscription (based on name containing "subscription") foreach ($order->get_items() as $order_item) {
if (stripos($order_item->get_name(), "subscription") !== false) { // Check if the item is a subscription (based on name containing "subscription")
// Get the coffee list associated with the subscription item if (stripos($order_item->get_name(), "subscription") !== false) {
$coffee_list = get_field("coffee_list", $order_item->get_product_id()); // Get the coffee list associated with the subscription item
$coffee_list = get_field(
"coffee_list",
$order_item->get_product_id()
);
// TODO log or display this error // TODO log or display this error
if (!$coffee_list) { if (!$coffee_list) {
continue; continue;
} }
// Get the list of coffees from the coffee list // Get the list of coffees from the coffee list
$coffees_in_list = get_field("coffees", $coffee_list); $coffees_in_list = get_field("coffees", $coffee_list);
// Get the quantity of this specific item in the order // Get the quantity of this specific item in the order
$item_quantity = $order_item->get_quantity(); $item_quantity = $order_item->get_quantity();
// Process each quantity of the subscription item // Process each quantity of the subscription item
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 // Check if this coffee list has been seen before in this order
if (array_key_exists($coffee_list_id, $seen_subscriptions)) { if (array_key_exists($coffee_list_id, $seen_subscriptions)) {
// Increment the count of times this coffee list has been encountered // Increment the count of times this coffee list has been encountered
$seen_subscriptions[$coffee_list_id]++; $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 = $coffees_in_list[$index % sizeof($coffees_in_list)]; $coffee =
} else { $coffees_in_list[$index % sizeof($coffees_in_list)];
// If this coffee list is encountered for the first time in this order } else {
$seen_subscriptions[$coffee_list_id] = 0; // If this coffee list is encountered for the first time in this order
// Select the last coffee from the list $seen_subscriptions[$coffee_list_id] = 0;
$coffee = array_pop(array_reverse($coffees_in_list)); // 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("coffee_list" => $coffee_list, "coffee" => $coffee)); array_push($order_coffees, [
} "coffee_list" => $coffee_list,
} "coffee" => $coffee,
} ]);
}
}
}
} }
// Handle new order creation event // Handle new order creation event
function action_woocommerce_new_order($order_id, $order) { function action_woocommerce_new_order($order_id, $order)
// Initialize arrays to store processed data {
$order_coffees = array(); // Initialize arrays to store processed data
$seen_subscriptions = array(); $order_coffees = [];
$seen_subscriptions = [];
// Process coffee items in the new order // Process coffee items in the new order
process_coffee_items($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 order // Update the custom field "coffees" with the processed coffee data for the order
update_field("coffees", $order_coffees, $order_id); update_field("coffees", $order_coffees, $order_id);
} }
// Hook the above function to the 'woocommerce_new_order' action // Hook the above function to the 'woocommerce_new_order' action
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
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 {
$last_orders = wcs_get_subscription($subscription)->get_related_orders('ids', array('parent', 'renewal')); // Retrieve related orders of the subscription excluding the current renewal order
unset($last_orders[$order->get_ID()]); $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 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); $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);
// Initialize arrays to store processed data for the current order // Initialize arrays to store processed data for the current order
$order_coffees = array(); $order_coffees = [];
$seen_subscriptions = array(); $seen_subscriptions = [];
// Get the latest coffee seen in each coffee list from the previous order // Get the latest coffee seen in each coffee list from the previous order
foreach(array_reverse($last_order_coffees) as $last_order_coffee) { foreach (array_reverse($last_order_coffees) as $last_order_coffee) {
if (array_key_exists($last_order_coffee["coffee_list"]->ID, $seen_subscriptions)) { if (
continue; array_key_exists(
} $last_order_coffee["coffee_list"]->ID,
$position_in_list = array_search( $seen_subscriptions
$last_order_coffee["coffee"]->ID, )
$last_order_coffee["coffee_list"]->coffees ) {
); continue;
if (!$position_in_list) { }
// ignore this, can't find it in list anymore $position_in_list = array_search(
continue; $last_order_coffee["coffee"]->ID,
} $last_order_coffee["coffee_list"]->coffees
$seen_subscriptions[$last_order_coffee["coffee_list"]->ID] = $position_in_list; );
} 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 based on the coffees in the last order // Process coffee items based on the coffees in the last order
process_coffee_items($last_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");
function mont58_coffee_label_add_sidepanel() { function mont58_coffee_label_add_sidepanel()
add_meta_box( {
'mont58_coffee_label_sidepanel', add_meta_box(
'Coffee Label', "mont58_coffee_label_sidepanel",
'mont58_coffee_label_render_sidepanel', "Coffee Label",
'shop_order', "mont58_coffee_label_render_sidepanel",
'side', "shop_order",
'high' "side",
); "high"
);
} }
function mont58_coffee_label_render_sidepanel($post) { function mont58_coffee_label_render_sidepanel($post)
// Render the content of the sidepanel here {
$order_ids = array($post->ID); // Render the content of the sidepanel here
echo '<a href="' . add_query_arg("order_ids", $order_ids, "/coffee-label") . '" class="button button-primary" target="_blank">View Label</a>'; $order_ids = [$post->ID];
echo '<a href="' .
add_query_arg("order_ids", $order_ids, "/coffee-label") .
'" class="button button-primary" target="_blank">View Label</a>';
} }
// Load custom template // Load custom template
function load_custom_template($template) { function load_custom_template($template)
if (is_page('coffee-label')) { {
return plugin_dir_path(__FILE__) . 'template-order-label.php'; if (is_page("coffee-label")) {
} return plugin_dir_path(__FILE__) . "template-order-label.php";
}
return $template; return $template;
} }
add_filter('template_include', 'load_custom_template'); add_filter("template_include", "load_custom_template");
add_filter('bulk_actions-edit-shop_order', function($bulk_actions) { add_filter("bulk_actions-edit-shop_order", function ($bulk_actions) {
$bulk_actions['generate-label'] = __('Generate label', 'txtdomain'); $bulk_actions["generate-label"] = __("Generate label", "txtdomain");
return $bulk_actions; return $bulk_actions;
}); });
add_filter('handle_bulk_actions-edit-shop_order', function($redirect_url, $action, $post_ids) { add_filter(
if ($action == 'generate-label') { "handle_bulk_actions-edit-shop_order",
$redirect_url = add_query_arg('order_ids', $post_ids, "/coffee-label"); function ($redirect_url, $action, $post_ids) {
} if ($action == "generate-label") {
return $redirect_url; $redirect_url = add_query_arg(
}, 10, 3); "order_ids",
$post_ids,
"/coffee-label"
);
}
return $redirect_url;
},
10,
3
);

View File

@ -1,4 +1,5 @@
<?php /* Template Name: Coffee Label */ <?php
/* Template Name: Coffee Label */
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
@ -81,70 +82,74 @@
</head> </head>
<body> <body>
<div class="container"> <div class="container">
<?php <?php foreach ($_GET["order_ids"] as $order_id) {
foreach ($_GET['order_ids'] as $order_id) { $order = wc_get_order($order_id);
$order = wc_get_order($order_id); $first_name = $order->get_billing_first_name();
$first_name = $order->get_billing_first_name(); $last_name = $order->get_billing_last_name();
$last_name = $order->get_billing_last_name(); $postcode = $order->get_billing_postcode();
$postcode = $order->get_billing_postcode(); $items = $order->get_items();
$items = $order->get_items(); $roast_date = (new DateTime())->modify("-3 days")->format("d/m/Y");
$roast_date = (new DateTime())->modify('-3 days')->format('d/m/Y'); $gift_message = $order->get_meta("_shipping_gift_message");
$gift_message = $order->get_meta('_shipping_gift_message');
$order_subscription_coffees = get_field('coffees', $order_id); $order_subscription_coffees = get_field("coffees", $order_id);
$j = 0; // Count order items $j = 0; // Count order items
foreach ($items as $item) { foreach ($items as $item) {
// checking if product is a coffee // checking if product is a coffee
$product_id = $item->get_product_id(); $product_id = $item->get_product_id();
$product_categories = get_the_terms($product_id, 'product_cat'); $product_categories = get_the_terms($product_id, "product_cat");
$is_coffee = false; $is_coffee = false;
$is_subscription = false; $is_subscription = false;
if (!empty($product_categories) && !is_wp_error($product_categories)) { if (
foreach ($product_categories as $product_category) { !empty($product_categories) &&
if ($product_category->slug == 'coffee') { !is_wp_error($product_categories)
$is_coffee = true; ) {
break; foreach ($product_categories as $product_category) {
} else if ($product_category->slug == 'tasting') { if ($product_category->slug == "coffee") {
$is_subscription = true; $is_coffee = true;
break; break;
} elseif ($product_category->slug == "tasting") {
$is_subscription = true;
break;
}
}
} }
}
}
if ((!$is_coffee) && (!$is_subscription)) {
continue;
}
$brew_method = $item->get_meta('pa_brew-method'); if (!$is_coffee && !$is_subscription) {
$weight = $item->get_meta('weight'); continue;
}
if ($is_coffee) { $brew_method = $item->get_meta("pa_brew-method");
// Non-subscription order $weight = $item->get_meta("weight");
$product_name = $item->get_name();
$roast = get_field('roast_level', $product_id);
$is_organic = get_field('is_organic', $product_id);
}
if ($is_subscription) { if ($is_coffee) {
$subscriptions = wcs_get_subscriptions_for_order($order_id, array( 'order_type' => 'any' )); // Non-subscription order
// TODO log / admin warning if there are >1 subscriptions $product_name = $item->get_name();
$subscription = array_pop($subscriptions); $roast = get_field("roast_level", $product_id);
$orders = $subscription->get_related_orders('ids', 'any'); $is_organic = get_field("is_organic", $product_id);
if (sizeof($orders) == 1) { }
$is_new = true;
}
}
$quantity = $item->get_quantity(); if ($is_subscription) {
for ($i = 0; $i < $quantity; $i++) { $subscriptions = wcs_get_subscriptions_for_order($order_id, [
if ($is_subscription) { "order_type" => "any",
$coffee = $order_subscription_coffees[$i+$j]["coffee"]; ]);
$product_name = $coffee->post_title; // TODO log / admin warning if there are >1 subscriptions
$roast = get_field('roast_level', $coffee->ID); $subscription = array_pop($subscriptions);
$is_organic = get_field('is_organic', $coffee->ID); $orders = $subscription->get_related_orders("ids", "any");
} ?> if (sizeof($orders) == 1) {
$is_new = true;
}
}
$quantity = $item->get_quantity();
for ($i = 0; $i < $quantity; $i++) {
if ($is_subscription) {
$coffee = $order_subscription_coffees[$i + $j]["coffee"];
$product_name = $coffee->post_title;
$roast = get_field("roast_level", $coffee->ID);
$is_organic = get_field("is_organic", $coffee->ID);
} ?>
<div class="label"> <div class="label">
<div> <div>
<div class="product-details"> <div class="product-details">
@ -153,7 +158,11 @@
<p class="bean-type"><?php echo $brew_method; ?></p> <p class="bean-type"><?php echo $brew_method; ?></p>
<p class="roast"><?php echo $roast; ?> Roast</p> <p class="roast"><?php echo $roast; ?> Roast</p>
<p class="date">Roasted on: <span><?php echo $roast_date; ?></span></p> <p class="date">Roasted on: <span><?php echo $roast_date; ?></span></p>
<p class="code"><?php echo $is_new ? 'N' : '' ?><?php echo $gift_message ? 'G' : '' ?><?php echo $is_organic ? 'O' : '' ?></p> <p class="code"><?php
echo $is_new ? "N" : "";
echo $gift_message ? "G" : "";
echo $is_organic ? "O" : "";
?></p>
</div> </div>
<div class="order-details"> <div class="order-details">
<p class="customer-name"><?php echo "$first_name $last_name"; ?></p> <p class="customer-name"><?php echo "$first_name $last_name"; ?></p>
@ -162,10 +171,11 @@
</div> </div>
</div> </div>
</div> </div>
<?php } <?php
$j++; // increment counter }
} $j++; // increment counter
}?> }
} ?>
</div> </div>
</body> </body>
</html> </html>