mont58-theme/wp-content/themes/mont58-coffee/functions.php

241 lines
7.9 KiB
PHP
Raw Normal View History

2024-05-08 20:15:09 +00:00
<?php
/**
* Equeue the child theme stylesheet.
*/
2024-05-08 20:27:19 +00:00
function mont58coffee_enqueue_styles()
{
wp_enqueue_style(
"child-style",
get_stylesheet_uri(),
["bridge"],
wp_get_theme()->get("Version")
);
2024-05-08 20:15:09 +00:00
}
2024-05-08 20:27:19 +00:00
add_action("wp_enqueue_scripts", "mont58coffee_enqueue_styles");
add_filter("get_terms", "ts_get_subcategory_terms", 10, 3);
function ts_get_subcategory_terms($terms, $taxonomies, $args)
{
$new_terms = [];
// if it is a product category and on the shop page
if (in_array("product_cat", $taxonomies) && !is_admin() && is_shop()) {
foreach ($terms as $key => $term) {
if (!in_array($term->slug, ["african-coffees"])) {
//pass the slug name here
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
2024-05-08 20:15:09 +00:00
}
/**
* Plugin Name: Extend WooCommerce Subscription Intervals
* Description: Add a "every 10 weeks" billing interval to WooCommerce Subscriptions
* Author: Brent Shepherd
* Author URI: http://brent.io
* Version: 1.0
* License: GPL v2
*/
2024-05-08 20:27:19 +00:00
function eg_extend_subscription_period_intervals($intervals)
{
$intervals[10] = sprintf(
__("every %s", "my-text-domain"),
WC_Subscriptions::append_numeral_suffix(10)
);
2024-05-08 20:15:09 +00:00
2024-05-08 20:27:19 +00:00
return $intervals;
2024-05-08 20:15:09 +00:00
}
2024-05-08 20:27:19 +00:00
add_filter(
"woocommerce_subscription_period_interval_strings",
"eg_extend_subscription_period_intervals"
);
/**
* Snippet Name: Mont58 Coffee Label
* Description: Display labels for coffee shipping
* Version: 1.0.0
* Author: Autonomic Co-op
* Author URI: https://autonomic.zone/
* License: GPL2
2024-05-08 20:27:19 +00:00
*/
2024-07-05 02:25:42 +00:00
/**
* 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
*/
2024-05-08 20:27:19 +00:00
function process_coffee_items($order, &$order_coffees, &$seen_subscriptions)
{
// Iterate through all items in the order
foreach ($order->get_items() as $order_item) {
// 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
2024-07-05 02:25:42 +00:00
$coffee_list = get_field("coffee_list", $order_item->get_product_id());
2024-05-08 20:27:19 +00:00
2024-07-05 02:25:42 +00:00
// Skip this item if no coffee list is found
2024-05-08 20:27:19 +00:00
if (!$coffee_list) {
continue;
}
// Get the list of coffees from the coffee list
$coffees_in_list = get_field("coffees", $coffee_list);
// Get the quantity of this specific item in the order
$item_quantity = $order_item->get_quantity();
// Process each quantity of the subscription item
for ($x = 0; $x < $item_quantity; $x++) {
$coffee_list_id = $coffee_list->ID;
2024-07-05 02:25:42 +00:00
// Initialize the seen count for this coffee list if not set
if (!isset($seen_subscriptions[$coffee_list_id])) {
2024-05-08 20:27:19 +00:00
$seen_subscriptions[$coffee_list_id] = 0;
}
2024-07-05 02:25:42 +00:00
// 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)];
2024-05-08 20:27:19 +00:00
// Add the selected coffee with its associated coffee list to the order_coffees array
array_push($order_coffees, [
"coffee_list" => $coffee_list,
"coffee" => $coffee,
]);
2024-07-05 02:25:42 +00:00
// Increment the seen count for this coffee list
$seen_subscriptions[$coffee_list_id]++;
2024-05-08 20:27:19 +00:00
}
}
}
}
// Handle new order creation event
2024-05-08 20:27:19 +00:00
function action_woocommerce_new_order($order_id, $order)
{
// Initialize arrays to store processed data
$order_coffees = [];
$seen_subscriptions = [];
2024-05-08 20:27:19 +00:00
// Process coffee items in the new order
process_coffee_items($order, $order_coffees, $seen_subscriptions);
2024-05-08 20:27:19 +00:00
// Update the custom field "coffees" with the processed coffee data for the order
update_field("coffees", $order_coffees, $order_id);
}
// Hook the above function to the 'woocommerce_new_order' action
2024-05-08 20:27:19 +00:00
add_action("woocommerce_new_order", "action_woocommerce_new_order", 10, 2);
// Handle renewal order creation event
2024-07-05 02:25:42 +00:00
/**
* Handle renewal order creation event
*
* @param WC_Order $order The new renewal order
* @param WC_Subscription $subscription The subscription object
*/
2024-05-08 20:27:19 +00:00
function action_wcs_renewal_order_created($order, $subscription)
{
// Retrieve related orders of the subscription excluding the current renewal order
2024-07-05 02:25:42 +00:00
$last_orders = wcs_get_subscription($subscription)->get_related_orders("ids", ["parent", "renewal"]);
2024-05-08 20:27:19 +00:00
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);
// Get the processed coffees from the most recent previous order
$last_order_coffees = get_field("coffees", $last_order_id);
// Initialize arrays to store processed data for the current order
$order_coffees = [];
$seen_subscriptions = [];
2024-07-05 02:25:42 +00:00
// 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;
2024-05-08 20:27:19 +00:00
}
}
2024-07-05 02:25:42 +00:00
// Process coffee items for the new renewal order
process_coffee_items($order, $order_coffees, $seen_subscriptions);
2024-05-08 20:27:19 +00:00
// 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
2024-07-05 02:25:42 +00:00
add_action("wcs_renewal_order_created", "action_wcs_renewal_order_created", 10, 2);
// Add a sidepanel on WooCommerce order pages
2024-05-08 20:27:19 +00:00
add_action("add_meta_boxes", "mont58_coffee_label_add_sidepanel");
function mont58_coffee_label_add_sidepanel()
{
add_meta_box(
"mont58_coffee_label_sidepanel",
2024-06-27 15:28:27 +00:00
"Label",
2024-05-08 20:27:19 +00:00
"mont58_coffee_label_render_sidepanel",
"shop_order",
"side",
"high"
);
}
2024-05-08 20:27:19 +00:00
function mont58_coffee_label_render_sidepanel($post)
{
// Render the content of the sidepanel here
$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
2024-05-08 20:27:19 +00:00
function load_custom_template($template)
{
if (is_page("coffee-label")) {
return plugin_dir_path(__FILE__) . "template-order-label.php";
}
2024-05-08 20:27:19 +00:00
return $template;
}
2024-05-08 20:27:19 +00:00
add_filter("template_include", "load_custom_template");
2024-05-08 20:27:19 +00:00
add_filter("bulk_actions-edit-shop_order", function ($bulk_actions) {
$bulk_actions["generate-label"] = __("Generate label", "txtdomain");
return $bulk_actions;
});
2024-05-08 20:27:19 +00:00
add_filter(
"handle_bulk_actions-edit-shop_order",
function ($redirect_url, $action, $post_ids) {
if ($action == "generate-label") {
$redirect_url = add_query_arg(
"order_ids",
$post_ids,
"/coffee-label"
);
}
return $redirect_url;
},
10,
3
);