/**
  * The function used to Add / Update product in woocommerce 
  *
  * @param array product
  * 
  * @return true if goes well without any error 
  */
 public function importProductToWoocommerce($products)
 {
     global $wpdb;
     $option = array(1 => 'one', 2 => 'two', 3 => 'three');
     $product_ids = array();
     foreach ($products['products'] as $product) {
         remove_all_actions('save_post');
         $ps_create_new = get_option('ps_create_new');
         # Product Setting if Create New Checked box is ON
         $name = $product['name'];
         $description = $product['description'];
         $reference = $product['sku'];
         // $list_price = $product['list_price'];
         $sell_price = $product['sell_price'];
         if (count($product['outlets']) != 0) {
             $quantity = 0;
             foreach ($product['outlets'] as $outlet) {
                 $product_type = get_option('product_sync_type');
                 if ($product_type == 'two_way') {
                     if (get_option('ps_wc_to_vend_outlet') == 'on') {
                         $selected_outlet = get_option('wc_to_vend_outlet_detail');
                         if (isset($selected_outlet) && !empty($selected_outlet)) {
                             $outlet_id = explode('|', $selected_outlet);
                             if (isset($outlet_id[1])) {
                                 if ($outlet_id[1] == $outlet['outlet_id']) {
                                     $quantity += $outlet['quantity'];
                                 }
                             }
                         }
                     }
                 } elseif ($product_type == 'vend_to_wc-way') {
                     if (get_option('ps_outlet') == 'on') {
                         $selected_outlet = get_option('ps_outlet_details');
                         if (isset($selected_outlet) && !empty($selected_outlet)) {
                             $outlet_id = explode('|', $selected_outlet);
                             foreach ($outlet_id as $id) {
                                 if ($id == $outlet['outlet_id']) {
                                     $quantity += $outlet['quantity'];
                                 }
                             }
                         }
                     }
                 }
             }
         } else {
             $outlet_checker = 'noOutlet';
         }
         $tax_name = $product['tax_name'];
         #Checking for Price Entered With Tax
         if (get_option('woocommerce_calc_taxes') == 'yes') {
             if (get_option('linksync_woocommerce_tax_option') == 'on') {
                 if (get_option('woocommerce_prices_include_tax') == 'yes') {
                     $excluding_tax = 'off';
                 } else {
                     $excluding_tax = 'on';
                 }
             } else {
                 $excluding_tax = get_option('excluding_tax');
             }
         } else {
             $excluding_tax = get_option('excluding_tax');
         }
         # @return product id if reference exists
         $result_reference = self::isReferenceExists($reference);
         #  Check if already exist product into woocommerce
         include_once ABSPATH . 'wp-admin/includes/image.php';
         if ($result_reference['result'] == 'success') {
             // it means it already exists
             $product_ids[] = $result_reference['data'] . '|update_id';
             $status = '';
             /*
              * Update exists product into WC
              */
             #Delete the product from the woocommerce with the value delected_at of the product
             if (get_option('ps_delete') == 'on') {
                 if (!empty($product['deleted_at'])) {
                     wp_delete_post($result_reference['data']);
                     //use the product Id and delete the product
                 }
             }
             #-----Delete All Relationship--------#
             if (get_option('ps_attribute') == 'on') {
                 $product_attributes = get_post_meta($result_reference['data'], '_product_attributes', TRUE);
                 if (isset($product_attributes) && !empty($product_attributes)) {
                     foreach ($product_attributes as $taxonomy_name => $taxonomy_detail) {
                         $taxonomy_query = mysql_query("SELECT term_taxonomy_id FROM `" . $wpdb->prefix . "term_taxonomy` WHERE `taxonomy`='{$taxonomy_name}'");
                         if (mysql_num_rows($taxonomy_query) != 0) {
                             while ($term_taxonmy_id_db = mysql_fetch_assoc($taxonomy_query)) {
                                 mysql_query("DELETE FROM `" . $wpdb->prefix . "term_relationships` WHERE object_id='" . $result_reference['data'] . "' AND term_taxonomy_id='" . $term_taxonmy_id_db['term_taxonomy_id'] . "'");
                             }
                         }
                     }
                 }
             }
             #-------------------------VARIENT DATA--------------------------------#
             if (isset($product['variants']) && !empty($product['variants'])) {
                 wp_set_object_terms($result_reference['data'], 'variable', 'product_type');
                 //this will create a variable product
                 $result_data = $this->variant_sku_check($product['variants'], $result_reference['data']);
                 if (isset($result_data) && !empty($result_data)) {
                     if (isset($result_data['thedata']) && !empty($result_data['thedata'])) {
                         $product_attributes = get_post_meta($result_reference['data'], '_product_attributes', TRUE);
                         if (isset($product_attributes) && !empty($product_attributes)) {
                             foreach ($result_data['thedata'] as $exists_attribute_key => $exists_attribute_value) {
                                 $exists_attribute[] = $exists_attribute_key;
                                 $var[$exists_attribute_key] = $exists_attribute_value;
                             }
                             for ($i = 0; $i < COUNT($exists_attribute); $i++) {
                                 foreach ($product_attributes as $key => $p_attribute) {
                                     if ($exists_attribute[$i] == $key) {
                                         $thedata[$key] = $p_attribute;
                                         break;
                                     } else {
                                         $thedata[$exists_attribute[$i]] = $var[$exists_attribute[$i]];
                                     }
                                 }
                             }
                             if (get_option('ps_attribute') == 'off') {
                                 $thedata = array_merge($thedata, $product_attributes);
                             }
                             if (get_option('linksync_visiable_attr') == 0) {
                                 foreach ($thedata as $visible) {
                                     $thedata[$visible['name']]['is_visible'] = 0;
                                 }
                             }
                         } else {
                             $thedata = $result_data['thedata'];
                         }
                         update_post_meta($result_reference['data'], '_product_attributes', $thedata);
                         //ADD Product Attribute
                         unset($thedata);
                         unset($result_data['thedata']);
                         unset($exists_attribute);
                         unset($var);
                     }
                     /*
                      * Max and Min variation prices
                      */
                     $variant_product_id_query = mysql_query("SELECT * FROM  `" . $wpdb->prefix . "posts` WHERE  `post_parent` ='" . $result_reference['data'] . "' AND  `post_type` =  'product_variation'");
                     $variant = array();
                     while ($variant_product_ids = mysql_fetch_assoc($variant_product_id_query)) {
                         $variant[] = $variant_product_ids['ID'];
                     }
                     $price_list = array('_price', '_sale_price', '_regular_price');
                     $max_and_min = array('max', 'min');
                     foreach ($max_and_min as $check) {
                         foreach ($price_list as $price) {
                             $db_prices_query = mysql_query("SELECT * FROM  `" . $wpdb->prefix . "postmeta` WHERE  `meta_key` =  '{$price}' AND  `post_id` IN (" . implode(',', $variant) . ")");
                             $max_min_price_handle = array();
                             while ($db_prices = mysql_fetch_assoc($db_prices_query)) {
                                 if (isset($db_prices['meta_value']) && !empty($db_prices['meta_value'])) {
                                     $max_min_price_handle[$price][$db_prices['post_id']] = $db_prices['meta_value'];
                                 }
                             }
                             if (isset($max_min_price_handle[$price]) && !empty($max_min_price_handle[$price])) {
                                 $set_price_id = array_keys($max_min_price_handle[$price], $check($max_min_price_handle[$price]));
                                 $set_price = $check($max_min_price_handle[$price]);
                                 if ($price == '_price' || $check == 'min') {
                                     update_post_meta($result_reference['data'], '_price', $set_price);
                                 }
                                 update_post_meta($result_reference['data'], '_' . $check . '_variation' . $price, $set_price);
                                 update_post_meta($result_reference['data'], '_' . $check . $price . '_variation_id', $set_price_id[0]);
                             }
                         }
                     }
                     // if all variants has qty is 0
                     if (isset($result_data['var_quantity'])) {
                         if ($result_data['var_quantity'] <= 0) {
                             if (isset($outlet_checker) && $outlet_checker == 'noOutlet') {
                             } else {
                                 update_post_meta($result_reference['data'], '_stock_status', 'outofstock');
                                 if (get_option('ps_unpublish') == 'on') {
                                     $status = 'draft';
                                 }
                             }
                         } else {
                             update_post_meta($result_reference['data'], '_stock_status', 'instock');
                         }
                     }
                 }
                 #----------------------------------------END VARIENT DATA----------------------------------------#
             }
             #Tag of the Products
             if (get_option('ps_tags') == 'on') {
                 $term_exists['term_id'] = 0;
                 # default parent id is 0
                 if (isset($product['tags'])) {
                     $data = mysql_query("SELECT term_taxonomy_id FROM  `" . $wpdb->prefix . "term_taxonomy` WHERE taxonomy='product_tag'");
                     while ($term_taxonmy_id = mysql_fetch_assoc($data)) {
                         mysql_query("DELETE FROM `" . $wpdb->prefix . "term_relationships` WHERE object_id='" . $result_reference['data'] . "' AND term_taxonomy_id='" . $term_taxonmy_id['term_taxonomy_id'] . "'");
                     }
                     foreach ($product['tags'] as $tag) {
                         if (isset($tag['name']) && !empty($tag['name']) && isset($term_exists['term_id'])) {
                             $check_term_exists = term_exists($tag['name'], 'product_tag', $term_exists['term_id']);
                             # just check if tag with name already created
                             if (!is_array($check_term_exists)) {
                                 $term_exists = (array) wp_insert_term($tag['name'], 'product_tag');
                                 if (isset($term_exists['term_taxonomy_id']) && $term_exists['term_id']) {
                                     mysql_query("INSERT INTO `" . $wpdb->prefix . "term_relationships`(object_id,term_taxonomy_id,term_order) VALUES('" . $result_reference['data'] . "','" . $term_exists['term_taxonomy_id'] . "',0)");
                                     mysql_query("UPDATE `" . $wpdb->prefix . "term_taxonomy` SET count=count+1 WHERE term_id='" . $term_exists['term_id'] . "'");
                                 }
                             } else {
                                 mysql_query("INSERT INTO `" . $wpdb->prefix . "term_relationships`(object_id,term_taxonomy_id,term_order) VALUES('" . $result_reference['data'] . "','" . $check_term_exists['term_taxonomy_id'] . "',0)");
                                 mysql_query("UPDATE `" . $wpdb->prefix . "term_taxonomy` SET count=count+1 WHERE term_id='" . $check_term_exists['term_id'] . "'");
                             }
                         }
                     }
                 }
             }
             # BRAND syncing ( update )
             if (in_array('woocommerce-brands/woocommerce-brands.php', apply_filters('active_plugins', get_option('active_plugins')))) {
                 if (get_option('ps_brand') == 'on') {
                     // Delete existing brand then create
                     $term_taxonmy_id = array();
                     $data = mysql_query("SELECT term_taxonomy_id FROM  `" . $wpdb->prefix . "term_taxonomy` WHERE taxonomy='product_brand'");
                     while ($exists_brands = mysql_fetch_assoc($data)) {
                         mysql_query("DELETE FROM `" . $wpdb->prefix . "term_relationships` WHERE object_id='" . $result_reference['data'] . "' AND term_taxonomy_id='" . $exists_brands['term_taxonomy_id'] . "'");
                     }
                     if (isset($product['brands']) && !empty($product['brands'])) {
                         $brands = $product['brands'];
                         foreach ($brands as $brand) {
                             if (isset($brand['name']) && !empty($brand['name'])) {
                                 if (!ctype_space($brand['name'])) {
                                     // if coming with white space
                                     $termid_taxonomy = term_exists($brand['name'], 'product_brand');
                                     if (!is_array($termid_taxonomy)) {
                                         $termid_taxonomy = @wp_insert_term($brand['name'], 'product_brand');
                                     }
                                     if (!isset($termid_taxonomy->errors)) {
                                         //print_r($termid_taxonomy);
                                         if (isset($termid_taxonomy['term_taxonomy_id']) && isset($termid_taxonomy['term_id'])) {
                                             mysql_query("INSERT INTO `" . $wpdb->prefix . "term_relationships`(object_id,term_taxonomy_id,term_order) VALUES('" . $result_reference['data'] . "','" . $termid_taxonomy['term_taxonomy_id'] . "',0)") or die('Error in Line: ' . __LINE__ . " " . mysql_error());
                                             mysql_query("UPDATE `" . $wpdb->prefix . "term_taxonomy` SET count=count+1  WHERE term_id='" . $termid_taxonomy['term_id'] . "'") or die('Error in Line: ' . __LINE__ . " " . mysql_error());
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     unset($termid_taxonomy);
                 }
             }
             #Category
             if (get_option('ps_categories') == 'on') {
                 $data = mysql_query("SELECT term_taxonomy_id FROM  `" . $wpdb->prefix . "term_taxonomy` WHERE taxonomy='product_cat'");
                 while ($term_taxonmy_id = mysql_fetch_assoc($data)) {
                     mysql_query("DELETE FROM `" . $wpdb->prefix . "term_relationships` WHERE object_id='" . $result_reference['data'] . "' AND term_taxonomy_id='" . $term_taxonmy_id['term_taxonomy_id'] . "'");
                 }
                 if (get_option('cat_radio') == 'ps_cat_product_type') {
                     if (isset($product['product_type']) && !empty($product['product_type'])) {
                         $term_exists = term_exists($product['product_type'], 'product_cat');
                         if (is_array($term_exists)) {
                             mysql_query("INSERT INTO `" . $wpdb->prefix . "term_relationships`(object_id,term_taxonomy_id,term_order) VALUES('" . $result_reference['data'] . "','" . $term_exists['term_taxonomy_id'] . "',0)");
                             mysql_query("UPDATE `" . $wpdb->prefix . "term_taxonomy`  SET count=count+1  WHERE term_id='" . $term_exists['term_id'] . "'");
                         }
                     }
                 }
                 if (get_option('cat_radio') == 'ps_cat_tags') {
                     //Product Tag is Selected
                     if (isset($product['tags']) && !empty($product['tags'])) {
                         foreach ($product['tags'] as $tag) {
                             $cat_parent_id = 0;
                             $flag_cat = array();
                             if (isset($tag['name']) && !empty($tag['name'])) {
                                 $tags = explode('/', $tag['name']);
                                 if (isset($tags) && !empty($tags)) {
                                     foreach ($tags as $cat_key => $cat_name) {
                                         $check_term_exists = term_exists($cat_name, 'product_cat', $cat_parent_id);
                                         if (is_array($check_term_exists)) {
                                             $flag_cat[] = 'yes';
                                             $cat_parent_id = $check_term_exists['term_id'];
                                             $cat_taxonmy = $check_term_exists;
                                         } else {
                                             $flag_cat[] = 'no';
                                             $cat_parent_id = 0;
                                         }
                                     }
                                     if (!in_array('no', $flag_cat)) {
                                         mysql_query("INSERT INTO `" . $wpdb->prefix . "term_relationships`(object_id,term_taxonomy_id,term_order) VALUES('" . $result_reference['data'] . "','" . $cat_taxonmy['term_taxonomy_id'] . "',0)");
                                         mysql_query("UPDATE `" . $wpdb->prefix . "term_taxonomy` SET count=count+1 WHERE term_id='" . $cat_taxonmy['term_id'] . "'");
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             if (isset($product['variants']) && !empty($product['variants'])) {
                 update_post_meta($result_reference['data'], '_regular_price', '');
                 update_post_meta($result_reference['data'], '_sale_price', '');
             } else {
                 # defined fundtion to update existing product
                 if (get_option('ps_price') == 'on') {
                     $update_tax_classes = get_option('tax_class');
                     if (isset($update_tax_classes) && !empty($update_tax_classes)) {
                         $taxes_all = explode(',', $update_tax_classes);
                         if (isset($taxes_all) && !empty($taxes_all)) {
                             foreach ($taxes_all as $taxes) {
                                 $tax = explode('|', $taxes);
                                 if (isset($tax) && !empty($tax)) {
                                     $explode_tax_name = explode('-', $tax[0]);
                                     //GST-1.0 to explode GST and 1.0
                                     if (in_array($tax_name, $explode_tax_name)) {
                                         $explode = explode(' ', $tax[1]);
                                         $implode = implode('-', $explode);
                                         $tax_mapping_name = strtolower($implode);
                                         update_post_meta($result_reference['data'], '_tax_status', 'taxable');
                                         if ($tax_mapping_name == 'standard-tax') {
                                             $tax_mapping_name = '';
                                         }
                                         update_post_meta($result_reference['data'], '_tax_class', $tax_mapping_name);
                                     }
                                 }
                             }
                         }
                     }
                     $db_sale_price = mysql_query("SELECT * FROM `" . $wpdb->prefix . "postmeta` WHERE `post_id` = '" . $result_reference['data'] . "' AND meta_key='_sale_price'");
                     if ($excluding_tax == 'on') {
                         //If 'yes' then product price SELL Price(excluding any taxes.)
                         if (0 != mysql_num_rows($db_sale_price)) {
                             $result_sale_price = mysql_fetch_assoc($db_sale_price);
                             if ($result_sale_price['meta_value'] == NULL) {
                                 update_post_meta($result_reference['data'], '_price', $sell_price);
                             }
                         } else {
                             update_post_meta($result_reference['data'], '_price', $sell_price);
                         }
                         if (get_option('price_field') == 'regular_price') {
                             update_post_meta($result_reference['data'], '_regular_price', $sell_price);
                         } else {
                             update_post_meta($result_reference['data'], '_price', $sell_price);
                             update_post_meta($result_reference['data'], '_sale_price', $sell_price);
                         }
                     } else {
                         //If 'no' then product price SELL Price(including any taxes.)
                         $tax_and_sell_price_product = $sell_price + $product['tax_value'];
                         if (0 != mysql_num_rows($db_sale_price)) {
                             $result_sale_price = mysql_fetch_assoc($db_sale_price);
                             if ($result_sale_price['meta_value'] == NULL) {
                                 update_post_meta($result_reference['data'], '_price', $tax_and_sell_price_product);
                             }
                         } else {
                             update_post_meta($result_reference['data'], '_price', $tax_and_sell_price_product);
                         }
                         if (get_option('price_field') == 'regular_price') {
                             update_post_meta($result_reference['data'], '_regular_price', $tax_and_sell_price_product);
                         } else {
                             update_post_meta($result_reference['data'], '_price', $tax_and_sell_price_product);
                             update_post_meta($result_reference['data'], '_sale_price', $tax_and_sell_price_product);
                         }
                     }
                 }
             }
             #Product Quantity
             if (get_option('ps_quantity') == 'on') {
                 if (isset($product['variants']) && !empty($product['variants'])) {
                 } else {
                     if (isset($outlet_checker) && $outlet_checker == 'noOutlet') {
                         update_post_meta($result_reference['data'], '_manage_stock', 'no');
                         update_post_meta($result_reference['data'], '_stock', NULL);
                         update_post_meta($result_reference['data'], '_stock_status', 'instock');
                     } else {
                         update_post_meta($result_reference['data'], '_manage_stock', 'yes');
                         update_post_meta($result_reference['data'], '_stock', $quantity);
                         update_post_meta($result_reference['data'], '_stock_status', $quantity > 0 ? 'instock' : 'outofstock');
                         if (get_option('ps_unpublish') == 'on' && $quantity < 1) {
                             $status = 'draft';
                         }
                     }
                     unset($outlet_checker);
                 }
             }
             #Product Image
             if (get_option('ps_images') == 'on') {
                 //Product Gallery Image
                 $woo_filename_gallery = array();
                 $image_query = mysql_query("SELECT meta_value FROM  `" . $wpdb->prefix . "postmeta` WHERE  meta_key='_product_image_gallery' AND `post_id` ='" . $result_reference['data'] . "'");
                 $result_image = mysql_num_rows($image_query);
                 if (isset($result_image) && !empty($result_image)) {
                     $image = mysql_fetch_assoc($image_query);
                     if (isset($image['meta_value']) && !empty($image['meta_value'])) {
                         if (strpos($image['meta_value'], ',')) {
                             $images_postId = explode(',', $image['meta_value']);
                         } else {
                             $images_postId[] = $image['meta_value'];
                         }
                         if (isset($images_postId) && !empty($images_postId)) {
                             foreach ($images_postId as $value) {
                                 $wp_attached_file = get_post_meta($value, '_wp_attached_file', true);
                                 // returns an array
                                 if (isset($wp_attached_file) && !empty($wp_attached_file)) {
                                     $woo_filename_gallery[$value] = basename($wp_attached_file);
                                 }
                             }
                         }
                     }
                 }
                 $current_user_id = get_current_user_id();
                 foreach ($product['images'] as $key => $images) {
                     $vend_image_data[$key . '|' . $images['url']] = basename($images['url']);
                 }
                 if ($current_user_id == 0) {
                     // logged_one is 'System';
                     if (isset($product['images']) && !empty($product['images'])) {
                         //Thumbnail Image data
                         if (isset($product['images'][0]['url']) && !empty($product['images'][0]['url'])) {
                             /*
                              *  Ongoing Seleted->This option provides the same function as 'Once',
                              *  but will update product images if the they are modified in Vend.
                              *  For example, if you update an image for a product in Vend, then that update images will be synced to the corresponding 
                              *  product in WooCommerce.
                              */
                             if (get_option('ps_import_image_radio') == 'Ongoing') {
                                 $image_query = mysql_query("SELECT meta_value FROM  `" . $wpdb->prefix . "postmeta` WHERE  meta_key='_thumbnail_id' AND `post_id` ='" . $result_reference['data'] . "'");
                                 if (mysql_num_rows($image_query) > 1) {
                                     while ($images = mysql_fetch_assoc($image_query)) {
                                         if (isset($images['meta_value']) && !empty($images['meta_value'])) {
                                             $image_attributes = get_post_meta($images['meta_value'], '_wp_attached_file', true);
                                             // returns an array  @wp_get_attachment_image_src($image[0]);
                                             if (isset($image_attributes) && !empty($image_attributes)) {
                                                 $path_parts = pathinfo($image_attributes);
                                                 $wp_upload_dir = @wp_upload_dir();
                                                 $ext = substr($image_attributes, strrpos($image_attributes, "."));
                                                 $filename = $wp_upload_dir['basedir'] . '/' . $path_parts['dirname'] . '/' . basename($image_attributes, $ext);
                                                 foreach (glob("{$filename}*") as $filename) {
                                                     if (file_exists($filename)) {
                                                         unlink($filename);
                                                     }
                                                 }
                                             }
                                             delete_post_meta($result_reference['data'], '_thumbnail_id');
                                         }
                                     }
                                 }
                                 $image = mysql_fetch_assoc($image_query);
                                 if (isset($image['meta_value']) && !empty($image['meta_value'])) {
                                     $image_attributes = get_post_meta($image['meta_value'], '_wp_attached_file', true);
                                     // returns an array  @wp_get_attachment_image_src($image[0]);
                                     if (isset($image_attributes) && !empty($image_attributes)) {
                                         checkAndDelete_attachement(basename($image_attributes));
                                         if (in_array(basename($image_attributes), $vend_image_data)) {
                                             $product_image_search = array_search(basename($image_attributes), $vend_image_data);
                                             $result_image_search = explode('|', $product_image_search);
                                             unset($product['images'][$result_image_search[0]]);
                                         } else {
                                             $thumbnail_id = addImage_thumbnail($product['images'][0]['url'], $result_reference['data']);
                                             update_post_meta($result_reference['data'], '_thumbnail_id', $thumbnail_id);
                                             unset($product['images'][0]);
                                         }
                                     }
                                 } else {
                                     if (!in_array(basename($product['images'][0]['url']), $woo_filename_gallery)) {
                                         $thumbnail_id = addImage_thumbnail($product['images'][0]['url'], $result_reference['data']);
                                         update_post_meta($result_reference['data'], '_thumbnail_id', $thumbnail_id);
                                         unset($product['images'][0]);
                                     } else {
                                         $left_upload = array_diff($vend_image_data, $woo_filename_gallery);
                                         foreach ($left_upload as $upload => $value_upload) {
                                             $image_left = explode('|', $upload);
                                             $thumbnail_id = addImage_thumbnail($image_left[1], $result_reference['data']);
                                             update_post_meta($result_reference['data'], '_thumbnail_id', $thumbnail_id);
                                             unset($product['images'][$image_left[0]]);
                                             break;
                                         }
                                     }
                                 }
                                 unset($thumbnail_id);
                             } elseif (get_option('ps_import_image_radio') == 'Enable') {
                                 $thumb_query = mysql_query("SELECT meta_value FROM  `" . $wpdb->prefix . "postmeta` WHERE  meta_key='_thumbnail_id' AND `post_id` ='" . $result_reference['data'] . "'");
                                 $image = mysql_num_rows($thumb_query);
                                 if ($image != 0) {
                                     $image_id = mysql_fetch_assoc($thumb_query);
                                     $image_name = get_post_meta($image_id['meta_value'], '_wp_attached_file', TRUE);
                                     $unsetvalue = array_search(basename($image_name), $vend_image_data);
                                     if ($unsetvalue) {
                                         $image_left = explode('|', $unsetvalue);
                                         unset($product['images'][$image_left[0]]);
                                     }
                                 } else {
                                     if (!in_array(basename($product['images'][0]['url']), $woo_filename_gallery)) {
                                         $attach_id = addImage_thumbnail($product['images'][0]['url'], $result_reference['data']);
                                         add_post_meta($result_reference['data'], '_thumbnail_id', $attach_id);
                                         unset($product['images'][0]);
                                     } else {
                                         $left_upload = array_diff($vend_image_data, $woo_filename_gallery);
                                         foreach ($left_upload as $upload => $value_upload) {
                                             $image_left = explode('|', $upload);
                                             $attach_id = addImage_thumbnail($image_left[1], $result_reference['data']);
                                             add_post_meta($result_reference['data'], '_thumbnail_id', $attach_id);
                                             unset($product['images'][$image_left[0]]);
                                             break;
                                         }
                                     }
                                     unset($attach_id);
                                 }
                             }
                         }
                         if (isset($product['images']) && !empty($product['images'])) {
                             foreach ($product['images'] as $images) {
                                 if (get_option('ps_import_image_radio') == 'Ongoing') {
                                     if (!in_array(basename($images['url']), $woo_filename_gallery)) {
                                         $attach_ids[] = linksync_insert_image($images['url'], $result_reference['data']);
                                     } else {
                                         $attach_ids[] = array_search(basename($images['url']), $woo_filename_gallery);
                                     }
                                 } elseif (get_option('ps_import_image_radio') == 'Enable') {
                                     $image = mysql_num_rows(mysql_query("SELECT * FROM  `" . $wpdb->prefix . "postmeta` WHERE  meta_key='_product_image_gallery' AND `post_id` ='" . $result_reference['data'] . "'"));
                                     if ($image != 0) {
                                     } else {
                                         $attach_ids[] = linksync_insert_image($images['url'], $result_reference['data']);
                                     }
                                 }
                             }
                         }
                         if (get_option('ps_import_image_radio') == 'Ongoing') {
                             if (isset($attach_ids) && !empty($attach_ids)) {
                                 $product_image_gallery = implode(",", $attach_ids);
                                 update_post_meta($result_reference['data'], '_product_image_gallery', $product_image_gallery);
                             } else {
                                 update_post_meta($result_reference['data'], '_product_image_gallery', '');
                             }
                         } elseif (get_option('ps_import_image_radio') == 'Enable') {
                             if (isset($attach_ids) && !empty($attach_ids)) {
                                 $product_image_gallery = implode(",", $attach_ids);
                                 add_post_meta($result_reference['data'], '_product_image_gallery', $product_image_gallery);
                             }
                         }
                         unset($attach_ids);
                         unset($product_image_gallery);
                     } else {
                         if (get_option('product_sync_type') != 'two_way') {
                             if (get_option('ps_import_image_radio') == 'Ongoing') {
                                 $image = mysql_num_rows(mysql_query("SELECT * FROM  `" . $wpdb->prefix . "postmeta` WHERE  meta_key='_product_image_gallery' AND `post_id` ='" . $result_reference['data'] . "'"));
                                 if ($image != 0) {
                                     update_post_meta($result_reference['data'], '_product_image_gallery', '');
                                 }
                                 $thumbnail_image = mysql_num_rows(mysql_query("SELECT * FROM  `" . $wpdb->prefix . "postmeta` WHERE  meta_key='_thumbnail_id' AND `post_id` ='" . $result_reference['data'] . "'"));
                                 if ($thumbnail_image != 0) {
                                     update_post_meta($result_reference['data'], '_thumbnail_id', '');
                                 }
                             }
                         }
                     }
                 } else {
                     if (isset($product['images']) && !empty($product['images'])) {
                         //logged_one is current_user
                         if (isset($product['images'][0]['url']) && !empty($product['images'][0]['url'])) {
                             if (get_option('ps_import_image_radio') == 'Ongoing') {
                                 $image_query = mysql_query("SELECT meta_value FROM  `" . $wpdb->prefix . "postmeta` WHERE  meta_key='_thumbnail_id' AND `post_id` ='" . $result_reference['data'] . "'");
                                 $image = mysql_fetch_assoc($image_query);
                                 if (isset($image['meta_value']) && !empty($image['meta_value'])) {
                                     $image_attributes = get_post_meta($image['meta_value'], '_wp_attached_file', true);
                                     // returns an array  @wp_get_attachment_image_src($image[0]);
                                     if (isset($image_attributes) && !empty($image_attributes)) {
                                         if (in_array(basename($image_attributes), $vend_image_data)) {
                                             $product_image_search = array_search(basename($image_attributes), $vend_image_data);
                                             $result_image_search = explode('|', $product_image_search);
                                             unset($product['images'][$result_image_search[0]]);
                                             unset($vend_image_data);
                                             unset($product_image_search);
                                         } else {
                                             update_post_meta($result_reference['data'], 'Vend_thumbnail_image', $product['images'][0]['url']);
                                             unset($product['images'][0]);
                                         }
                                     }
                                 } else {
                                     if (isset($woo_filename_gallery) && !empty($woo_filename_gallery)) {
                                         if (!in_array(basename($product['images'][0]['url']), $woo_filename_gallery)) {
                                             update_post_meta($result_reference['data'], 'Vend_thumbnail_image', $product['images'][0]['url']);
                                             unset($product['images'][0]);
                                         } else {
                                             $left_upload = array_diff($vend_image_data, $woo_filename_gallery);
                                             foreach ($left_upload as $upload => $value_upload) {
                                                 $image_left = explode('|', $upload);
                                                 update_post_meta($result_reference['data'], 'Vend_thumbnail_image', $image_left[1]);
                                                 unset($product['images'][$image_left[0]]);
                                                 break;
                                             }
                                         }
                                     } else {
                                         update_post_meta($result_reference['data'], 'Vend_thumbnail_image', $product['images'][0]['url']);
                                         unset($product['images'][0]);
                                     }
                                 }
                             } elseif (get_option('ps_import_image_radio') == 'Enable') {
                                 $image_query = mysql_query("SELECT meta_value FROM  `" . $wpdb->prefix . "postmeta` WHERE  meta_key='_thumbnail_id' AND `post_id` ='" . $result_reference['data'] . "'");
                                 $image = mysql_fetch_assoc($image_query);
                                 if (isset($image['meta_value']) && !empty($image['meta_value'])) {
                                     $image_attributes = get_post_meta($image['meta_value'], '_wp_attached_file', true);
                                     // returns an array  @wp_get_attachment_image_src($image[0]);
                                     if (isset($image_attributes) && !empty($image_attributes)) {
                                         if (in_array(basename($image_attributes), $vend_image_data)) {
                                             $product_image_search = array_search(basename($image_attributes), $vend_image_data);
                                             $result_image_search = explode('|', $product_image_search);
                                             unset($product['images'][$result_image_search[0]]);
                                             unset($vend_image_data);
                                             unset($product_image_search);
                                         } else {
                                             if (isset($woo_filename_gallery) && !empty($woo_filename_gallery)) {
                                                 if (!in_array(basename($product['images'][0]['url']), $woo_filename_gallery)) {
                                                     update_post_meta($result_reference['data'], 'Vend_thumbnail_image', $product['images'][0]['url']);
                                                     unset($product['images'][0]);
                                                 } else {
                                                     $left_upload = array_diff($vend_image_data, $woo_filename_gallery);
                                                     foreach ($left_upload as $upload => $value_upload) {
                                                         $image_left = explode('|', $upload);
                                                         update_post_meta($result_reference['data'], 'Vend_thumbnail_image', $image_left[1]);
                                                         unset($product['images'][$image_left[0]]);
                                                         break;
                                                     }
                                                 }
                                             } else {
                                                 update_post_meta($result_reference['data'], 'Vend_thumbnail_image', $product['images'][0]['url']);
                                                 unset($product['images'][0]);
                                             }
                                         }
                                     }
                                 } else {
                                     if (isset($woo_filename_gallery) && !empty($woo_filename_gallery)) {
                                         if (!in_array(basename($product['images'][0]['url']), $woo_filename_gallery)) {
                                             update_post_meta($result_reference['data'], 'Vend_thumbnail_image', $product['images'][0]['url']);
                                             unset($product['images'][0]);
                                         } else {
                                             $left_upload = array_diff($vend_image_data, $woo_filename_gallery);
                                             foreach ($left_upload as $upload => $value_upload) {
                                                 $image_left = explode('|', $upload);
                                                 update_post_meta($result_reference['data'], 'Vend_thumbnail_image', $image_left[1]);
                                                 unset($product['images'][$image_left[0]]);
                                                 break;
                                             }
                                         }
                                     } else {
                                         update_post_meta($result_reference['data'], 'Vend_thumbnail_image', $product['images'][0]['url']);
                                         unset($product['images'][0]);
                                     }
                                 }
                             }
                         } else {
                             if (get_option('ps_import_image_radio') == 'Ongoing') {
                                 update_post_meta($result_reference['data'], '_thumbnail_id', '');
                             }
                         }
                         if (isset($product['images']) && !empty($product['images'])) {
                             delete_post_meta($result_reference['data'], 'Vend_product_image_gallery');
                             foreach ($product['images'] as $images) {
                                 $vend_gallery_image = mysql_num_rows(mysql_query("SELECT * FROM  `" . $wpdb->prefix . "postmeta` WHERE  meta_key='Vend_product_image_gallery' AND `post_id` ='" . $result_reference['data'] . "'"));
                                 if ($vend_gallery_image != 0) {
                                     mysql_query("UPDATE `" . $wpdb->prefix . "postmeta` SET meta_value=CONCAT(meta_value,',{$images['url']}') WHERE post_id='" . $result_reference['data'] . "' AND meta_key='Vend_product_image_gallery'");
                                 } else {
                                     add_post_meta($result_reference['data'], 'Vend_product_image_gallery', $images['url']);
                                 }
                             }
                         } else {
                             update_post_meta($result_reference['data'], '_product_image_gallery', '');
                         }
                         unset($product['images']);
                     } else {
                         if (get_option('product_sync_type') != 'two_way') {
                             if (get_option('ps_import_image_radio') == 'Ongoing') {
                                 update_post_meta($result_reference['data'], '_thumbnail_id', '');
                                 update_post_meta($result_reference['data'], '_product_image_gallery', '');
                             }
                         }
                     }
                 }
             }
             // if product in vend having status : inactive ( active==0  ) it should be not displayed (mark as draft in woo)
             if ($product['active'] == '0') {
                 $status = 'draft';
             }
             #---------GET product Status-------------#
             $product_status_db = mysql_query("SELECT post_status FROM `" . $wpdb->prefix . "posts` WHERE post_status ='pending' AND ID='" . $result_reference['data'] . "'");
             if (mysql_num_rows($product_status_db) != 0) {
                 $status = 'pending';
             }
             if (get_option('ps_unpublish') == 'on') {
                 $status = isset($status) && !empty($status) ? $status : 'publish';
             } else {
                 $product_status_db = mysql_query("SELECT post_status FROM `" . $wpdb->prefix . "posts` WHERE ID='" . $result_reference['data'] . "'");
                 if (mysql_num_rows($product_status_db) != 0) {
                     $product_status = mysql_fetch_assoc($product_status_db);
                     $status = $product_status['post_status'];
                 }
             }
             //                $product_detail = mysql_query("SELECT post_content,post_excerpt FROM `" . $wpdb->prefix . "posts` WHERE post_type='product' AND ID='" . $result_reference['data'] . "'");
             //                 if (0 != mysql_num_rows($query)) {
             //                    $detail = mysql_fetch_assoc($product_detail);
             //                    if (isset($detail['post_content']) && !empty($detail['post_content'])) {
             //                        $description_status = 'yes';
             //                    }
             //                    if (isset($detail['post_excerpt']) && !empty($detail['post_excerpt'])) {
             //                        $description_excerpt = 'yes';
             //                    }
             //                }
             $my_product = array();
             $my_product['ID'] = $result_reference['data'];
             $my_product['post_status'] = $status;
             $my_product['post_modified'] = current_time('mysql');
             $my_product['post_modified_gmt'] = gmdate('Y-m-d h:i:s');
             if (get_option('ps_name_title') == 'on') {
                 $my_product['post_title'] = $name;
             }
             //Import Description
             if (get_option('ps_description') == 'on') {
                 if (isset($description) && !empty($description)) {
                     //if(isset($description_status)&&$description_status=='yes'){
                     $my_product['post_content'] = $description;
                     // }
                 }
             }
             // Import Copy Short Description
             if (get_option('ps_desc_copy') == 'on') {
                 if (isset($description) && !empty($description)) {
                     // if(isset($description_excerpt)&&$description_excerpt=='yes'){
                     $my_product['post_excerpt'] = $description;
                     //}
                 }
             }
             if (get_option('ps_unpublish') == 'on') {
                 update_post_meta($result_reference['data'], '_visibility', $status == 'publish' ? 'visible' : '');
             }
             //Update product Post
             wp_update_post($my_product);
             unset($status);
             wc_delete_product_transients($result_reference['data']);
             /*
              * Ending Update product
              */
         } elseif ($result_reference['result'] == 'error') {
             /*
              * New Product Creation if "Create New" option enabled 
              */
             if ($ps_create_new == 'on' && empty($product['deleted_at'])) {
                 # it's new product
                 $status = '';
                 // code for adding new product int WC
                 $my_post = array('post_author' => 1, 'post_type' => 'product');
                 //Import Name
                 //  if (get_option('ps_name_title') == 'on') #we have used wp_insert_post() function that required at least one parameters
                 $my_post['post_title'] = $product['name'];
                 //Import Description
                 if (get_option('ps_description') == 'on') {
                     $my_post['post_content'] = $description;
                 }
                 // Import Copy Short Description
                 if (get_option('ps_desc_copy') == 'on') {
                     $my_post['post_excerpt'] = $description;
                 }
                 $product_ID = wp_insert_post($my_post);
                 $product_ids[] = $product_ID . '|new_id';
                 if ($product_ID) {
                     add_post_meta($product_ID, '_sku', $product['sku']);
                     #Tag of the Products
                     if (get_option('ps_tags') == 'on') {
                         $term_exists['term_id'] = 0;
                         foreach ($product['tags'] as $tag) {
                             if (isset($tag['name']) && !empty($tag['name'])) {
                                 $check_term_exists = term_exists($tag['name'], 'product_tag', $term_exists['term_id']);
                                 if (!is_array($check_term_exists)) {
                                     $term_exists = wp_insert_term($tag['name'], 'product_tag');
                                 }
                                 $term_exists = term_exists($tag['name'], 'product_tag');
                                 if (is_array($term_exists)) {
                                     mysql_query("INSERT INTO `" . $wpdb->prefix . "term_relationships`(object_id,term_taxonomy_id,term_order) VALUES('" . $product_ID . "','" . $term_exists['term_taxonomy_id'] . "',0)");
                                     mysql_query("UPDATE `" . $wpdb->prefix . "term_taxonomy` SET count=count+1  WHERE term_id='" . $term_exists['term_id'] . "'");
                                 }
                             }
                         }
                     }
                     # BRAND syncing
                     if (in_array('woocommerce-brands/woocommerce-brands.php', apply_filters('active_plugins', get_option('active_plugins')))) {
                         if (get_option('ps_brand') == 'on') {
                             if (isset($product['brands']) && !empty($product['brands'])) {
                                 $brands = $product['brands'];
                                 foreach ($brands as $brand) {
                                     if (isset($brand['name']) && !empty($brand['name'])) {
                                         if (!ctype_space($brand['name'])) {
                                             $termid_taxonomy = term_exists($brand['name'], 'product_brand');
                                             if (!is_array($termid_taxonomy)) {
                                                 $termid_taxonomy = @wp_insert_term($brand['name'], 'product_brand');
                                             }
                                             if (!isset($termid_taxonomy->errors)) {
                                                 if (isset($termid_taxonomy['term_taxonomy_id']) && isset($termid_taxonomy['term_id'])) {
                                                     mysql_query("INSERT INTO `" . $wpdb->prefix . "term_relationships`(object_id,term_taxonomy_id,term_order) VALUES('" . $product_ID . "','" . $termid_taxonomy['term_taxonomy_id'] . "',0)");
                                                     mysql_query("UPDATE `" . $wpdb->prefix . "term_taxonomy` SET count=count+1  WHERE term_id='" . $termid_taxonomy['term_id'] . "'");
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                             unset($termid_taxonomy);
                         }
                     }
                     #Category
                     if (get_option('ps_categories') == 'on') {
                         if (get_option('cat_radio') == 'ps_cat_product_type') {
                             if (isset($product['product_type']) && !empty($product['product_type'])) {
                                 $term_exists = term_exists($product['product_type'], 'product_cat');
                                 if (is_array($term_exists)) {
                                     mysql_query("INSERT INTO `" . $wpdb->prefix . "term_relationships`(object_id,term_taxonomy_id,term_order) VALUES('" . $product_ID . "','" . $term_exists['term_taxonomy_id'] . "',0)");
                                     mysql_query("UPDATE `" . $wpdb->prefix . "term_taxonomy`  SET count=count+1  WHERE term_id='" . $term_exists['term_id'] . "'");
                                 }
                             }
                         }
                         if (get_option('cat_radio') == 'ps_cat_tags') {
                             //Product Tag is Selected
                             if (isset($product['tags']) && !empty($product['tags'])) {
                                 foreach ($product['tags'] as $tag) {
                                     $cat_parent_id = 0;
                                     $flag_cat = array();
                                     if (isset($tag['name']) && !empty($tag['name'])) {
                                         $tags = explode('/', $tag['name']);
                                         if (isset($tags) && !empty($tags)) {
                                             foreach ($tags as $cat_key => $cat_name) {
                                                 $check_term_exists = term_exists($cat_name, 'product_cat', $cat_parent_id);
                                                 if (is_array($check_term_exists)) {
                                                     $flag_cat[] = 'yes';
                                                     $cat_parent_id = $check_term_exists['term_id'];
                                                     $cat_taxonmy = $check_term_exists;
                                                 } else {
                                                     $flag_cat[] = 'no';
                                                     $cat_parent_id = 0;
                                                 }
                                             }
                                             if (!in_array('no', $flag_cat)) {
                                                 mysql_query("INSERT INTO `" . $wpdb->prefix . "term_relationships`(object_id,term_taxonomy_id,term_order) VALUES('" . $product_ID . "','" . $cat_taxonmy['term_taxonomy_id'] . "',0)");
                                                 mysql_query("UPDATE `" . $wpdb->prefix . "term_taxonomy` SET count=count+1 WHERE term_id='" . $cat_taxonmy['term_id'] . "'");
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     if (isset($product['variants']) && !empty($product['variants'])) {
                         add_post_meta($product_ID, '_regular_price', '');
                         add_post_meta($product_ID, '_sale_price', '');
                     } else {
                         if (get_option('ps_price') == 'on') {
                             $new_product_taxes = get_option('tax_class');
                             if (isset($new_product_taxes) && !empty($new_product_taxes)) {
                                 $taxes_all = explode(',', $new_product_taxes);
                                 if (isset($taxes_all) && !empty($taxes_all)) {
                                     foreach ($taxes_all as $taxes) {
                                         $tax = explode('|', $taxes);
                                         if (isset($tax) && !empty($tax)) {
                                             $explode_tax_name = explode('-', $tax[0]);
                                             //GST-1.0 to explode GST and 1.0
                                             if (in_array($tax_name, $explode_tax_name)) {
                                                 $explode = explode(' ', $tax[1]);
                                                 $implode = implode('-', $explode);
                                                 $tax_mapping_name = strtolower($implode);
                                                 add_post_meta($product_ID, '_tax_status', 'taxable');
                                                 if ($tax_mapping_name == 'standard-tax') {
                                                     $tax_mapping_name = '';
                                                 }
                                                 add_post_meta($product_ID, '_tax_class', $tax_mapping_name);
                                             }
                                         }
                                     }
                                 }
                             }
                             if ($excluding_tax == 'on') {
                                 //If 'yes' then product price SELL Price(excluding any taxes.)
                                 add_post_meta($product_ID, '_price', $sell_price);
                                 if (get_option('price_field') == 'regular_price') {
                                     add_post_meta($product_ID, '_regular_price', $sell_price);
                                 } else {
                                     add_post_meta($product_ID, '_sale_price', $sell_price);
                                 }
                             } else {
                                 //If 'no' then product price SELL Price(including any taxes.)
                                 $tax_and_sell_price_product = $sell_price + $product['tax_value'];
                                 add_post_meta($product_ID, '_price', $tax_and_sell_price_product);
                                 if (get_option('price_field') == 'regular_price') {
                                     add_post_meta($product_ID, '_regular_price', $tax_and_sell_price_product);
                                 } else {
                                     add_post_meta($product_ID, '_sale_price', $tax_and_sell_price_product);
                                 }
                             }
                         }
                     }
                     #Product Image
                     if (get_option('ps_images') == 'on') {
                         $current_user_id = get_current_user_id();
                         if (get_option('ps_import_image_radio') == 'Enable' || get_option('ps_import_image_radio') == 'Ongoing') {
                             if (isset($product['images']) && !empty($product['images'])) {
                                 if (isset($product['images'][0]['url']) && !empty($product['images'][0]['url'])) {
                                     if ($current_user_id == 0) {
                                         $attach_id = addImage_thumbnail($product['images'][0]['url'], $product_ID);
                                         add_post_meta($product_ID, '_thumbnail_id', $attach_id);
                                     } else {
                                         add_post_meta($product_ID, 'Vend_thumbnail_image', $product['images'][0]['url']);
                                     }
                                     unset($attach_id);
                                 }
                                 unset($product['images'][0]);
                                 foreach ($product['images'] as $images) {
                                     if ($current_user_id == 0) {
                                         $attach_ids = linksync_insert_image($images['url'], $product_ID);
                                         $imageDb = get_post_meta($product_ID, '_product_image_gallery');
                                         if (isset($imageDb) && !empty($imageDb)) {
                                             mysql_query("UPDATE `" . $wpdb->prefix . "postmeta` SET meta_value=CONCAT(meta_value,',{$attach_ids}') WHERE post_id='" . $product_ID . "' AND meta_key='_product_image_gallery'");
                                         } else {
                                             add_post_meta($product_ID, '_product_image_gallery', $attach_ids);
                                         }
                                         unset($attach_ids);
                                     } else {
                                         $vend_gallery_image = get_post_meta($product_ID, 'Vend_product_image_gallery');
                                         if (isset($vend_gallery_image) && !empty($vend_gallery_image)) {
                                             mysql_query("UPDATE `" . $wpdb->prefix . "postmeta` SET meta_value=CONCAT(meta_value,',{$images['url']}') WHERE post_id='" . $product_ID . "' AND meta_key='Vend_product_image_gallery'");
                                         } else {
                                             add_post_meta($product_ID, 'Vend_product_image_gallery', $images['url']);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     #-------------------------VARIENT DATA--------------------------------#
                     if (isset($product['variants']) && !empty($product['variants'])) {
                         $thedata = array();
                         $var_qty = 0;
                         $price_max = array('max' => 0, 'max_variable_id' => 0);
                         $price_min = array('min' => 0, 'min_variable_id' => 0);
                         wp_set_object_terms($product_ID, 'variable', 'product_type');
                         //this will create a variable product
                         foreach ($product['variants'] as $product_variants) {
                             if ($product_variants['deleted_at'] == null) {
                                 $variant_status = 'publish';
                                 //  $list_price = $product_variants['list_price'];
                                 $sell_price = $product_variants['sell_price'];
                                 if (count($product_variants['outlets']) != 0) {
                                     $variant_quantity = 0;
                                     foreach ($product_variants['outlets'] as $outlet) {
                                         $product_type = get_option('product_sync_type');
                                         if ($product_type == 'two_way') {
                                             $selected_outlet = get_option('wc_to_vend_outlet_detail');
                                             $outlet_id = explode('|', $selected_outlet);
                                             if ($outlet_id[1] == $outlet['outlet_id']) {
                                                 $variant_quantity += (double) $outlet['quantity'];
                                             }
                                         } elseif ($product_type == 'vend_to_wc-way') {
                                             $selected_outlet = get_option('ps_outlet_details');
                                             $outlet_id = explode('|', $selected_outlet);
                                             foreach ($outlet_id as $id) {
                                                 if ($id == $outlet['outlet_id']) {
                                                     $variant_quantity += (double) $outlet['quantity'];
                                                 }
                                             }
                                         }
                                     }
                                 } else {
                                     $outlet_checker_variant = 'noOutlet';
                                 }
                                 if (isset($variant_quantity)) {
                                     $var_qty += $variant_quantity;
                                 }
                                 $tax_name = $product_variants['tax_name'];
                                 $my_post = array('post_title' => $product_variants['name'], 'post_status' => $variant_status, 'post_author' => 1, 'post_type' => 'product_variation', 'post_parent' => $product_ID);
                                 $variation_product_id = wp_insert_post($my_post);
                                 if ($variation_product_id) {
                                     add_post_meta($variation_product_id, '_sku', $product_variants['sku']);
                                     if (get_option('ps_unpublish') == 'on') {
                                         add_post_meta($variation_product_id, '_visibility', $variant_status == 'publish' ? 'visible' : '');
                                     }
                                     if (get_option('ps_price') == 'on') {
                                         $new_variant_taxes = get_option('tax_class');
                                         if (isset($new_variant_taxes) && !empty($new_variant_taxes)) {
                                             $taxes_all = explode(',', $new_variant_taxes);
                                             if (isset($taxes_all) && !empty($taxes_all)) {
                                                 foreach ($taxes_all as $taxes) {
                                                     $tax = explode('|', $taxes);
                                                     if (isset($tax) && !empty($tax)) {
                                                         $explode_tax_name = explode('-', $tax[0]);
                                                         //GST-1.0 to explode GST and 1.0
                                                         if (in_array($tax_name, $explode_tax_name)) {
                                                             $explode = explode(' ', $tax[1]);
                                                             $implode = implode('-', $explode);
                                                             $tax_mapping_name = strtolower($implode);
                                                             add_post_meta($variation_product_id, '_tax_status', 'taxable');
                                                             if ($tax_mapping_name == 'standard-tax') {
                                                                 $tax_mapping_name = '';
                                                             }
                                                             add_post_meta($variation_product_id, '_tax_class', $tax_mapping_name);
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                         if ($excluding_tax == 'on') {
                                             if ($price_max['max'] == 0) {
                                                 $price_max['max'] = $sell_price;
                                                 $price_max['max_variable_id'] = $variation_product_id;
                                             }
                                             if ($price_max['max'] <= $sell_price) {
                                                 $price_max['max'] = $sell_price;
                                                 $price_max['max_variable_id'] = $variation_product_id;
                                             }
                                             if ($price_min['min'] == 0) {
                                                 $price_max['min'] = $sell_price;
                                                 $price_max['min_variable_id'] = $variation_product_id;
                                             }
                                             if ($price_min['min'] >= $sell_price) {
                                                 $price_max['min'] = $sell_price;
                                                 $price_max['min_variable_id'] = $variation_product_id;
                                             }
                                             //If 'yes' then product price SELL Price(excluding any taxes.)
                                             add_post_meta($variation_product_id, '_price', $sell_price);
                                             if (get_option('price_field') == 'regular_price') {
                                                 add_post_meta($variation_product_id, '_regular_price', $sell_price);
                                             } else {
                                                 add_post_meta($variation_product_id, '_sale_price', $sell_price);
                                             }
                                         } else {
                                             //If 'no' then product price SELL Price(including any taxes.)
                                             $tax_and_sell_price_variant = $sell_price + $product_variants['tax_value'];
                                             if ($price_max['max'] == 0) {
                                                 $price_max['max'] = $tax_and_sell_price_variant;
                                                 $price_max['max_variable_id'] = $variation_product_id;
                                             }
                                             if ($price_max['max'] <= $tax_and_sell_price_variant) {
                                                 $price_max['max'] = $tax_and_sell_price_variant;
                                                 $price_max['max_variable_id'] = $variation_product_id;
                                             }
                                             if ($price_min['min'] == 0) {
                                                 $price_max['min'] = $tax_and_sell_price_variant;
                                                 $price_max['min_variable_id'] = $variation_product_id;
                                             }
                                             if ($price_min['min'] >= $tax_and_sell_price_variant) {
                                                 $price_max['min'] = $tax_and_sell_price_variant;
                                                 $price_max['min_variable_id'] = $variation_product_id;
                                             }
                                             add_post_meta($variation_product_id, '_price', $tax_and_sell_price_variant);
                                             if (get_option('price_field') == 'regular_price') {
                                                 add_post_meta($variation_product_id, '_regular_price', $tax_and_sell_price_variant);
                                             } else {
                                                 add_post_meta($variation_product_id, '_sale_price', $tax_and_sell_price_variant);
                                             }
                                         }
                                     }
                                     #Product Quantity
                                     if (get_option('ps_quantity') == 'on') {
                                         if (isset($outlet_checker_variant) && $outlet_checker_variant == 'noOutlet') {
                                             add_post_meta($variation_product_id, '_manage_stock', 'no');
                                             add_post_meta($variation_product_id, '_stock', NULL);
                                             add_post_meta($variation_product_id, '_stock_status', 'instock');
                                         } else {
                                             add_post_meta($variation_product_id, '_manage_stock', 'yes');
                                             add_post_meta($variation_product_id, '_stock', $variant_quantity);
                                             add_post_meta($variation_product_id, '_stock_status', $variant_quantity > 0 ? 'instock' : 'outofstock');
                                         }
                                         unset($outlet_checker_variant);
                                     } else {
                                         add_post_meta($variation_product_id, '_manage_stock', 'no');
                                     }
                                 }
                                 for ($i = 1; $i <= 3; $i++) {
                                     if (!empty($product_variants['option_' . $option[$i] . '_name'])) {
                                         /*
                                          * check attribute lable Exists or Not tbl->woocommerce_attribute_taxonomies
                                          */
                                         $attribute_name = $this->linksync_check_attribute_label($product_variants['option_' . $option[$i] . '_name']);
                                         $visible = get_option('linksync_visiable_attr');
                                         $thedata['pa_' . $attribute_name] = array('name' => 'pa_' . $attribute_name, 'value' => '', 'is_visible' => $visible, 'is_variation' => '1', 'is_taxonomy' => '1');
                                         /*
                                          * check Term value  Exists or Not tbl->term
                                          */
                                         if (!empty($product_variants['option_' . $option[$i] . '_value'])) {
                                             $term_slug = $this->linksync_check_term_value($product_variants['option_' . $option[$i] . '_value']);
                                             if (isset($term_slug) && !empty($term_slug)) {
                                                 add_post_meta($variation_product_id, "attribute_pa_" . strtolower($attribute_name), strtolower($term_slug['slug']));
                                                 $taxonomy_query = mysql_query("SELECT * FROM `" . $wpdb->prefix . "term_taxonomy` WHERE term_id='" . $term_slug['term_id'] . "' AND taxonomy='pa_" . strtolower($attribute_name) . "'");
                                                 if (mysql_num_rows($taxonomy_query) == 0) {
                                                     if (mysql_query("INSERT INTO `" . $wpdb->prefix . "term_taxonomy` (term_id,taxonomy,parent,description,count) VALUES('" . $term_slug['term_id'] . "','pa_" . strtolower($attribute_name) . "',0,' ',0)")) {
                                                         $taxonomy_id = mysql_insert_id();
                                                         mysql_query("INSERT INTO `" . $wpdb->prefix . "term_relationships`(object_id,term_taxonomy_id,term_order) VALUES('" . $product_ID . "','" . $taxonomy_id . "',0)");
                                                     }
                                                     $query_select = mysql_query("SELECT * FROM `" . $wpdb->prefix . "woocommerce_termmeta` WHERE woocommerce_term_id='" . $term_slug['term_id'] . "'");
                                                     if (0 == mysql_num_rows($query_select)) {
                                                         mysql_query("INSERT INTO `" . $wpdb->prefix . "woocommerce_termmeta`(woocommerce_term_id,meta_key,meta_value) VALUES('" . $term_slug['term_id'] . "','order_pa_" . strtolower($attribute_name) . "',0)");
                                                     }
                                                 } else {
                                                     $taxonomy_data = mysql_fetch_assoc($taxonomy_query);
                                                     mysql_query("INSERT INTO `" . $wpdb->prefix . "term_relationships`(object_id,term_taxonomy_id,term_order) VALUES('" . $product_ID . "','" . $taxonomy_data['term_taxonomy_id'] . "',0)");
                                                     mysql_query("UPDATE `" . $wpdb->prefix . "term_taxonomy` SET count=count+1 WHERE term_id='" . $term_slug['term_id'] . "'");
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                         add_post_meta($product_ID, '_product_attributes', $thedata);
                         //ADD Product Attribute
                         if (isset($price_min)) {
                             add_post_meta($product_ID, '_min_variation_price', $price_min['min']);
                             add_post_meta($product_ID, '_price', $price_min['min']);
                             add_post_meta($product_ID, '_min_price_variation_id', $price_min['min_variable_id']);
                             add_post_meta($product_ID, '_min_variation_regular_price', $price_min['min']);
                             add_post_meta($product_ID, '_min_regular_price_variation_id', $price_min['min_variable_id']);
                             add_post_meta($product_ID, '_min_variation_sale_price', '');
                             add_post_meta($product_ID, '_min_sale_price_variation_id', '');
                         }
                         if (isset($price_max)) {
                             add_post_meta($product_ID, '_max_variation_price', $price_max['max']);
                             add_post_meta($product_ID, '_max_price_variation_id', $price_max['max_variable_id']);
                             add_post_meta($product_ID, '_max_variation_regular_price', $price_max['max']);
                             add_post_meta($product_ID, '_max_regular_price_variation_id', $price_max['max_variable_id']);
                             add_post_meta($product_ID, '_max_variation_sale_price', '');
                             add_post_meta($product_ID, '_max_sale_price_variation_id', '');
                         }
                         // $thedata = array();
                         if ($var_qty <= 0) {
                             if (isset($outlet_checker) && $outlet_checker == 'noOutlet') {
                             } else {
                                 add_post_meta($product_ID, '_stock_status', 'outofstock');
                                 if (get_option('ps_unpublish') == 'on') {
                                     $status = 'draft';
                                 }
                             }
                         } else {
                             add_post_meta($product_ID, '_stock_status', 'instock');
                         }
                     }
                     #----------------------------------------END VARIENT DATA----------------------------------------#
                     #Product Quantity
                     if (get_option('ps_quantity') == 'on') {
                         if (isset($product['variants']) && !empty($product['variants'])) {
                             # if it's variable  product then ignore qty for parent product
                         } else {
                             if (isset($outlet_checker) && $outlet_checker == 'noOutlet') {
                                 add_post_meta($product_ID, '_manage_stock', 'no');
                                 add_post_meta($product_ID, '_stock', NULL);
                                 add_post_meta($product_ID, '_stock_status', 'instock');
                             } else {
                                 add_post_meta($product_ID, '_manage_stock', 'yes');
                                 add_post_meta($product_ID, '_stock', $quantity);
                                 add_post_meta($product_ID, '_stock_status', $quantity > 0 ? 'instock' : 'outofstock');
                                 if (get_option('ps_unpublish') == 'on' && $quantity < 1) {
                                     $status = 'draft';
                                 }
                             }
                         }
                         unset($outlet_checker);
                     } else {
                         add_post_meta($product_ID, '_manage_stock', 'no');
                     }
                     /*
                      * Product Status Dealing
                      */
                     //If the Pending is checked
                     if (get_option('ps_pending') == 'on') {
                         $status = 'pending';
                     }
                     // if product in vend having status : inactive ( active==0  ) it should be not displayed (mark as draft in woo)
                     if ($product['active'] == '0') {
                         $status = 'draft';
                     }
                     $status = isset($status) && !empty($status) ? $status : 'publish';
                     $my_post = array('ID' => $product_ID, 'post_status' => $status);
                     wp_update_post($my_post);
                     if (get_option('ps_unpublish') == 'on') {
                         add_post_meta($product_ID, '_visibility', $status == 'publish' ? 'visible' : '');
                     }
                     unset($status);
                 }
             }
             delete_transient('wc_attribute_taxonomies');
         }
     }
     delete_transient('wc_attribute_taxonomies');
     #Flush attribute
     $prod_update_suc = get_option('prod_update_suc');
     # it has NULL or DATETIME
     if (isset($prod_update_suc) && !empty($prod_update_suc)) {
         linksync_class::add('Product Sync Vend to Woo', 'success', 'Product synced SKU:' . $product['sku'], get_option('linksync_laid'));
     }
     return $product_ids;
 }
 }
 $vend_gallery_image = get_post_meta($product_ID, 'Vend_product_image_gallery', TRUE);
 if (isset($vend_gallery_image) && !empty($vend_gallery_image)) {
     if (strpos($vend_gallery_image, ',')) {
         $gallery_data = explode(',', $vend_gallery_image);
     } else {
         $gallery_data[] = $vend_gallery_image;
     }
     foreach ($gallery_data as $image) {
         if (isset($woo_filename_gallery) && !empty($woo_filename_gallery)) {
             if (!in_array(basename($image), $woo_filename_gallery)) {
                 $attach_ids = linksync_insert_image($image, $product_ID);
                 mysql_query("UPDATE `" . $wpdb->prefix . "postmeta` SET meta_value=CONCAT(meta_value,',{$attach_ids}') WHERE post_id='" . $product_ID . "' AND meta_key='_product_image_gallery'");
             }
         } else {
             $attach_ids = linksync_insert_image($image, $product_ID);
             $imageDb = mysql_num_rows(mysql_query("SELECT * FROM  `" . $wpdb->prefix . "postmeta` WHERE  meta_key='_product_image_gallery' AND `post_id` ='" . $product_ID . "'"));
             if ($imageDb != 0) {
                 mysql_query("UPDATE `" . $wpdb->prefix . "postmeta` SET meta_value=CONCAT(meta_value,',{$attach_ids}') WHERE post_id='" . $product_ID . "' AND meta_key='_product_image_gallery'");
             } else {
                 add_post_meta($product_ID, '_product_image_gallery', $attach_ids);
             }
         }
     }
     unset($gallery_data);
     delete_post_meta($product_ID, 'Vend_product_image_gallery');
     $gallery_response = 'success';
 } else {
     $gallery_response = 'success';
 }
 unset($vend_gallery_image);