/**
  * Get compare list on popup
  */
 public static function get_compare_list_html_popup()
 {
     global $woo_compare_comparison_page_global_settings, $woo_compare_page_style, $woo_compare_table_style, $woo_compare_table_content_style, $woo_compare_addtocart_style, $woo_compare_viewcart_style;
     global $woo_compare_product_prices_style;
     $current_db_version = get_option('woocommerce_db_version', null);
     $compare_list = WC_Compare_Functions::get_compare_list();
     $woo_compare_basket_icon = get_option('woo_compare_basket_icon');
     if (trim($woo_compare_basket_icon) == '') {
         $woo_compare_basket_icon = WOOCP_IMAGES_URL . '/compare_remove.png';
     }
     $html = '';
     $product_cats = array();
     $products_fields = array();
     $products_prices = array();
     $custom_class = '';
     $add_to_cart_text = $woo_compare_addtocart_style['addtocart_link_text'];
     $add_to_cart_button_class = 'add_to_cart_link_type';
     if (is_array($compare_list) && count($compare_list) > 0) {
         $html .= '<div id="compare-wrapper"><div class="compare-products">';
         $html .= '<table id="product_comparison" class="compare_popup_table" border="1" bordercolor="' . $woo_compare_table_style['table_border_colour'] . '" cellpadding="5" cellspacing="0" width="">';
         $html .= '<tbody><tr class="row_1 row_product_detail"><th class="column_first first_row"><div class="column_first_wide">&nbsp;';
         $html .= '</div></th>';
         $i = 0;
         foreach ($compare_list as $product_id) {
             $product_cat = get_post_meta($product_id, '_woo_compare_category', true);
             $products_fields[$product_id] = WC_Compare_Categories_Fields_Data::get_fieldid_results($product_cat);
             if ($product_cat > 0) {
                 $product_cats[] = $product_cat;
             }
             $i++;
             if (version_compare($current_db_version, '2.0', '<') && null !== $current_db_version) {
                 $current_product = new WC_Product($product_id);
             } elseif (version_compare(WC()->version, '2.2.0', '<')) {
                 $current_product = get_product($product_id);
             } else {
                 $current_product = wc_get_product($product_id);
             }
             $product_name = WC_Compare_Functions::get_variation_name($product_id);
             $product_price = $current_product->get_price_html();
             /**
              * Add code check show or hide price and add to cart button support for Woo Catalog Visibility Options plugin
              */
             $show_add_to_cart = true;
             if (class_exists('WC_CVO_Visibility_Options')) {
                 global $wc_cvo;
                 /**
                  * Check show or hide price
                  */
                 if ($wc_cvo->setting('wc_cvo_prices') == 'secured' && !catalog_visibility_user_has_access() || $wc_cvo->setting('wc_cvo_prices') == 'disabled') {
                     $product_price = '';
                 }
                 /**
                  * Check show or hide add to cart button
                  */
                 if ($wc_cvo->setting('wc_cvo_atc') == 'secured' && !catalog_visibility_user_has_access() || $wc_cvo->setting('wc_cvo_atc') == 'disabled') {
                     $show_add_to_cart = false;
                 }
             }
             $products_prices[$product_id] = $product_price;
             $image_src = WC_Compare_Functions::get_post_thumbnail($product_id, 220, 180);
             if (trim($image_src) == '') {
                 $image_src = '<img alt="' . $product_name . '" src="' . (version_compare($current_db_version, '2.1.0', '<') && null !== $current_db_version ? woocommerce_placeholder_img_src() : wc_placeholder_img_src()) . '" />';
             }
             $html .= '<td class="first_row column_' . $i . '"><div class="td-spacer"><div class="woo_compare_popup_remove_product_container"><a class="woo_compare_popup_remove_product" rel="' . $product_id . '" style="cursor:pointer;">Remove <img src="' . $woo_compare_basket_icon . '" border=0 /></a></div>';
             $html .= '<div class="compare_image_container">' . $image_src . '</div>';
             $html .= '<div class="compare_product_name">' . $product_name . '</div>';
             $html .= '<div class="compare_price">' . $products_prices[$product_id] . '</div>';
             if ($show_add_to_cart && $current_product->is_in_stock() && trim($products_prices[$product_id]) != '') {
                 if ($current_product->product_type != 'external') {
                     $cart_url = add_query_arg('add-to-cart', $product_id, get_option('siteurl') . '/?post_type=product');
                 } else {
                     if ($current_product->product_type == 'external') {
                         if (version_compare($current_db_version, '2.0', '<') && null !== $current_db_version) {
                             $cart_url = get_post_meta($product_id, '_product_url', true);
                             $add_to_cart_text_external = get_post_meta($product_id, '_button_text', true);
                             $add_to_cart_text_external ? $add_to_cart_text_external : __('Buy product', 'woo_cp');
                         } else {
                             $cart_url = $current_product->product_url;
                             $add_to_cart_text_external = $current_product->get_button_text();
                         }
                     }
                 }
                 switch (get_post_type($product_id)) {
                     case "product_variation":
                         $class = 'is_variation';
                         $cart_url = WC_Compare_Functions::get_product_url($product_id);
                         break;
                     default:
                         $class = 'simple';
                         break;
                 }
                 $html .= '<div class="compare_add_cart">';
                 if ($current_product->product_type == 'external') {
                     $html .= sprintf('<a href="%s" rel="nofollow" class="button add_to_cart_button %s product_type_%s %s" target="_blank">%s</a>', $cart_url, $add_to_cart_button_class, $class, $custom_class, $add_to_cart_text_external);
                 } else {
                     $html .= sprintf('<a href="%s" data-product_id="%s" class="button add_to_cart_button %s product_type_%s %s" target="_blank">%s</a>', $cart_url, $product_id, $add_to_cart_button_class, $class, $custom_class, $add_to_cart_text);
                 }
                 $html .= '<a class="virtual_added_to_cart" href="#">&nbsp;</a>';
                 $html .= '</div>';
             }
             $html .= '</div></td>';
         }
         $html .= '</tr>';
         $product_cats = implode(",", $product_cats);
         $compare_fields = WC_Compare_Categories_Fields_Data::get_results('cat_id IN(' . $product_cats . ')', 'cf.cat_id ASC, cf.field_order ASC');
         if (is_array($compare_fields) && count($compare_fields) > 0) {
             $j = 1;
             foreach ($compare_fields as $field_data) {
                 $j++;
                 $html .= '<tr class="row_' . $j . '">';
                 if (trim($field_data->field_unit) != '') {
                     $html .= '<th class="column_first"><div class="compare_value">' . stripslashes($field_data->field_name) . ' (' . trim(stripslashes($field_data->field_unit)) . ')</div></th>';
                 } else {
                     $html .= '<th class="column_first"><div class="compare_value">' . stripslashes($field_data->field_name) . '</div></th>';
                 }
                 $i = 0;
                 foreach ($compare_list as $product_id) {
                     $i++;
                     $empty_cell_class = '';
                     $empty_text_class = '';
                     if (in_array($field_data->id, $products_fields[$product_id])) {
                         $field_value = get_post_meta($product_id, '_woo_compare_' . $field_data->field_key, true);
                         if (is_serialized($field_value)) {
                             $field_value = maybe_unserialize($field_value);
                         }
                         if (is_array($field_value) && count($field_value) > 0) {
                             $field_value = implode(', ', $field_value);
                         } elseif (is_array($field_value) && count($field_value) < 0) {
                             $field_value = $woo_compare_table_content_style['empty_text'];
                         }
                         if (trim($field_value) == '') {
                             $field_value = $woo_compare_table_content_style['empty_text'];
                         }
                     } else {
                         $field_value = $woo_compare_table_content_style['empty_text'];
                     }
                     if ($field_value == $woo_compare_table_content_style['empty_text']) {
                         $empty_cell_class = 'empty_cell';
                         $empty_text_class = 'empty_text';
                     }
                     $html .= '<td class="column_' . $i . ' ' . $empty_cell_class . '"><div class="td-spacer ' . $empty_text_class . ' compare_' . $field_data->field_key . '">' . $field_value . '</div></td>';
                 }
                 $html .= '</tr>';
                 if ($j == 2) {
                     $j = 0;
                 }
             }
             $j++;
             if ($j > 2) {
                 $j = 1;
             }
             $html .= '<tr class="row_' . $j . ' row_end"><th class="column_first">&nbsp;</th>';
             $i = 0;
             foreach ($compare_list as $product_id) {
                 $i++;
                 $html .= '<td class="column_' . $i . '">';
                 $html .= '<div class="td-spacer compare_price">' . $products_prices[$product_id] . '</div>';
                 $html .= '</td>';
             }
         }
         $html .= '</tbody></table>';
         $html .= '</div></div>';
     } else {
         $html .= '<div class="no_compare_list">' . $woo_compare_page_style['no_product_message_text'] . '</div>';
     }
     return $html;
 }
 public static function show_compare_fields($product_id = '', $use_wootheme_style = true)
 {
     global $post, $woo_compare_table_content_style;
     if (trim($product_id) == '') {
         $product_id = $post->ID;
     }
     $html = '';
     $variations_list = WC_Compare_Functions::get_variations($product_id);
     if (is_array($variations_list) && count($variations_list) > 0) {
         foreach ($variations_list as $variation_id) {
             if (WC_Compare_Functions::check_product_activate_compare($variation_id) && WC_Compare_Functions::check_product_have_cat($variation_id)) {
                 $compare_category = get_post_meta($variation_id, '_woo_compare_category', true);
                 $compare_fields = WC_Compare_Categories_Fields_Data::get_results("cat_id='" . $compare_category . "'", 'cf.field_order ASC');
                 if (is_array($compare_fields) && count($compare_fields) > 0) {
                     $html .= '<div class="compare_product_variation"><h2>' . WC_Compare_Functions::get_variation_name($variation_id) . '</h2></div>';
                     if ($use_wootheme_style) {
                         $html .= '<table class="compare_featured_fields shop_attributes">';
                     } else {
                         $html .= '<ul class="compare_featured_fields">';
                     }
                     $fixed_width = ' width="60%"';
                     foreach ($compare_fields as $field_data) {
                         $field_value = get_post_meta($variation_id, '_woo_compare_' . $field_data->field_key, true);
                         if (is_serialized($field_value)) {
                             $field_value = maybe_unserialize($field_value);
                         }
                         if (is_array($field_value) && count($field_value) > 0) {
                             $field_value = implode(', ', $field_value);
                         } elseif (is_array($field_value) && count($field_value) < 0) {
                             $field_value = $woo_compare_table_content_style['empty_text'];
                         }
                         if (trim($field_value) == '') {
                             $field_value = $woo_compare_table_content_style['empty_text'];
                         }
                         $field_unit = '';
                         if (trim($field_data->field_unit) != '') {
                             $field_unit = ' <span class="compare_featured_unit">(' . trim(stripslashes($field_data->field_unit)) . ')</span>';
                         }
                         if ($use_wootheme_style) {
                             $html .= '<tr><th><span class="compare_featured_name">' . stripslashes($field_data->field_name) . '</span>' . $field_unit . '</th><td ' . $fixed_width . '><span class="compare_featured_value">' . $field_value . '</span></td></tr>';
                         } else {
                             $html .= '<li class="compare_featured_item"><span class="compare_featured_name"><strong>' . stripslashes($field_data->field_name) . '</strong>' . $field_unit . '</span> : <span class="compare_featured_value">' . $field_value . '</span></li>';
                         }
                         $fixed_width = '';
                     }
                     if ($use_wootheme_style) {
                         $html .= '</table>';
                     } else {
                         $html .= '</ul>';
                     }
                 }
             }
         }
     } elseif (WC_Compare_Functions::check_product_activate_compare($product_id) && WC_Compare_Functions::check_product_have_cat($product_id)) {
         $compare_category = get_post_meta($product_id, '_woo_compare_category', true);
         $compare_fields = WC_Compare_Categories_Fields_Data::get_results("cat_id='" . $compare_category . "'", 'cf.field_order ASC');
         if (is_array($compare_fields) && count($compare_fields) > 0) {
             if ($use_wootheme_style) {
                 $html .= '<table class="compare_featured_fields shop_attributes">';
             } else {
                 $html .= '<ul class="compare_featured_fields">';
             }
             $fixed_width = ' width="60%"';
             foreach ($compare_fields as $field_data) {
                 $field_value = get_post_meta($product_id, '_woo_compare_' . $field_data->field_key, true);
                 if (is_serialized($field_value)) {
                     $field_value = maybe_unserialize($field_value);
                 }
                 if (is_array($field_value) && count($field_value) > 0) {
                     $field_value = implode(', ', $field_value);
                 } elseif (is_array($field_value) && count($field_value) < 0) {
                     $field_value = $woo_compare_table_content_style['empty_text'];
                 }
                 if (trim($field_value) == '') {
                     $field_value = $woo_compare_table_content_style['empty_text'];
                 }
                 $field_unit = '';
                 if (trim($field_data->field_unit) != '') {
                     $field_unit = ' <span class="compare_featured_unit">(' . trim(stripslashes($field_data->field_unit)) . ')</span>';
                 }
                 if ($use_wootheme_style) {
                     $html .= '<tr><th><span class="compare_featured_name">' . stripslashes($field_data->field_name) . '</span>' . $field_unit . '</th><td ' . $fixed_width . '><span class="compare_featured_value">' . $field_value . '</span></td></tr>';
                 } else {
                     $html .= '<li class="compare_featured_item"><span class="compare_featured_name"><strong>' . stripslashes($field_data->field_name) . '</strong>' . $field_unit . '</span> : <span class="compare_featured_value">' . $field_value . '</span></li>';
                 }
                 $fixed_width = '';
             }
             if ($use_wootheme_style) {
                 $html .= '</table>';
             } else {
                 $html .= '</ul>';
             }
         }
     }
     return $html;
 }
 public static function woocp_get_products()
 {
     check_ajax_referer('woocp-products-manager', 'security');
     $paged = isset($_POST['page']) ? $_POST['page'] : 1;
     $rp = isset($_POST['rp']) ? $_POST['rp'] : 10;
     $cp_show_variations = isset($_POST['cp_show_variations']) ? $_POST['cp_show_variations'] : 0;
     $start = ($paged - 1) * $rp;
     $sortname = isset($_POST['sortname']) ? $_POST['sortname'] : 'title';
     $sortorder = isset($_POST['sortorder']) ? $_POST['sortorder'] : 'asc';
     $query = isset($_POST['query']) ? $_POST['query'] : false;
     $qtype = isset($_POST['qtype']) ? $_POST['qtype'] : false;
     $data_a = array();
     $data_a['s'] = $query;
     $data_a['numberposts'] = $rp;
     $data_a['offset'] = $start;
     if ($sortname == 'title') {
         $data_a['orderby'] = $sortname;
     } else {
         $data_a['orderby'] = 'meta_value';
         $data_a['meta_key'] = $sortname;
     }
     $data_a['order'] = strtoupper($sortorder);
     $data_a['post_type'] = 'product';
     $data_a['post_status'] = array('private', 'publish');
     $all_data = array();
     $all_data['s'] = $query;
     $all_data['posts_per_page'] = 1;
     $all_data['post_type'] = 'product';
     $all_data['post_status'] = array('private', 'publish');
     //$all_products = get_posts($all_data);
     //$total = count($all_products);
     $query = new WP_Query($all_data);
     $total = $query->found_posts;
     //$total = $wpdb->get_var("SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_title LIKE '%{$query}%' AND post_type='wpsc-product' AND post_status IN ('private', 'publish') ;");
     $products = get_posts($data_a);
     $jsonData = array('page' => $paged, 'total' => $total, 'rows' => array());
     $number = $start;
     foreach ($products as $product) {
         $number++;
         //If cell's elements have named keys, they must match column names
         //Only cell's with named keys and matching columns are order independent.
         $terms = get_the_terms($product->ID, 'product_cat');
         $on_cats = '';
         if ($terms && !is_wp_error($terms)) {
             $cat_links = array();
             foreach ($terms as $term) {
                 $cat_links[] = $term->name;
             }
             $on_cats = join(", ", $cat_links);
         }
         $compare_category = get_post_meta($product->ID, '_woo_compare_category_name', true);
         $deactivate_compare_feature = get_post_meta($product->ID, '_woo_deactivate_compare_feature', true);
         if ($deactivate_compare_feature == 'no' && $compare_category != '') {
             $status = '<font style="color:green">' . __("Activated", 'woo_cp') . '</font>';
         } else {
             $status = '<font style="color:red">' . __("Deactivated", 'woo_cp') . '</font>';
         }
         $entry = array('id' => $product->ID, 'cell' => array('number' => $number, 'title' => $product->post_title, 'cat' => $on_cats, '_woo_compare_category_name' => $compare_category, '_woo_deactivate_compare_feature' => $status, 'edit' => '<span rel="' . $product->ID . '|' . $paged . '|' . $rp . '|' . $sortname . '|' . $sortorder . '|' . $cp_show_variations . '|' . $qtype . '" class="edit_product_compare">' . __("Edit", 'woo_cp') . '</span>'));
         $jsonData['rows'][] = $entry;
         if ($cp_show_variations == 1) {
             $args = array('post_type' => 'product_variation', 'post_status' => array('publish'), 'numberposts' => -1, 'orderby' => 'id', 'order' => 'asc', 'post_parent' => $product->ID);
             $variations = get_posts($args);
             if ($variations && is_array($variations) && count($variations) > 0) {
                 $sub = 0;
                 foreach ($variations as $variation) {
                     $sub++;
                     $compare_category = get_post_meta($variation->ID, '_woo_compare_category_name', true);
                     $deactivate_compare_feature = get_post_meta($variation->ID, '_woo_deactivate_compare_feature', true);
                     if ($deactivate_compare_feature == 'no' && $compare_category != '') {
                         $status = '<font style="color:green">' . __("Activated", 'woo_cp') . '</font>';
                     } else {
                         $status = '<font style="color:red">' . __("Deactivated", 'woo_cp') . '</font>';
                     }
                     $entry = array('id' => $variation->ID, 'cell' => array('number' => '', 'title' => '-- ' . WC_Compare_Functions::get_variation_name($variation->ID), 'cat' => $on_cats, '_woo_compare_category_name' => $compare_category, '_woo_deactivate_compare_feature' => $status, 'edit' => '<span rel="' . $variation->ID . '|' . $paged . '|' . $rp . '|' . $sortname . '|' . $sortorder . '|' . $cp_show_variations . '|' . $qtype . '" class="edit_product_compare">' . __("Edit", 'woo_cp') . '</span>'));
                     $jsonData['rows'][] = $entry;
                 }
             }
         }
     }
     echo json_encode($jsonData);
     die;
 }