-
Notifications
You must be signed in to change notification settings - Fork 0
/
wc-orderitem-rearrange.php
102 lines (82 loc) · 3.77 KB
/
wc-orderitem-rearrange.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/*
* Plugin Name: Rearrange Order Items for WooCommerce
* Description: Rearrange Woocommerce Order Item from admin backend
* Version: 1.0.5
* Author: ole1986 <[email protected]>
* Author URI: https://github.com/ole1986/wc-orderitem-rearrange
* Plugin URI: https://github.com/ole1986/wc-orderitem-rearrange/releases
* Text Domain: wc-orderitem-rearrange
*
* WC requires at least: 3.0.0
* WC tested up to: 9.4
*/
namespace Ole1986\WcOrderItemRearrange;
defined('ABSPATH') or die('No script kiddies please!');
define('WCORDERITEMREARRANGE_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('WCORDERITEMREARRANGE_PLUGIN_URL', plugin_dir_url(__FILE__));
class WcOrderItemRearrange
{
public function init()
{
add_action('woocommerce_after_order_itemmeta', [$this, 'add_item_mover'], 10, 3);
// enable ajax request for Order Item positioning
add_action('wp_ajax_OrderItemRearrange', [$this, 'DoAjax']);
if (is_admin()) {
add_action('admin_enqueue_scripts', [$this, 'loadJS']);
}
}
public function loadJS()
{
wp_enqueue_script('WcOrderItemRearrange_script', WCORDERITEMREARRANGE_PLUGIN_URL . 'js/admin.js?_' . time());
}
public function DoAjax()
{
global $wpdb;
$item_id = intval($_POST['item_id']);
$direction = intval($_POST['direction']);
if (empty($item_id)) {
wp_die();
}
if (is_null($direction)) {
wp_die();
}
$orderItem = new \WC_Order_Item_Product($item_id);
$order = wc_get_order($orderItem->get_order_id());
$itemIds = array_keys($order->get_items());
$key = array_search($item_id, $itemIds);
$swapKey = $direction == 1 ? $key + 1 : $key - 1;
$swap_id = $itemIds[$swapKey];
if (!$swap_id) {
return;
}
$results = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = %d OR order_item_id = %d", $item_id, $swap_id), OBJECT);
$meta_ids = array_column($results, 'meta_id');
$wpdb->query($wpdb->prepare("UPDATE {$wpdb->prefix}woocommerce_order_itemmeta SET order_item_id = IF(order_item_id = %d, %d, %d) WHERE meta_id IN (" . implode(',', $meta_ids) . ")", $item_id, $swap_id, $item_id));
$results = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d OR order_item_id = %d", $item_id, $swap_id), OBJECT);
$resultIds = array_column($results, 'order_item_id');
foreach ($results as $item) {
unset($item->order_item_id);
$id = array_pop($resultIds);
$affected = $wpdb->update("{$wpdb->prefix}woocommerce_order_items", (array)$item, ["order_item_id" => $id]);
}
}
public function add_item_mover($item_id, $item, $product)
{
$css = 'font-size:1.2em; margin-right: 0.5em; padding: 0.2em; text-decoration:none';
?>
<div style="margin-top: 1em;">
<a href="javascript:void(0)" style="<?php echo esc_attr($css) ?>" onclick="WcOrderItemRearrange.MoveOrderItem(this, <?php echo esc_attr($item_id) ?>, 0)">⇧</span></a>
<a href="javascript:void(0)" style="<?php echo esc_attr($css) ?>" onclick="WcOrderItemRearrange.MoveOrderItem(this, <?php echo esc_attr($item_id) ?>, 1)">⇩</span></a>
</div>
<?php
}
}
$app = new WcOrderItemRearrange();
add_action('before_woocommerce_init', function () {
// Support for WooCommerce High Performance Order Storage
if (class_exists(\Automattic\WooCommerce\Utilities\FeaturesUtil::class)) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('custom_order_tables', __FILE__, true);
}
});
add_action('init', [$app, 'init']);