Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FrancoStino/e4a7cd45784419f3bc6d0c8d31cecf10 to your computer and use it in GitHub Desktop.
Save FrancoStino/e4a7cd45784419f3bc6d0c8d31cecf10 to your computer and use it in GitHub Desktop.
Calculate quantity by single item product into order admin page through bulk action selection (filter) - Woocommerce
<?php
// Display the custom actions on admin Orders bulk action dropdown
add_filter('bulk_actions-edit-shop_order', 'orders_bulk_action_delivery_collect_calc');
function orders_bulk_action_delivery_collect_calc($bulk_actions)
{
$bulk_actions['delivery-collect'] = __("Calcolo quantità degli ordini", 'woocommerce');
return $bulk_actions;
}
// Process the bulk action from selected orders
add_filter('handle_bulk_actions-edit-shop_order', 'delivery_collect_calc_bulk_action_edit_shop_order', 10, 3);
function delivery_collect_calc_bulk_action_edit_shop_order($redirect_to, $action, $post_ids)
{
if ($action === 'delivery-collect')
{
$order_numbers = []; // Initializing
$cod_orders_total = 0; // Initializing
// $salary_per_order = 1.5;
foreach ($post_ids as $post_id)
{
// Get Order status
$order = wc_get_order($post_id);
/*if( $order->get_payment_method() === 'cod' ) {
$cod_orders_total = (float) $order->get_total();
}*/
// Order status change to "completed" (To enable uncomment the line below)
// $order->update_status("completed");
$cod_orders_total += (float)$order->get_total();
$order_numbers[] = $order->get_order_number(); // Adding processed order numbers to an array
// initializes the array that will contain the product categories with the ordered quantities of each
$count_by_cat = array();
// for each product ordered
foreach ($order->get_items() as $item)
{
$product = $item->get_product();
$product_names = [$item->get_name() ];
// get product categories
$terms = get_the_terms($product->get_id() , 'product_cat');
foreach ($product_names as $term)
{
$cat_name = $term;
// if the product category is already present in the array, add the quantities
if (array_key_exists($cat_name, $count_by_cat))
{
$count_by_cat[$cat_name] += $item->get_quantity();
// otherwise it adds the category and quantity to the array
}
else
{
$count_by_cat[$cat_name] = $item->get_quantity();
}
}
}
}
$orders_count = count($order_numbers);
$amount_to_collect = $cod_orders_total;
// Adding the right query vars to the returned URL
$redirect_to = add_query_arg(array(
'collect_action' => $action,
'processed_count' => $orders_count,
'proc_order_nums' => implode(', ', $order_numbers) ,
'amount_to_collect' => $amount_to_collect,
'nome_prodotto' => $cat_name,
'quantità' => $count_by_cat,
) , $redirect_to);
}
return $redirect_to;
}
// Display the results notice from bulk action on orders
add_action('admin_notices', 'set_delivery_collect_bulk_action_admin_notice');
function set_delivery_collect_bulk_action_admin_notice()
{
global $pagenow;
if ('edit.php' === $pagenow && isset($_GET['post_type']) && 'shop_order' === $_GET['post_type'] && isset($_GET['collect_action']) && isset($_GET['processed_count']) && isset($_GET['proc_order_nums']) && isset($_GET['amount_to_collect']) && isset($_GET['quantità']))
{
$count_ids = intval($_GET['processed_count']);
$amount = floatval($_GET['amount_to_collect']);
$currency = get_woocommerce_currency_symbol();
printf('<div class="notice notice-success fade is-dismissible"><p>' . _n("Su %s ordine selezionato, l'importo calcolato da riscuotere è di %sL'ID dell'ordine processato è %s.", "Su %s ordini selezionati, l'importo calcolato da riscuotere è di %sGli ID degli ordini processati sono %s.", $count_ids, "woocommerce") . '</p>', $count_ids, '<code>' . number_format_i18n($amount, 2) . '</code> (' . $currency . ').</p><p>', '<code>' . $_GET['proc_order_nums'] . '</code>',);
echo "<br>Totale quantità d'ordine per singolo prodotto:<br><br>";
echo "<ul style='list-style: disc; padding: 0 20px 0 20px;'>";
foreach ($_GET['quantità'] as $key => $value)
{
echo "<li> " . $key . " ( <strong style='color:red;'>" . $value . "</strong> )</li>";
}
echo "</ul>";
echo '<br></div>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment