I have a store with many variable products. All orders are custom made specifically for the customer. This means that when we receive the product in the warehouse we need to find the order of the specific product. Right now we simply have to manually check most orders.
What we want to do that will save us valuable time is search orders by custom attribute terms. For example: We use attribute "size" for custom orders. We have 24 "size" terms (e.g. "12cm", :16cm" etc). We want to find the order based on the size term. For example: Find all orders who's size is "48cm".
Based on Extending search in backend orders list for product items by id or by sku, here is my code attempt:
add_filter( 'woocommerce_shop_order_search_fields', function ($search_fields ) {
$orders = get_posts( array( 'post_type' => 'shop_order' ) );
foreach ($orders as $order_post) {
$order_id = $order_post->ID;
$order = new WC_Order($order_id);
$items = $order->get_items();
foreach( $order->get_items() as $item_id => $item_values ) {
if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
$product_id = $item_values['product_id'];
} else {
$product_id = $item_values->get_product_id();
}
$attribute = $item_values->get_data();
// attribute value
$value = $attribute['value'];
// attribute slug
$slug = $attribute['size'];
add_post_meta($order_id, $slug, $value, true); //
}
}
return array_merge($search_fields, array($slug));
});
But It didn't work. Any help is appreciated.
