public function orders_dashboard_model($dashboard_model)
 {
     // $dashboard_model[$this->dashboard_key]['tables']['posts']['where']['post_status'] = array('wc-pending', 'wc-processing', 'wc-on-hold', 'wc-completed', 'wc-cancelled', 'wc-refunded', 'wc-failed');
     $dashboard_model[$this->dashboard_key]['tables']['posts']['where']['post_type'] = 'shop_order';
     $post_type_col_index = sm_multidimesional_array_search('posts_post_status', 'index', $dashboard_model[$this->dashboard_key]['columns']);
     $dashboard_model[$this->dashboard_key]['columns'][$post_type_col_index]['values'] = wc_get_order_statuses();
     return $dashboard_model;
 }
 public function products_data_model($data_model)
 {
     global $wpdb;
     //Code for loading the data for the attributes column
     if (empty($data_model) || empty($data_model['items'])) {
         return;
     }
     $current_store_model = get_transient('sm_dashboard_model_' . $this->dashboard_key);
     $col_model = !empty($current_store_model[$this->dashboard_key]['columns']) ? $current_store_model[$this->dashboard_key]['columns'] : array();
     if (!empty($col_model)) {
         //Code to get attr values by slug name
         $attr_val_by_slug = array();
         $attr_taxonomy_nm = get_object_taxonomies($this->post_type);
         if (!empty($attr_taxonomy_nm)) {
             foreach ($attr_taxonomy_nm as $key => $attr_taxonomy) {
                 if (substr($attr_taxonomy, 0, 3) != 'pa_') {
                     unset($attr_taxonomy_nm[$key]);
                 }
             }
             $attr_terms = get_terms($attr_taxonomy_nm, array('hide_empty' => 0, 'orderby' => 'id'));
             if (!empty($attr_terms)) {
                 foreach ($attr_terms as $attr_term) {
                     if (empty($attr_val_by_slug[$attr_term->taxonomy])) {
                         $attr_val_by_slug[$attr_term->taxonomy] = array();
                     }
                     $attr_val_by_slug[$attr_term->taxonomy][$attr_term->slug] = $attr_term->name;
                 }
             }
         }
         $taxonomy_nm = array();
         $term_taxonomy_ids = array();
         $post_ids = array();
         $product_attributes_postmeta = array();
         foreach ($col_model as $column) {
             if (empty($column['src'])) {
                 continue;
             }
             $src_exploded = explode("/", $column['src']);
             if (!empty($src_exploded) && $src_exploded[1] == 'product_attributes') {
                 $attr_values = $column['values'];
                 if (!empty($attr_values)) {
                     foreach ($attr_values as $key => $attr_value) {
                         $taxonomy_nm[] = $key;
                         $term_taxonomy_ids = $term_taxonomy_ids + $attr_value;
                     }
                 }
             }
         }
         foreach ($data_model['items'] as $key => &$data) {
             if (empty($data['posts_id'])) {
                 continue;
             }
             $post_ids[] = $data['posts_id'];
             $data['loaded'] = true;
             $data['expanded'] = true;
             if (!empty($data['posts_post_parent'])) {
                 $parent_key = sm_multidimesional_array_search($data['posts_post_parent'], 'posts_id', $data_model['items']);
                 // $parent_key = $data['posts_post_parent'];
                 // $parent_type = '';
                 // if ( !empty($data_model['items'][$parent_key]['terms_product_type']) ) {
                 // 	$parent_type = $data_model['items'][$parent_key]['terms_product_type'];
                 // } else if ( empty($data_model['items'][$parent_key]['terms_product_type'])) {
                 // 	$parent_type = wp_get_object_terms( $parent_key, 'product_type', array('fields' => 'names') );
                 // 	$parent_type = $parent_type[0];
                 // }
                 // if ( $parent_type != 'variable' ) {
                 // 	unset($data_model['items'][$key]);
                 // 	continue;
                 // }
                 $parent_title = '';
                 // Code for the variation title on sorting
                 if ($this->prod_sort === true) {
                     $parent_title = !empty($data_model['items'][$parent_key]['posts_post_title']) ? $data_model['items'][$parent_key]['posts_post_title'] : get_the_title($data['posts_post_parent']);
                     $parent_title .= !empty($parent_title) ? ' - ' : '';
                 }
                 $data['parent'] = $data['posts_post_parent'];
                 $data['isLeaf'] = true;
                 $data['level'] = 1;
                 //Code for modifying the variation name
                 $variation_title = '';
                 foreach ($data as $key => &$value) {
                     $start_pos = strrpos($key, '_meta_value_attribute_');
                     if ($start_pos !== false) {
                         $attr_nm = substr($key, $start_pos + 22);
                         $value = empty($value) ? 'any' : $value;
                         if (!empty($attr_values[$attr_nm])) {
                             $attr_lbl = !empty($attr_values[$attr_nm]['lbl']) ? $attr_values[$attr_nm]['lbl'] : $attr_nm;
                             $attr_val = !empty($attr_val_by_slug[$attr_nm][$value]) ? $attr_val_by_slug[$attr_nm][$value] : $value;
                             $variation_title .= $attr_lbl . ' : ' . $attr_val;
                         } else {
                             $variation_title .= $attr_nm . ' : ' . $value;
                         }
                         $variation_title .= ', ';
                     }
                 }
                 $data['posts_post_title'] = $parent_title . '' . substr($variation_title, 0, strlen($variation_title) - 2);
             } else {
                 if (!empty($data['terms_product_type'])) {
                     if ($data['terms_product_type'] == 'simple') {
                         $data['icon_show'] = false;
                     }
                     $data['parent'] = 'null';
                     $data['isLeaf'] = false;
                     $data['level'] = 0;
                 }
             }
             if ($this->prod_sort === true) {
                 $data['icon_show'] = false;
                 $data['parent'] = 'null';
                 $data['isLeaf'] = false;
                 $data['level'] = 0;
             }
             if (empty($data['postmeta_meta_key__product_attributes_meta_value__product_attributes'])) {
                 continue;
             }
             $product_attributes_postmeta[$data['posts_id']] = $data['postmeta_meta_key__product_attributes_meta_value__product_attributes'];
         }
         $data_model['items'] = array_values($data_model['items']);
         $terms_objects = wp_get_object_terms($post_ids, $taxonomy_nm, 'orderby=none&fields=all_with_object_id');
         $attributes_val = array();
         $temp_attribute_nm = "";
         if (!empty($terms_objects)) {
             foreach ($terms_objects as $terms_object) {
                 $post_id = $terms_object->object_id;
                 $taxonomy = $terms_object->taxonomy;
                 $term_id = $terms_object->term_id;
                 if (!isset($attributes_val[$post_id])) {
                     $attributes_val[$post_id] = array();
                 }
                 if (!isset($attributes_val[$post_id][$taxonomy])) {
                     $attributes_val[$post_id][$taxonomy] = array();
                 }
                 $attributes_val[$post_id][$taxonomy][$term_id] = $terms_object->name;
             }
         }
         //Query to get the attribute name
         $query_attribute_label = "SELECT attribute_name, attribute_label\n\t\t                                FROM {$wpdb->prefix}woocommerce_attribute_taxonomies";
         $results_attribute_label = $wpdb->get_results($query_attribute_label, 'ARRAY_A');
         $attribute_label_count = $wpdb->num_rows;
         $attributes_label = array();
         if ($attribute_label_count > 0) {
             foreach ($results_attribute_label as $results_attribute_label1) {
                 $attributes_label['pa_' . $results_attribute_label1['attribute_name']] = array();
                 $attributes_label['pa_' . $results_attribute_label1['attribute_name']] = $results_attribute_label1['attribute_label'];
             }
         }
         // $query_attributes = $wpdb->prepare("SELECT post_id as id,
         // 											meta_value as product_attributes
         // 										FROM {$wpdb->prefix}postmeta
         // 										WHERE meta_key = '%s'
         // 											AND meta_value <> '%s'
         // 											AND post_id IN (".implode(',', array_filter($post_ids,'is_int')).")
         // 										GROUP BY id",'_product_attributes','a:0:{}');
         // $product_attributes = $wpdb->get_results($query_attributes, 'ARRAY_A');
         // $product_attributes_count = $wpdb->num_rows;
         if (!empty($product_attributes_postmeta)) {
             foreach ($product_attributes_postmeta as $post_id => $product_attribute) {
                 if (empty($product_attribute)) {
                     continue;
                 }
                 $prod_attr = json_decode($product_attribute, true);
                 $update_index = sm_multidimesional_array_search($post_id, 'posts_id', $data_model['items']);
                 $attributes_list = "";
                 //cond added for handling blank data
                 if (is_array($prod_attr) && !empty($prod_attr)) {
                     $attributes_list = "";
                     foreach ($prod_attr as &$prod_attr1) {
                         if ($prod_attr1['is_taxonomy'] == 0) {
                             $attributes_list .= $prod_attr1['name'] . ": [" . trim($prod_attr1['value']) . "]";
                             $attributes_list .= "<br>";
                         } else {
                             $attributes_val_current = !empty($attributes_val[$post_id][$prod_attr1['name']]) ? $attributes_val[$post_id][$prod_attr1['name']] : array();
                             $attributes_list .= $attributes_label[$prod_attr1['name']] . ": [" . implode(" | ", $attributes_val_current) . "]";
                             $attributes_list .= "<br>";
                             $prod_attr1['value'] = $attributes_val_current;
                         }
                     }
                     $data_model['items'][$update_index]['custom_product_attributes'] = $attributes_list;
                     $data_model['items'][$update_index]['postmeta_meta_key__product_attributes_meta_value__product_attributes'] = json_encode($prod_attr);
                 }
             }
         }
     }
     return $data_model;
 }