Sort Order Items By Custom Field
-
Hello!
I saw this thread but didn’t want to add to it…
I’m looking to sort the order items on the packing list by a custom field. The custom field was added to the General Product Tab like so:
// Display Custom Product Fields For Aisle add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' ); function woo_add_custom_general_fields() { global $woocommerce, $post; echo '<div class="options_group">'; // Custom fields will be created here... // Select woocommerce_wp_select( array( 'id' => '_select', 'label' => __( 'Choose the aisle this product in on', 'woocommerce' ), 'options' => array( '1' => __( 'Aisle 1', 'woocommerce' ), '2' => __( 'Aisle 2', 'woocommerce' ), '3' => __( 'Aisle 3', 'woocommerce' ), '4' => __( 'Aisle 4', 'woocommerce' ), '5' => __( 'Aisle 5', 'woocommerce' ), '6' => __( 'Aisle 6', 'woocommerce' ), '7' => __( 'Aisle 7', 'woocommerce' ), '8' => __( 'Aisle 8', 'woocommerce' ), '9' => __( 'Aisle 9', 'woocommerce' ), '10' => __( 'Aisle 10', 'woocommerce' ), '11' => __( 'Aisle 11', 'woocommerce' ), '12' => __( 'Aisle 12', 'woocommerce' ), '13' => __( 'Aisle 13', 'woocommerce' ), '14' => __( 'Aisle 14', 'woocommerce' ), '15' => __( 'Aisle 15', 'woocommerce' ), '16' => __( 'Aisle 16', 'woocommerce' ), ) ) ); echo '</div>'; }
The custom field is
_select
This custom field is saved and I’ve been able to get it to show up in Email Orders and the PDF template.
However, this did not work.
<?php $wpo_wcpdf->custom_field('_select', 'Custom field:'); ?>
(Not sure why?)I had to use this code inside the Packing Slip template instead:
<?php $product_id = $item['product_id']; echo 'Located on Aisle '; echo get_post_meta( $product_id, '_select', true ); ?>
On this thread I saw where you provided some code for sorting by SKU
add_filter( 'wpo_wcpdf_order_items_data', 'wpo_wcpdf_sort_items_by_sku', 10, 2 ); function wpo_wcpdf_sort_items_by_sku ( $items, $order ) { usort($items, 'wpo_wcpdf_sort_by_sku'); return $items; } function wpo_wcpdf_sort_by_sku($a, $b) { if ($a['sku']==$b['sku']) return 0; return ($a['sku']<$b['sku'])?-1:1; }
This does work perfectly! It even works for quantity if you swap it out…
I thought maybe it would work for the custom field
_select
, but it does not.Do you know if the code you originally provided can work with a field like I described?
Any help would be greatly appreciated it, even if you can point me in the right direction.
https://wordpress.org/plugins/woocommerce-pdf-invoices-packing-slips/
- The topic ‘Sort Order Items By Custom Field’ is closed to new replies.