So, I am trying to implement a drag and drop interface for WooCommerce order line items so that I can control the order in which each line item appears (without having to add each item to the cart in the proper sequence). This functionality overrides the default WooCommerce functionality which displays order items (in the frontend shopping cart, frontend order review, and backend order admin) in the order which they were put into the cart (read, ascending order_item_id
from the woocommerce_order_items
table).
My solution involves using the woocommerce_order_itemmeta
table to store an additional meta key (_order_item_order
) / value (integer) pair, which are used in a custom query, specifically in the ORDER BY
:
$line_items = $wpdb->get_results( $wpdb->prepare( "
SELECT woi.order_item_id, woi.order_item_name, woi.order_item_type
FROM {$wpdb->prefix}woocommerce_order_items woi
INNER JOIN wp_woocommerce_order_itemmeta woim1 ON
(woi.order_item_id=woim1.order_item_id AND woim1.meta_key='_order_item_order' AND CAST(woim1.meta_value AS CHAR) != '')
WHERE
woi.order_item_type = 'line_item'
AND
woi.order_id = %d
ORDER BY woim1.meta_value ASC
", $order->id ) );
Looking at the get_items()
public function found on lines 247 through 283 of the class-wc-order.php, shows the exact functionality which I am trying to alter with my custom query. (The query above has been tested and returns what I want.)
Josh Kohlbach from 'Code My Own Road' shared a method he used to order his order items by the order_item_name
in the woocommerce_order_items
table, by hooking into woocommerce_order_get_items
.
Other than this, I have not come across anything which has helped me wrap my head around what I need to do. I am admittedly ignorant about the proper application of filters in WordPress — despite having read, re-read, and read yet again various tutorials and having consulted the WP documentation (on ThemeShaper, WP Tuts+, and WordPress Codex to name a few...)
So, my question is: how do I properly utilize filtering to implement my query?