Order Tags
This code adds new field “Order tags” to >Setup Fields>Others and new filter at top of the form
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 |
// Filter by order tags + export field "Order tags" class Woe_Order_Tags { function __construct() { //add settings add_action("woe_settings_form_view_top", array($this,"draw_options") ); // start filtering add_filter("woe_settings_validate_defaults", function($settings) { $settings[ 'order_tags' ] = array_filter($settings[ 'order_tags' ]); if( !empty($settings[ 'order_tags' ]) ) $this->set_filter($settings[ 'order_tags' ]); return $settings; }); //add field to export add_filter('woe_get_order_fields', function ($fields) { $fields['wcot_order_tags'] = array( 'label' => 'Order Tags', 'colname' => 'Order Tags', 'checked' => 1 ); return $fields; }); //set field value add_filter('woe_get_order_value_wcot_order_tags', function ($value,$order, $field) { $order_tags = wcot_order_tags_get_order_tags( $order->get_id() ); $value= join(",", wp_list_pluck($order_tags,"name")); return $value; }, 10, 3); } function draw_options($settings){ if( empty($settings[ 'order_tags' ]) ) $settings[ 'order_tags' ] = array(); echo "<strong>Order tags:</strong>"; $terms = get_terms([ 'taxonomy' => 'wcot_order_tag', 'hide_empty' => false, ]); echo "<input type='hidden' name='settings[order_tags][]' value='0' />"; foreach($terms as $term) { $checked = in_array($term->term_id,$settings[ 'order_tags' ]) ? 'checked': ''; echo "<input type='checkbox' $checked name='settings[order_tags][]' value='{$term->term_id}' /> {$term->name} "; } echo "<hr><br>"; } function set_filter($tags){ global $wpdb; $terms = join(",", $tags); $sql = "SELECT object_id FROM {$wpdb->prefix}term_relationships AS TR INNER JOIN {$wpdb->prefix}term_taxonomy AS TT ON TR.term_taxonomy_id = TT.term_taxonomy_id INNER JOIN {$wpdb->prefix}terms AS T ON TT.term_id = T.term_id WHERE TT.taxonomy = 'wcot_order_tag' AND T.term_id IN($terms)"; $orders = $wpdb->get_col($sql); add_action( 'woe_sql_get_order_ids_where', function ($where, $settings) use ($orders) { if(!$orders) $orders[] = "0"; // we don't have any matched orders, so export must be EMPTY! $orders = join(",", $orders); $where[] = "orders.ID IN ($orders)"; return $where; }, 10, 2); } } new Woe_Order_Tags(); |