<?php
//exit(0);
set_time_limit(0);
ini_set('memory_limit', '10214530 4M');
require_once("includes/config.php");
require_once("helpers/deschelper.php");
require_once("helpers/shopifyGraphQL.php");

require_once("helpers/general.php");
require 'includes/eblib/ebay-sdk-php-autoloader.php';

use \DTS\eBaySDK\Trading\Services;
use \DTS\eBaySDK\Trading\Types;

$logfile = fopen("logs/importToShopify23importgraph_new_new" . date('Y-m-d') . ".txt", "a+") or die("Unable to open log file!");
addlog("Execution Started", "INFO");

$conn = new mysqli($host, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
	addlog("Database connection failed: " . $conn->connect_error, "ERROR");
	die("");
}
mysqli_set_charset($conn, "utf8");

$categoryLogicArr = array(35027, 23749, 19563, 21651, 19477, 21596, 18278, 21221, 18965, 18177, 19321, 19300, 19083, 2119, 18647, 18646, 18593, 16333, 16873, 2961, 1280, 17595, 17422, 11930, 16968, 16997, 17001, 16993, 16969, 16283, 16480, 15434, 16055, 15346, 15410, 15221, 15295, 15153, 15057, 14669, 13821, 13915, 13979, 13884, 13434, 13526, 13233, 13174, 12409, 12343, 11993, 11668, 5736, 2865, 9276, 9431, 9388, 9441, 9332, 9294, 9188, 8442, 7863, 7512, 7682, 7375, 6943, 5925, 113, 490, 997, 1056, 1193, 1447, 2653, 2904, 37236, 46190, 44582, 47617, 49421, 48043, 49161, 48908,50223);

$cronQuery = $conn->query("select * from crons where crontype = 'importtoshopify_graphql'");
$cronrow = $cronQuery->fetch_assoc();
if ($cronrow['isrunning'] == 1) {
	/* $oldlastrun = $cronrow['lastrun'];
	    $datetime1 = new DateTime($oldlastrun);
        $datetime2 = new DateTime();
        $interval = $datetime1->diff($datetime2);
        $hours   = $interval->format('%h'); 
        $minutes = $interval->format('%i');
        $diff =  $hours * 60 + $minutes;
        if($diff > 450){
            @mail("khariwal.rohit@gmail.com", "importtoshopify: Cron already running", "importtoshopify: Cron already running");   
        }*/
	//  @mail("khariwal.rohit@gmail.com", "importtoshopify: Cron already running", "importtoshopify: Cron already running");   
	//die(""); 
}

$conn->query("update crons set lastrun = now(), isrunning = 1 where crontype = 'importtoshopify_graphql'");
$extraQuery = "";
$skipRegx = false;
if (is_array($argv) && isset($argv[2]) && trim($argv[1]) == 'user') {
	$extraQuery = " and user_id = " . trim($argv[2]) . " ";
	if (isset($argv[3]) && trim($argv[3]) == 'skip') {
		$skipRegx = true;
	}
} else {
//	 die("");
}
echo $extraQuery;


//  note : 48018 in not this user have separate file importToShopify_48018
$result = $conn->query("select distinct user_id,count from processimports where   user_id in (select id from users where membershiptype != 'free' ) 	 " . $extraQuery . " and user_id not in (select user_id from importqueue)   and user_id != 46688 and user_id != 50002  and user_id !=48102 and user_id !=48270 and user_id != 48018  and user_id != 49053   and user_id != 49918 limit 0, 10  ");

echo "\n";
echo "select distinct user_id,count from processimports where   user_id in (select id from users where membershiptype != 'free' ) 	 " . $extraQuery . " and user_id not in (select user_id from importqueue)     and user_id !=48102 and user_id !=48270 and user_id != 48018 limit 0, 10  ";
echo "\n";

addlog("Execution Started -1234 ", "INFO");

$totalcount = 0;
echo "12";
addlog("Execution Started -5678 ", "INFO");
$userArr = array();
$biguser = false;
echo "45  " . $result->num_rows;
if ($result->num_rows > 0) {
	echo "fffff";
	while ($row = $result->fetch_assoc()) {
		$user_id = $row['user_id'];
		$array = $row['count'];
		$totalcount = $row['count'];

		if ($array > 1000) {
			$biguser = true;
			echo "delete from importqueue where user_id in (" . implode(",", $userArr) . ")";
			$conn->query("delete from importqueue where user_id in (" . implode(",", $userArr) . ")");
			$userArr = array();
			echo "big user more then 300";
			$conn->query("insert into importqueue (user_id, dateofmodification) values(" . $user_id . ", now())");
			$userArr[] = $user_id;
			break;
		} else {
			echo "less  than 300";
			$conn->query("insert into importqueue (user_id, dateofmodification) values(" . $user_id . ", now())");
			$userArr[] = $user_id;
		}
	}
}
addlog("Execution Started -678888888 ", "INFO");
print_r($userArr);
echo json_encode($biguser) . " is true or false";
//exit(0);
$masterRegExArr = array();
foreach ($userArr as $user_id) {
	addlog("Execution Started -12345666 ", "INFO");
	//	$user_id = $row['user_id'];
	echo $user_id . "--";
	$userresult = $conn->query("select * from users where installationstatus = 1 and id = " . $user_id);
	if ($userresult->num_rows > 0) {
		while ($userrow = $userresult->fetch_assoc()) {
			echo "2";

			addlog("Execution Started -rtrrrrr ", "INFO");
			$shopurl = $userrow['shopurl'];
			$token = $userrow['token'];
			$multi_location_enabled = $userrow['multi_location_enabled'];
			$published = 1;
			$fulfilment_service = 'manual';
			$inventory_policy = 'shopify';
			$includeoutofstock = 0;
			$location_id = "";
			$settingsResult = $conn->query("select * from settings where user_id = " . $user_id);
			if ($settingsResult->num_rows < 1) {
				// Settings not defined for user
				echo "no setting";
				continue;
			}
			$settingsRow = $settingsResult->fetch_assoc();
			$published = $settingsRow['published'];
			$fulfilment_service = $settingsRow['fulfilment_service'];
			$inventory_policy = $settingsRow['inventory_policy'];
			 $location_id = $settingsRow['shopifylocationid'];
			//$location_id = "";
			if ($location_id == "") {
				$location_id = getMainLocation($user_id, $shopurl, $token);
				print_r($location_id);
				if (!$location_id) {
					echo "no location";
					continue;
				}
				//$conn->query("update settings set shopifylocationid = '" . mysqli_real_escape_string($conn, $location_id) . "' where user_id = " . $user_id);
			}
			$existingSKUs = getExistingSKUs($user_id);
			$ebKeysResult = $conn->query("select * from eb_keys where valid = 1 and user_id = " . $user_id);
			if ($ebKeysResult->num_rows < 1) {
				echo "key missing";
				//	continue;
			}
			$ebKeysRow = $ebKeysResult->fetch_assoc();
			$ebtoken = $ebKeysRow['token'];
			$siteid = $ebKeysRow['siteid'];
			echo "hello";

			if ($user_id == 47855) {
				applyOptions47855();
			}
			addlog("Execution Started -12weweeeetrt456 " . $user_id, "INFO");


			processProducts($user_id, $token, $shopurl, $existingSKUs, $includeoutofstock, $published, $fulfilment_service, $inventory_policy, $location_id, $ebtoken, $siteid, $settingsRow, $totalcount);
		}
	}
	$conn->query("delete from processimports where user_id = " . $user_id);
	$conn->query("delete from importqueue where user_id = " . $user_id);
}
$conn->query("update crons set isrunning = 0 where crontype = 'importtoshopify'");
echo "update crons set isrunning = 0 where crontype = 'importtoshopify'";

function applyOptions47855()
{
	global $conn;
	$result = $conn->query("SELECT * FROM `products` WHERE user_id = 47855 and status = 'Import in progress' ");
	if ($result->num_rows > 0) {
		while ($row = $result->fetch_assoc()) {
			$item_specific = $row['item_specific'];
			$condition_val = $row['condition_val'];
			$option1name = "";
			$option2name = "";
			$option3name = "";
			$option1val = "";
			$option2val = "";
			$option3val = "";
			if (strlen($item_specific) > 0) {
				$itemSpecArr = json_decode($item_specific, true);

				if (isset($itemSpecArr['Handedness'])) {
					$option1val = $itemSpecArr['Handedness'];
					$option1name = "Handedness";
				}
				if (isset($itemSpecArr['Club Number'])) {
					$option2val = $itemSpecArr['Club Number'];
					$option2name = "Club Number";
				}
			}
			if ($condition_val) {
				$option3val =  $condition_val;
				$option3name = "Condition";
			}
			$conn->query("update products set option1name = '" . mysqli_real_escape_string($conn, $option1name) . "',option2name = '" . mysqli_real_escape_string($conn, $option2name) . "',option3name = '" . mysqli_real_escape_string($conn, $option3name) . "' where user_id = 47855 and product_id = " . $row['product_id']);
			$conn->query("update product_variants set option1val = '" . mysqli_real_escape_string($conn, $option1val) . "',option2val = '" . mysqli_real_escape_string($conn, $option2val) . "',option3val = '" . mysqli_real_escape_string($conn, $option3val) . "' where user_id = 47855 and product_id = " . $row['product_id']);

			//$conn->query("update products set block = 1 where user_id = 14033 and product_id = ".$row['product_id']);
		}
	}
	return true;
}


function filterTitle16968($title)
{
	$titleArr = array("!", "“", "Top", "Rar", "kg", "Sammlung", "selten", "konvolut");
	foreach ($titleArr as $filter) {
		$title = str_ireplace($filter, "", $title);
	}
	$title = trim($title);
	return $title;
}


function applyRegEx($user_id, $description)
{
	global $conn, $masterRegExArr;
	$regExArr = array();
	if (!array_key_exists($user_id, $masterRegExArr)) {
		$result = $conn->query("select * from regular_expressions where user_id = " . $user_id . " order by created_at asc");
		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {
				$regExArr[] = $row['regex'];
			}
		}
		$masterRegExArr[$user_id] = $regExArr;
	} else {
		$regExArr = $masterRegExArr[$user_id];
	}
	$matched = 0;
	$newdesc = $description;
	foreach ($regExArr as $regex) {
		$matched = 1;
		$res = preg_match_all($regex, $description, $result1);
		if ($res) {
			$matched = 2;
			$newdesc = isset($result1[1][0]) ? trim($result1[1][0]) : "";
			break;
		}
	}
	$newdesc = iconv('windows-1250', 'utf-8', $newdesc);
	$newdesc = str_replace('”', '"', $newdesc);
	$newdesc = str_replace("Â", "", $newdesc);
	return array("matched" => $matched, "newdesc" => $newdesc);
}

function generateTagsFromTitle($title)
{
	$filters = array("&", "/", "-", "+", "|");
	$tags = array();
	$temp = explode(" ", $title);
	$temp = array_map('trim', $temp);
	foreach ($temp as $v) {
		if (!in_array($v, $filters)) {
			$tags[] = $v;
		}
	}
	return $tags;
}

function processProducts($user_id, $token, $shopurl, $existingSKUs, $includeoutofstock, $published, $fulfilment_service, $inventory_policy, $location_id, $ebtoken, $siteid, $settingsRow, $totalcount)
{
	echo $user_id . "--importprocessproducts";
	addlog("import  Started - " . $user_id, "INFO");

	global $conn, $biguser;
	$productQuery;
	echo $biguser;

	addlog("import  Started small user- " . $user_id, "INFO");
	echo $biguser;
	//exit(0);
	if (!$biguser) {
		//echo "all users less then 300 ".json_encode($biguser). " is true or false";
		addlog("import  Started small user- " . $user_id, "INFO");
		$productQuery = "select * from products where title != '' and status = 'Import in progress' and shopifyproductid = '' and  duplicate = 0 and block = 0 and deleted = 0 and user_id = " . $user_id . " limit 0, 400";
	} else {
		addlog("import  Started big user - " . $user_id, "INFO");
		echo "users more the 300 " . json_encode($biguser) . " is true or false";
		$productQuery = "select * from products where title != '' and status = 'Import in progress' and shopifyproductid = '' and  duplicate = 0 and block = 0 and deleted = 0 and user_id = " . $user_id . " limit 0, 8000";
	}

	addlog("import  Started and product found- " . $user_id, "INFO");
	$productResult = $conn->query($productQuery);
	echo  "ALl record" . json_encode($productResult);
	if ($productResult->num_rows > 0) {
		echo $productResult->num_rows;
		while ($productRow = $productResult->fetch_assoc()) {


			$product_id = $productRow['product_id'];
			if ($user_id == 47855 && strpos($productRow['product_type'], 'Golf Clubs') === false) {
				$conn->query("update products set block = 1 where user_id =47855 and product_id = " . $product_id);
				continue;
				// Your code here
			}
			echo "productid-------" . $product_id;
			$option1name = $productRow['option1name'];
			$variantResult = $conn->query("select * from product_variants where product_id = " . $product_id . " and user_id = " . $user_id . " and block = 0 and duplicate = 0 and shopifyvariantid = ''");
			$noOfVariants = $variantResult->num_rows;

			if ($user_id == 48766) {

				if ($option1name == "") {
					echo "in";
					handleProductWithoutVariants($user_id, $token, $shopurl, $existingSKUs, $includeoutofstock, $published, $fulfilment_service, $inventory_policy, $productRow, $location_id, $ebtoken, $siteid, $settingsRow, $totalcount);
				} else {
					echo "in123";
					handleProductsWithVariants($user_id, $token, $shopurl, $existingSKUs, $includeoutofstock, $published, $fulfilment_service, $inventory_policy, $productRow, $location_id, $ebtoken, $siteid, $settingsRow, $totalcount);
				}
			} else {
				if ($noOfVariants == 1) {
					echo "in456";
					handleProductWithoutVariants($user_id, $token, $shopurl, $existingSKUs, $includeoutofstock, $published, $fulfilment_service, $inventory_policy, $productRow, $location_id, $ebtoken, $siteid, $settingsRow, $totalcount);
				} else if ($noOfVariants > 1) {
					echo "567";
					handleProductsWithVariants($user_id, $token, $shopurl, $existingSKUs, $includeoutofstock, $published, $fulfilment_service, $inventory_policy, $productRow, $location_id, $ebtoken, $siteid, $settingsRow, $totalcount);
				} else {
					$conn->query("update products set status = 'reimport in progress' where product_id = " . $product_id . " and user_id = " . $user_id);
					echo "no variant found!";
				}
			}
		}
	} else {

		$conn->query("delete from processimports  where user_id =" . $user_id);
	}
}

function removeDescriptionTemplate($user_id, $description)
{
	$newDescription = $description;
	$pattern = '/<div\s*class="desc-hedtitle">Product\s*Description<\/div>(.*)<\/div>\s*<!--\s*front\s*view\s*tab\s*-->/sm';
	$res1 = preg_match_all($pattern, $description, $result1);
	if ($res1) {
		$newDescription = isset($result1[1][0]) ? trim($result1[1][0]) : "";
	} else {
		$pattern = '/<div\s*class="desc-hedtitle"\s*style=".*">Product\s*Description<\/div>(.*)<\/div>\s*<!--\s*front\s*view\s*tab\s*-->/sm';
		$res1 = preg_match_all($pattern, $description, $result1);
		if ($res1) {
			$newDescription = isset($result1[1][0]) ? trim($result1[1][0]) : "";
		} else {
			$pattern = "/(<div\s*class='description'\s*style='.*'>.*)<div\s*class='speclist'>/sm";
			$res1 = preg_match_all($pattern, $description, $result1);
			if ($res1) {
				$newDescription = isset($result1[1][0]) ? trim($result1[1][0]) : "";
			} else {
				$pattern = '/<div\s*class="desc-hedtitle">Product\s*Description<\/div>(.*)<\/div>\s*<font\s*size="3">\s*<!--\s*front\s*view\s*tab\s*-->/sm';
				$res1 = preg_match_all($pattern, $description, $result1);
				if ($res1) {
					$newDescription = isset($result1[1][0]) ? trim($result1[1][0]) : "";
				} else {
					$pattern = '/<div\s*class="desc-hedtitle"><font\s*face="Arial">Product\s*Description<\/font><\/div>(.*)<\/div>\s*<!--\s*front\s*view\s*tab\s*-->/sm';
					$res1 = preg_match_all($pattern, $description, $result1);
					if ($res1) {
						$newDescription = isset($result1[1][0]) ? trim($result1[1][0]) : "";
					} else {
						$pattern = '/(<div\s*class="description"\s*style="font-family:Arial;font-size:14px;color:#646464;font-weight:normal;">.*<p\s*class="description-item-number">.*)<\/div>\s*<div\s*class="col-lg-4"\s*id="hydraimg">/sm';
						$res1 = preg_match_all($pattern, $description, $result1);
						if ($res1) {
							$newDescription = isset($result1[1][0]) ? trim($result1[1][0]) : "";
						} else {
							return false;
						}
					}
				}
			}
		}
	}
	return $newDescription;
}

function handleProductWithoutVariants($user_id, $token, $shopurl, $existingSKUs, $includeoutofstock, $published, $fulfilment_service, $inventory_policy, $productRow, $location_id, $ebtoken, $siteid, $settingsRow, $totalcount)
{
	echo "handleProductWithoutVariants  count" . $totalcount;
	addlog("handleProductWithoutVariants - ", "INFO");
	global $conn, $skipRegx, $categoryLogicArr;
	$catArr = array();
	if (in_array($user_id, $categoryLogicArr)) {
		$result = $conn->query("select * from storecategories where user_id = " . $user_id);
		addlog("import  Started and product found- " . $user_id, "INFO");
		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {

				$category_id = $row['category_id'];
				$category_name = $row['category_name'];
				$parent_id = $row['parent_id'];
				$catArr[$category_id] = array("category_name" => $category_name, "parent_id" => $parent_id);
			}
		}
	}

	$shippingCostArr = array();

	$product_id = $productRow['product_id'];
	$ebayitemid = $productRow['ebayitemid'];
	echo $ebayitemid;
	$title = $productRow['title'];

	$condition_note = $productRow['condition_note'];
	$condition_val = $productRow['condition_val'];
	$brand = $productRow['brand'];
	$product_type = $productRow['product_type'];
	$item_specific = $productRow['item_specific'];
	$searchstring12 = $productRow['searchstring'];
	$category_idv = $productRow['storecategoryid'];
	$itemSpecificObj = json_decode($item_specific, true);
	$tags = array();
	echo $user_id . " in progress";
	$variantResult = $conn->query("select * from product_variants where product_id = " . $product_id . " and user_id = " . $user_id . " and block = 0 and duplicate = 0 and shopifyvariantid = ''");

	if ($variantResult->num_rows < 1) {
		return true;
	}
	$variantRow = $variantResult->fetch_assoc();

	$variant_id = $variantRow['id'];
	$sku = $variantRow['sku'];
	$price = $variantRow['price'];
	$orig_price = $variantRow['orig_price'];
	$quantity = $variantRow['quantity'];
	$weight = $variantRow['weight'];
	$weight_unit = $variantRow['weight_unit'];
	$productid = $variantRow['productid'];



	if ($user_id == 46190) {

		$tags[] = $searchstring12;
	}
	if ($user_id == 44719 || $user_id == 45165) {
		$tags[] = "eb_" . $searchstring12;
		$tags[] = $ebayitemid;
	}

	if ($user_id == 45000   &&  $searchstring12 != 'UK') {

		$conn->query("update products set block = 1 where user_id = 45000 and ebayitemid =" . $ebayitemid);
		return false;
	}



	if ($user_id == 45738) {
		$sku =  $sku;
	}

	if ($user_id == 46021) {
		$sku =  $sku;
	}

	if (strlen($sku) > 255) {
		$str = wordwrap($sku, 254);
		$str = explode(",", $str);
		$sku = $str[0];
	}


	if ($user_id == 48043) {
		if (is_array($itemSpecificObj)) {
			foreach ($itemSpecificObj as $k => $v) {
				if ($k == "MPN" || $k == "Manufacturer Part Number") {
					$sku = $v;
					break;
				}
			}
		}
	}



	if ($user_id == 46674) {
		if (is_array($itemSpecificObj)) {
			foreach ($itemSpecificObj as $k => $v) {
				if ($k == "MPN" || $k == "Manufacturer Part Number") {
					$sku = $v;
					break;
				}
			}
		}
	}

	if ($user_id == 19443) {
		$price = applyPriceMarkupSettingsRow19443($price, $settingsRow);
	} else {
		$price = applyPriceMarkupSettingsRow($price, $settingsRow);
	}


	if ($user_id == 47617) {
		//GBP TO EURO
		$price = $variantRow['orig_price'];
		if ($price > 0) {
			$price = $price * 1.20;
		} else {
			$price = $variantRow['price'];
			$price = $price * 1.20;
		}
		//$price = round($price);
	}


	if ($user_id == 44582  || $user_id == 49161 || $user_id == 44143  ) {
		$shippingcost = 0;

		$tags = array();

		echo "select * from shippingcost where user_id = " . $user_id . " and product_id=" . $product_id;
		$result = $conn->query("select * from shippingcost where user_id = " . $user_id . " and product_id=" . $product_id);
		if ($result->num_rows > 0) {
			while ($shippingCost = $result->fetch_assoc()) {
				$shippingcost = $shippingCost['cost'];
				$tags[] = "Ship_" . $shippingcost;
			}
		}

		// 

		echo "     valuee tagess        " . "\n";
		print_r($tags);
	}
	if($user_id == 48747){
		$shippingcost = 0;

		$tags = array();

		echo "select * from shippingcost where user_id = " . $user_id . " and product_id=" . $product_id;
		$result = $conn->query("select * from shippingcost where user_id = " . $user_id . "  and cost in (9.95, 14.95, 19.95, 24.95, 29.95, 39.95, 49.95, 74.95, 99.95, 124.95, 149.95, 199.95, 249.95, 274.95, 299.95) and product_id=" . $product_id);
		if ($result->num_rows > 0) {
			while ($shippingCost = $result->fetch_assoc()) {
				$shippingcost = $shippingCost['cost'];
				$tags[] = $shippingcost;
			}
		}
		else{

			$conn->query("UPDATE products SET block = 1 WHERE user_id = " . $user_id . " AND product_id = " . $product_id);

		   return false;
		}
	}
	if ($user_id == 48747) {
		$title = $productRow['title'];
		$tags[] ="Ebay";
		// Corvette models
		$corvetteModels = ["C3 Corvette", "C4 Corvette", "C5 Corvette", "C6 Corvette", "C7 Corvette"];
		foreach ($corvetteModels as $model) {
			if (strpos($title, $model) !== false) {
				$tags[] = $model;
			}
		}

		// General tags
		$keywords = [
			"Corvette",
			"GTO",
			"G8",
			"SS Sedan",
			"SSR",
			"XLR",
			"Camaro",
			"Firebird",
			"Mustang",
			"Suburban",
			"Blazer",
			"Truck",
			"Thunderbird",
			"CTS, CTS-V",
			"Charger",
			"Challenger",
			"H2",
			"NOS"
		];

		foreach ($keywords as $keyword) {
			if (strpos($title, $keyword) !== false) {
				$tags[] = $keyword;
			}
		}
	}

	if ($user_id == 44582 || $user_id == 49161) {

		$temp = explode(":", $product_type);
		$product_type = trim(end($temp));
		$tags[] = $product_type;

		print_r($tags);
	}

	if ($user_id == 47582) {



		$temp = explode(":", $product_type);

		$segment1 = $temp[0];
		$tags[] = $segment1;
		$segment2 = $temp[1];
		$tags[] = $segment2;
		$segment3 = $temp[2];
		$tags[] = $segment3;
		$segment4 = $temp[3];
		$tags[] = $segment4;
		//}

	}



	if ($user_id == 44582 || $user_id == 49161) {
		$storecategoryid = $productRow['storecategoryid'];

		$product_type = "";
		if (array_key_exists($storecategoryid, $catArr)) {
			$temp = $catArr[$storecategoryid];
			$category_name = $temp['category_name'];
			$parent_id = $temp['parent_id'];
			$tags[] = $category_name;
			//$product_type = $category_name;
			if ($parent_id != "") {
				if (array_key_exists($parent_id, $catArr)) {
					$temp1 = $catArr[$parent_id];
					$category_name1 = $temp1['category_name'];
					$tags[] = $category_name1;
					//$product_type = $category_name1;
				}
			}
		}
	}



	print_r($tags);



	if ($user_id == 44841 && $weight_unit == 'lb') {
		$weight = $weight * 0.453592;
		$weight_unit = "kg";
	}



	if (strlen($title) > 255) {
		$str = wordwrap($title, 254);
		$str = explode("\n", $str);
		$title = $str[0];
	}

	$description = "";
	if ($user_id != 16702) {
		$description = getProdDescription($user_id, $product_id);
	}
	if ($description == "") {
		$description = getProdDescriptionNew($user_id, $ebayitemid, $ebtoken, $siteid);
	}

	$is_item_specific = false;
	$is_condition_note = false;
	$is_condition_val =  false;
	if ($settingsRow["desc_cond "] == 1) {
		$is_condition_note = true;
		$is_condition_val =  true;
	}
	if ($settingsRow["desc_itemspecs"] == 1) {
		$is_item_specific = true;
	}
	//error can be

	echo "description start";

	print_r($description);
	echo "anjali" . json_encode($is_item_specific, true);
	//sleep(3);
	$descFilterResp = applyDescriptionFilter($description, $item_specific, $condition_note, $condition_val, $is_item_specific, $is_condition_note, $is_condition_val, $user_id);
	print_r($descFilterResp);
	if ($descFilterResp["status"] == "fail") {
		echo "error may be here " . $user_id;
		if (!$skipRegx) {

			echo "hgghhghghghghghghghghghg";
			return false;
		}
	} else {
		$description = $descFilterResp["message"];
	}
	if (strlen($description) == 0) {
		$description = $title;
	}

	print_r("annntags " . json_encode($tag));

	if ($user_id == 43580) {
		$result = $conn->query("select * from storecategories where category_id ='" . $category_idv . "' and user_id = " . $user_id);
		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {
				$tags = $row['category_name'];
				// $shopifycategoryid = $row['shopifycategoryid'];
				// $catArr[$category_id] = $shopifycategoryid;
			}
		}
	}

	if ($user_id == 44401) {
		$result = $conn->query("select * from storecategories where category_id ='" . $category_idv . "' and user_id = " . $user_id);
		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {
				$tags[] = $row['category_name'];
				// $shopifycategoryid = $row['shopifycategoryid'];
				// $catArr[$category_id] = $shopifycategoryid;
			}
		}
	}


	if ($user_id == 46926) {
		$temp = explode(":", $product_type);
		$product_type = trim(end($temp));
	}

	// if($user_id == 48908){
	// 	$pTypeArr = explode(":", $product_type);
	// 	foreach($pTypeArr as $pType){
	// 	    $tags[] = trim($pType);
	// 	}   

	//  }

	if ($user_id == 48908) {
		foreach ($itemSpecificObj as $k => $v) {
			$tags[] = $v;
		}
		
	}


	//46926

	if ($user_id == 44893) {
		$temp = explode(":", $product_type);
		$product_type = end($temp);
		$tags[] = $product_type;
		echo $product_type . "                        ";
	}



	if (strlen($sku) > 255) {
		$str = wordwrap($sku, 254);
		$str = explode("\n", $str);
		$sku = $str[0];
	}

	echo "17346 in progress imags";
	$imageArr = array();
	$position = 1;
	$imageResult = $conn->query("select * from product_images where user_id = " . $user_id . " and variant_id = " . $variant_id);
	if ($imageResult->num_rows > 0) {
		while ($imageRow = $imageResult->fetch_assoc()) {
			$imgurl = $imageRow['imgurl'];
			if ($user_id != 15715) {
				$imgurl = str_replace("_12.JPG", "_57.JPG", $imgurl);
				$imgurl = str_replace("_1.JPG", "_57.JPG", $imgurl);
				$imgurl = str_replace("_0.JPG", "_57.JPG", $imgurl);
				$imgurl = str_replace("_6.JPG", "_57.JPG", $imgurl);
			} else {
				if (strpos($imgurl, 'ebayimg') !== false) {
					$imgurl = str_replace("_12.JPG", "_57.JPG", $imgurl);
					$imgurl = str_replace("_1.JPG", "_57.JPG", $imgurl);
					$imgurl = str_replace("_0.JPG", "_57.JPG", $imgurl);
					$imgurl = str_replace("_6.JPG", "_57.JPG", $imgurl);
				}
			}
			if ($user_id == 38128 || $user_id == 50223 || $user_id == 50338) {
				$imgurl = str_replace("_12.PNG", "_57.PNG", $imgurl);
				$imgurl = str_replace("_1.PNG", "_57.PNG", $imgurl);
				$imgurl = str_replace("_0.PNG", "_57.PNG", $imgurl);
				$imgurl = str_replace("_6.PNG", "_57.PNG", $imgurl);
			}

			if ($user_id == 43059) {
				$imageArr[] = array("src" => trim($imgurl), "position" => $position++, "width" => 1200, "height" => 1600);

				print_r($imageArr);

				//exit(0);
			} else {
				print_r($imageArr);
				$imageArr[] = array("src" => trim($imgurl), "position" => $position++);
			}
		}
	}


	if (in_array($user_id, $categoryLogicArr)) {
		$storecategoryid = $productRow['storecategoryid'];
		$storecategoryid2 = $productRow['storecategoryid2'];
		if (array_key_exists($storecategoryid, $catArr)) {
			$temp = $catArr[$storecategoryid];
			$category_name = $temp['category_name'];
			$parent_id = $temp['parent_id'];
			$tags[] = $category_name;
			$product_type = $category_name;
			if ($parent_id != "") {
				if (array_key_exists($parent_id, $catArr)) {
					$temp1 = $catArr[$parent_id];
					$category_name1 = $temp1['category_name'];
					$parent_id = $temp1['parent_id'];
					$tags[] = $category_name1;
					if ($user_id != 19321 && $user_id != 19563) {
						$product_type = $category_name1;
					}
					if ($parent_id != "") {
						if (array_key_exists($parent_id, $catArr)) {
							$temp2 = $catArr[$parent_id];
							$category_name2 = $temp2['category_name'];
							$tags[] = $category_name2;
							if ($user_id != 19321 && $user_id != 19563) {
								$product_type = $category_name2;
							}
						}
					}
				}
			}
		}
		if (array_key_exists($storecategoryid2, $catArr)) {
			$temp = $catArr[$storecategoryid2];
			$category_name = $temp['category_name'];
			$parent_id = $temp['parent_id'];
			$tags[] = $category_name;
			if ($parent_id != "") {
				if (array_key_exists($parent_id, $catArr)) {
					$temp1 = $catArr[$parent_id];
					$category_name1 = $temp1['category_name'];
					$tags[] = $category_name1;
				}
			}
		}
		if ($user_id == 15057) {
			if ($storecategoryid == "27421515014") { // LAMBSKIN LEATHER
				$tags[] = "retail_lambskins";
			} else if ($storecategoryid == "27421516014") { // GOATSKIN LEATHER
				$tags[] = "retail_goatskins";
			} else if ($storecategoryid == "27421517014") { // METALLIC LEATHER
				$tags[] = "retail_metallics";
			} else if ($storecategoryid == "1095742014") { // CALF/COW LEATHER
				$tags[] = "retail_calf/cow";
			} else {
				$conn->query("update products set block = 1, status = 'Ready to Import' where user_id = 15057 and product_id = " . $productRow['product_id']);
				return false;
			}
		}
	}



	if ($user_id == 45000) {

		$item_specific = $productRow['item_specific'];
		if (strlen($item_specific) > 0) {
			$jsonObj = json_decode($item_specific, true);

			if (isset($jsonObj['Size'])) {
				$tags[] = trim($jsonObj['Size']);
			}
		}
	}




	if ($user_id == 46026  &&   $condition_val == "New other (see details)") {
		$tags[] = "take off";
	}

	$comparePrice = 0;





	// Add settings from DB
	$dbtags = trim($settingsRow['tags']);
	$dbvendor = trim($settingsRow['vendor']);
	$dbproduct_type = trim($settingsRow['product_type']);
	$dbtaxable = $settingsRow['taxable'];
	if ($dbvendor != "") {
		if ($user_id == 18969) {
			echo "1st vendor $dbvendor";
			if ($dbvendor == "HG Garage" && $productRow['brand'] == 'KTM') {
				$brand = "KTM";
				echo "anjali from db with HGgrage $brand";
			} else {
				$brand = $dbvendor;
				echo "anjali from db without HGgrage $brand";
			}
		} else {
			$brand = $dbvendor;
			echo "anjali other  $brand";
		}
	}
	if ($dbproduct_type != "") {
		$product_type = $dbproduct_type;
	}
	if ($dbtags != "") {
		$tempTags = explode(",", $dbtags);
		foreach ($tempTags as $tempTag) {
			$tags[] = trim($tempTag);
		}
	}

	$taxable = true;
	if ($dbtaxable == 0) {
		$taxable = false;
	}

	$pubstatus = "active";
	if ($published == 0) {
		$pubstatus = "draft";
	}
	if (strlen($sku) > 255) {
		$str = wordwrap($sku, 254);
		$str = explode(",", $str);
		$sku = $str[0];
	}


	if ($user_id == 41563) {
		//die();

		$p_category_name = "";
		$product_id = $productRow['product_id'];

		echo "select category_name from storecategories where shopifycategoryid != '' and user_id = '" . $user_id . "'  and category_id in (select storecategoryid from products where user_id= '" . $user_id . "'  and  product_id = '" . $product_id . "')";

		$result = $conn->query("select category_name,category_id from storecategories where shopifycategoryid != '' and user_id = '" . $user_id . "'  and category_id in (select storecategoryid from products where user_id= '" . $user_id . "'  and  product_id = '" . $product_id . "') ");
		if ($result->num_rows > 0) {


			// while (
			$row = $result->fetch_assoc();
			//) {
			$child_category_name = $row['category_name'];
			$child_category_id  = $row['category_id'];

			echo "hkjhhghkhkjhkjh" . $child_category_id;

			$tags[] = $child_category_name;



			$newcategory = $conn->query("select * from storecategories where user_id = 41563");

			//echo "select * from storecategories where user_id = 41563";

			if ($newcategory->num_rows > 0) {


				while ($row = $newcategory->fetch_assoc()) {

					$category_id = $row['category_id'];


					$category_name = $row['category_name'];
					$parent_id = $row['parent_id'];
					$catArr[$category_id] = array("category_name" => $category_name, "parent_id" => $parent_id);
				}

				foreach ($catArr as $categoryId => $categoryData) {
					if ($categoryData['category_name'] == $child_category_name && $categoryId == $child_category_id) {
						$parent_id = $categoryData['parent_id'];
						break;
					}
				}
				if ($parent_id != ' ') {

					foreach ($catArr as $categoryId => $categoryData) {
						if ($categoryId == $parent_id) {
							$p_category_name = $categoryData['category_name'];
							break;
						}
					}
				}

				echo "check ";
				if ($p_category_name != ' ') {
					echo "herrrrrr isss        ";
					echo "Parent id  " . $parent_id;

					echo "<br></br>";
					echo "                   ";

					echo "Parent Category name " . $p_category_name;

					$tags[] = $p_category_name;
				} else {
					echo "Category not found";
				}


				echo "here is tags ";
				print_r($tags);
			}
		}
	}

	if ($user_id == 49421) {
		//die();

		$p_category_name = "";
		$product_id = $productRow['product_id'];

		echo "select category_name from storecategories where shopifycategoryid != '' and user_id = '" . $user_id . "'  and category_id in (select storecategoryid from products where user_id= '" . $user_id . "'  and  product_id = '" . $product_id . "')";

		$result = $conn->query("select category_name,category_id from storecategories where shopifycategoryid != '' and user_id = '" . $user_id . "'  and category_id in (select storecategoryid from products where user_id= '" . $user_id . "'  and  product_id = '" . $product_id . "') ");
		if ($result->num_rows > 0) {


			// while (
			$row = $result->fetch_assoc();
			//) {
			$child_category_name = $row['category_name'];
			$child_category_id  = $row['category_id'];

			echo "hkjhhghkhkjhkjh" . $child_category_id;

			$tags[] = $child_category_name;



			$newcategory = $conn->query("select * from storecategories where user_id = 49421");

			//echo "select * from storecategories where user_id = 49421";

			if ($newcategory->num_rows > 0) {


				while ($row = $newcategory->fetch_assoc()) {

					$category_id = $row['category_id'];


					$category_name = $row['category_name'];
					$parent_id = $row['parent_id'];
					$catArr[$category_id] = array("category_name" => $category_name, "parent_id" => $parent_id);
				}

				foreach ($catArr as $categoryId => $categoryData) {
					if ($categoryData['category_name'] == $child_category_name && $categoryId == $child_category_id) {
						$parent_id = $categoryData['parent_id'];
						break;
					}
				}
				if ($parent_id != ' ') {

					foreach ($catArr as $categoryId => $categoryData) {
						if ($categoryId == $parent_id) {
							$p_category_name = $categoryData['category_name'];
							break;
						}
					}
				}

				if ($p_category_name != ' ') {

					echo "herrrrrr isss        ";

					echo "Parent id  " . $parent_id;

					echo "<br></br>";
					echo "                   ";

					echo "Parent Category name " . $p_category_name;

					$tags[] = $p_category_name;
				} else {
					echo "Category not found";
				}


				echo "here is tags ";
				print_r($tags);
			}
		}
	}



	if ($user_id == 50223) {
		//die();

		$p_category_name = "";
		$product_id = $productRow['product_id'];

		echo "select category_name from storecategories where shopifycategoryid != '' and user_id = '" . $user_id . "'  and category_id in (select storecategoryid from products where user_id= '" . $user_id . "'  and  product_id = '" . $product_id . "')";

		$result = $conn->query("select category_name,category_id from storecategories where shopifycategoryid != '' and user_id = '" . $user_id . "'  and category_id in (select storecategoryid from products where user_id= '" . $user_id . "'  and  product_id = '" . $product_id . "') ");
		if ($result->num_rows > 0) {


			// while (
			$row = $result->fetch_assoc();
			//) {
			$child_category_name = $row['category_name'];
			$child_category_id  = $row['category_id'];

			echo "hkjhhghkhkjhkjh" . $child_category_id;

			$tags[] = $child_category_name;



			$newcategory = $conn->query("select * from storecategories where user_id = 50223");

			//echo "select * from storecategories where user_id = 49421";

			if ($newcategory->num_rows > 0) {


				while ($row = $newcategory->fetch_assoc()) {

					$category_id = $row['category_id'];


					$category_name = $row['category_name'];
					$parent_id = $row['parent_id'];
					$catArr[$category_id] = array("category_name" => $category_name, "parent_id" => $parent_id);
				}

				foreach ($catArr as $categoryId => $categoryData) {
					if ($categoryData['category_name'] == $child_category_name && $categoryId == $child_category_id) {
						$parent_id = $categoryData['parent_id'];
						break;
					}
				}
				if ($parent_id != ' ') {

					foreach ($catArr as $categoryId => $categoryData) {
						if ($categoryId == $parent_id) {
							$p_category_name = $categoryData['category_name'];
							break;
						}
					}
				}

				if ($p_category_name != ' ') {

					echo "herrrrrr isss        ";

					echo "Parent id  " . $parent_id;

					echo "<br></br>";
					echo "                   ";

					echo "Parent Category name " . $p_category_name;

					$tags[] = $p_category_name;
				} else {
					echo "Category not found";
				}


				echo "here is tags ";
				print_r($tags);
			}
		}
	}




	if ($user_id == 47617) {
		//die();

		$p_category_name = "";
		$product_id = $productRow['product_id'];

		echo "select category_name from storecategories where shopifycategoryid != '' and user_id = '" . $user_id . "'  and category_id in (select storecategoryid from products where user_id= '" . $user_id . "'  and  product_id = '" . $product_id . "')";

		$result = $conn->query("select category_name,category_id from storecategories where shopifycategoryid != '' and user_id = '" . $user_id . "'  and category_id in (select storecategoryid from products where user_id= '" . $user_id . "'  and  product_id = '" . $product_id . "') ");
		if ($result->num_rows > 0) {


			// while (
			$row = $result->fetch_assoc();
			//) {
			$child_category_name = $row['category_name'];
			$child_category_id  = $row['category_id'];

			echo "hkjhhghkhkjhkjh" . $child_category_id;

			$tags[] = $child_category_name;



			$newcategory = $conn->query("select * from storecategories where user_id = 47617");

			//echo "select * from storecategories where user_id = 41563";

			if ($newcategory->num_rows > 0) {


				while ($row = $newcategory->fetch_assoc()) {

					$category_id = $row['category_id'];


					$category_name = $row['category_name'];
					$parent_id = $row['parent_id'];
					$catArr[$category_id] = array("category_name" => $category_name, "parent_id" => $parent_id);
				}

				foreach ($catArr as $categoryId => $categoryData) {
					if ($categoryData['category_name'] == $child_category_name && $categoryId == $child_category_id) {
						$parent_id = $categoryData['parent_id'];
						break;
					}
				}
				if ($parent_id != ' ') {

					foreach ($catArr as $categoryId => $categoryData) {
						if ($categoryId == $parent_id) {
							$p_category_name = $categoryData['category_name'];
							break;
						}
					}
				}

				if ($p_category_name != ' ') {

					echo "herrrrrr isss        ";

					echo "Parent id  " . $parent_id;

					echo "<br></br>";
					echo "                   ";

					echo "Parent Category name " . $p_category_name;

					$tags[] = $p_category_name;
				} else {
					echo "Category not found";
				}


				echo "here is tags ";
				print_r($tags);

				//print_r($catArr);

				//exit(0);

			}

			// TODO: find child_category_name in catArr with child_category_nameand find parent_id
			//find pparent_id in catArr and get category_name and add in tag

			// 




			//}		



			// echo ".>>>>>soosmayyyrjhkjhkjhgkjh";
			// print_r($tags);




		}
	}








	if ($user_id == 44893) {
		//die();

		$p_category_name = "";
		$product_id = $productRow['product_id'];

		echo "select category_name from storecategories where shopifycategoryid = '' and user_id = '" . $user_id . "'  and category_id in (select storecategoryid from products where user_id= '" . $user_id . "'  and  product_id = '" . $product_id . "')";

		$result = $conn->query("select category_name,category_id from storecategories where shopifycategoryid = '' and user_id = '" . $user_id . "'  and category_id in (select storecategoryid from products where user_id= '" . $user_id . "'  and  product_id = '" . $product_id . "') ");
		if ($result->num_rows > 0) {


			// while (
			$row = $result->fetch_assoc();
			//) {
			$child_category_name = $row['category_name'];
			$child_category_id  = $row['category_id'];

			echo "hkjhhghkhkjhkjh" . $child_category_id;

			$tags[] = $child_category_name;



			$newcategory = $conn->query("select * from storecategories where user_id = 44893");

			//echo "select * from storecategories where user_id = 44893";

			if ($newcategory->num_rows > 0) {


				while ($row = $newcategory->fetch_assoc()) {

					$category_id = $row['category_id'];


					$category_name = $row['category_name'];
					$parent_id = $row['parent_id'];
					$catArr[$category_id] = array("category_name" => $category_name, "parent_id" => $parent_id);
				}

				foreach ($catArr as $categoryId => $categoryData) {
					if ($categoryData['category_name'] == $child_category_name && $categoryId == $child_category_id) {
						$parent_id = $categoryData['parent_id'];
						break;
					}
				}
				if ($parent_id != ' ') {

					foreach ($catArr as $categoryId => $categoryData) {
						if ($categoryId == $parent_id) {
							$p_category_name = $categoryData['category_name'];
							break;
						}
					}
				}

				if ($p_category_name != ' ') {

					echo "herrrrrr isss        ";

					echo "Parent id  " . $parent_id;

					echo "<br></br>";
					echo "                   ";

					echo "Parent Category name " . $p_category_name;

					$tags[] = $p_category_name;
				} else {
					echo "Category not found";
				}


				echo "here is tags ";
				print_r($tags);

				//print_r($catArr);

				//exit(0);

			}
		}
	}

	if ($user_id == 46190) {
		//die();

		$p_category_name = "";
		$product_id = $productRow['product_id'];

		echo "select category_name from storecategories where shopifycategoryid = '' and user_id = '" . $user_id . "'  and category_id in (select storecategoryid from products where user_id= '" . $user_id . "'  and  product_id = '" . $product_id . "')";

		$result = $conn->query("select category_name,category_id from storecategories where shopifycategoryid = '' and user_id = '" . $user_id . "'  and category_id in (select storecategoryid from products where user_id= '" . $user_id . "'  and  product_id = '" . $product_id . "') ");
		if ($result->num_rows > 0) {


			// while (
			$row = $result->fetch_assoc();
			//) {
			$child_category_name = $row['category_name'];
			$child_category_id  = $row['category_id'];

			echo "hkjhhghkhkjhkjh" . $child_category_id;

			$tags[] = $child_category_name;



			$newcategory = $conn->query("select * from storecategories where user_id = 46190");

			//echo "select * from storecategories where user_id = 46190";

			if ($newcategory->num_rows > 0) {


				while ($row = $newcategory->fetch_assoc()) {

					$category_id = $row['category_id'];


					$category_name = $row['category_name'];
					$parent_id = $row['parent_id'];
					$catArr[$category_id] = array("category_name" => $category_name, "parent_id" => $parent_id);
				}

				foreach ($catArr as $categoryId => $categoryData) {
					if ($categoryData['category_name'] == $child_category_name && $categoryId == $child_category_id) {
						$parent_id = $categoryData['parent_id'];
						break;
					}
				}
				if ($parent_id != ' ') {

					foreach ($catArr as $categoryId => $categoryData) {
						if ($categoryId == $parent_id) {
							$p_category_name = $categoryData['category_name'];
							break;
						}
					}
				}

				if ($p_category_name != ' ') {

					echo "herrrrrr isss        ";

					echo "Parent id  " . $parent_id;

					echo "<br></br>";
					echo "                   ";

					echo "Parent Category name " . $p_category_name;

					$tags[] = $p_category_name;
				} else {
					echo "Category not found";
				}


				echo "here is tags ";
				print_r($tags);

				//print_r($catArr);

				//exit(0);

			}



			// 




			//}		



			// echo ".>>>>>soosmayyyrjhkjhkjhgkjh";
			// print_r($tags);




		}
	}



	if ($user_id == 43398) {
		$tags = explode(":", $product_type);
		$product_type = end($tags);
	}

	if ($user_id == 45000) {
		$productid = $sku;
	}
	if ($user_id == 44841) {
		$productid = "";
		$taxable = false;
	}
	if ($user_id == 46127) {

		$description = "";
	}

	if ($user_id == 46021) {
		$tags = array();
		if (strpos($title, 'Mens') !== false) {
			$tags[] = "gender_mens";
		}
		if (strpos($title, 'Womens') !== false) {

			$tags[] = "gender_womens";
		}
	}
	// if($user_id == 46021){
	// 	$tags[] =array();
	// }
	if ($user_id == 46668) {

		$sku = $ebayitemid;
	}
	if ($user_id == 46910) {

		$tags[] = $sku;
	}

	if ($user_id == 47309) {
		$description = "";
		$item_specific = $productRow['item_specific'];
		$condition_val = $productRow['condition_val'];
		$description = setdescriptiontemplate47309($item_specific, $title, $condition_val);
	}

	if ($user_id == 47855) {
		if (isset($itemSpecificObj['Brand'])) {
			$brand = $itemSpecificObj['Brand'];
		}
		if (isset($itemSpecificObj['Golf Club Type'])) {
			$product_type = $itemSpecificObj['Golf Club Type'];
		}
		if (isset($itemSpecificObj['Flex'])) {
			$tags[] = $itemSpecificObj['Flex'];
		}
	}
	if ($sku  == " ") {
		$sku = $ebayitemid;
	}
	if ($user_id  == 48662) {
		$taxable = true;
	}
	print_r("iamge" . $imageArr);
	if ($user_id == 47855) {
		$sku = $variantRow['sku'];
	}
	if ($user_id == 48805) {
		$weight = $weight * 0.453592;
		$weight_unit = "kg";			
		$itemid = $productRow['ebayitemid'];
        $description = $description."<p style=\"word-wrap: break-word\"><strong>Item ID:</strong> $itemid</p>";
	
	}
	$data = array(
		"product" => array(
			"title" => $title,
			"body_html" => $description,
			"vendor" => $brand,
			"product_type" => $product_type,
			"status" => strtoupper($pubstatus),
			"tags" => $tags,
			"published_scope" => "global",
			"images" => $imageArr,
			"variants" => array(
				array(
					"sku" => $sku,
					"position" => 1,
					"price" => number_format($price, 2, '.', ''),
					"inventory_policy" => "deny",
					"fulfillment_service" => $fulfilment_service,
					"inventory_management" => $inventory_policy,
					"taxable" => $taxable,
					/*"inventory_quantity"=>$quantity,*/
					"weight" => $weight,
					"weight_unit" => $weight_unit,
					"barcode" => $productid,
					"requires_shipping" => true,
					"quantity" => $quantity
				)
			)
		)
	);
	if (count($imageArr) === 1) {
		$data["product"]["variants"][0]["imgurl"] = $imageArr[0]['src'];
	}
	print_r($data);

	if ($user_id == 46926) {

		$metafieldsvv = array();
		$conditionarray = array();
		$conditionarray[0] =  "Pre-owned";
		$metafieldsvv[] = array("key" => "condition_", "value" => json_encode($conditionarray), "type" => "list.single_line_text_field", "namespace" => "custom");
		$data["product"]["metafields"] = $metafieldsvv;
	}
   if ($user_id == 48805) {

		$metafieldsvv = array();
		
		$metafieldsvv[] = array("key" => "ebay_item_id", "value" => "$ebayitemid", "type" => "single_line_text_field", "namespace" => "custom");
		$data["product"]["metafields"] = $metafieldsvv;
	}
	if ($user_id == 47617) {
		$price = $variantRow['orig_price'];
		if ($price > 0) {
			$price = $price;
		} else {
			$price = $variantRow['price'];
		}
		$metafieldsvv = array();
		$metafieldsvv[] = array("key" => "ebay_item_price", "value" =>  $price, "type" => "single_line_text_field", "namespace" => "custom");
		$data["product"]["variants"][0]["metafields"] = $metafieldsvv;
	}
	  if ($user_id == 48805) {

		$metafieldsvv = array();
		
		$metafieldsvv[] = array("key" => "ebay_item_id", "value" => "$ebayitemid", "type" => "single_line_text_field", "namespace" => "custom");
		$data["product"]["metafields"] = $metafieldsvv;
	}
	if ($user_id == 44495) {
		$product_type = $productRow['product_type'];
		if (strpos($product_type, 'Clothes') === false) {
			$data["product"]["variants"][0]["requires_shipping"] = false;
		}
	}

	if ($sku  == " ") {
		$sku = $ebayitemid;
	}
	if ($user_id == 46454) {
		if ($brand == 'Oliver People' || $brand == 'Saint Laurent') {
			$comparePrice = $price * 0.35;
			$singleVariant["compare_at_price"] = number_format($comparePrice, 2, '.', '');
		} else if ($brand == 'PUMA') {
			$comparePrice = $price * 0.60;
			$singleVariant["compare_at_price"] = number_format($comparePrice, 2, '.', '');
		}
	}

	if ($user_id == 43398) {
		if ($orig_price > $price) {
			$data["product"]["variants"][0]["compare_at_price"] = number_format($orig_price, 2, '.', '');
		}
	}
    if($user_id == 48908){
		$metafields = array();
		$item_specific = $productRow['item_specific'];
		$itemSpecificJson = json_decode($item_specific, true);
		foreach ($itemSpecificJson as $k => $v) {
			print_r($k);

			if ($k == "Allergene") {
				$key = "informazioni_sugli_allergeni";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" =>"single_line_text_field", "namespace" => "custom");
			}
			if ($k == "Paese di fabbricazione") {
				$key = "country";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" =>"single_line_text_field", "namespace" => "custom");
			}
			if ($k == "Attributi speciali") {
				$key = "esigenze_alimentari";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" =>"single_line_text_field", "namespace" => "custom");
			}
			if ($k == "Area Geografica") {
				$key = "area_geografica";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" =>"single_line_text_field", "namespace" => "custom");
			}
			if ($k == "Stile") {
				$key = "dolcezza_del_vino";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" =>"single_line_text_field", "namespace" => "custom");
			}
			if ($k == "Tipo") {
				$key = "variet_di_vino";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" =>"single_line_text_field", "namespace" => "custom");
			}
		}
		$data["product"]["metafields"] = $metafields;
		print_r($data["product"]);
	}
	if ($user_id == 46021) {

		$metafields = array();

		$item_specific = $productRow['item_specific'];
		print_r("here are the metafileds " . $item_specific);
		// exit(0);
		$itemSpecificJson = json_decode($item_specific, true);
		foreach ($itemSpecificJson as $k => $v) {
			print_r($k);

			if ($k == "Dress Length") {
				$key = "dress_length";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Theme") {
				$key = "theme";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Fit") {
				$key = "fit";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Rise") {
				$key = "rise";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Waist Size") {
				$key = "waist_size";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Colour") {
				$key = "colour";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Brand") {
				$key = "brand_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Material") {
				$key = "material_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Closure") {
				$key = "closure_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Condition") {
				$key = "condition_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $condition_val, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Department") {
				$key = "department_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Style") {
				$key = "style";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Type") {
				$key = "type_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Size Type") {
				$key = "size_type_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Size") {
				$key = "size_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Sleeve Length") {
				$key = "sleeve_length_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Fabric Type") {
				$key = "fabric_type";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Pattern") {
				$key = "pattern_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Neckline") {
				$key = "neckline_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Graphic Print") {
				$key = "graphic_print_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Front Type") {
				$key = "front_type";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Chest size") {
				$key = "chest_size_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Pocket Type") {
				$key = "pocket_type";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Strap Type") {
				$key = "strap_type";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Swim Bottom Style") {
				$key = "swim_bottom_style";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Garment Care") {
				$key = "garment_care";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Accent") {
				$key = "accent";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Character") {
				$key = "character";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Season") {
				$key = "season";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Skirt Length") {
				$key = "skirt_length";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Handle/Strap Colour") {
				$key = "handle_strap_colour";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Handle Style") {
				$key = "handle_style";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Exterior Material") {
				$key = "exterior_material";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Exterior Colour") {
				$key = "exterior_colour";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Bag Width") {
				$key = "bag_width";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Bag Heigh") {
				$key = "bag_heigh";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Upper Material") {
				$key = "upper_material";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "UK Shoe Size") {
				$key = "uk_shoe_size";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Toe Shape") {
				$key = "toe_shape";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Outsole Material") {
				$key = "outsole_material";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Outer Shell Material") {
				$key = "outer_shell_material";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Lining Material") {
				$key = "lining_material";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Jacket/Coat Length") {
				$key = "jacket_coat_length";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Leg Style") {
				$key = "leg_style";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Inside Leg") {
				$key = "inside_leg";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Distressed") {
				$key = "distressed";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Other Material") {
				$key = "other_material";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Collar To Hem") {
				$key = "collar_to_hem_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
		}
		$data["product"]["metafields"] = $metafields;
		print_r($data["product"]);
	}
    if ($user_id == 50251) {
        $metafields = array();
		$tags =array();
		$condition_val = $productRow['condition_val'];
		$item_specific = $productRow['item_specific'];
		$title  =$productRow['title'];
		$res = mapItemAttributes($title,$tags,$metafields); // fixed: you don’t need to pass tags/metafields in
        $tags       = array_merge($tags, $res['tags']);
        $metafields = array_merge($metafields, $res['metafields']);
		$storecategoryid  =$productRow['storecategoryid'];
       
		$item_specificObj = json_decode($item_specific, true);
		$productType       = $productRow['product_type'];
		$parts = array_map('trim', explode(':', $productType));
   
        
        $partGroup = isset($parts[2]) ? 'PartGroup_' . str_replace([' ', ','], '_', $parts[2]) : '';

        $partSubGroup = isset($parts[3]) ? 'PartSubGroup_' . str_replace([' ', ','], '_', $parts[3]) : '';

        if ($partGroup)    $tags[] = $partGroup;
        if ($partSubGroup) $tags[] = $partSubGroup;
		$metafields[] = array(  
                "namespace" => "custom",
				"key"  =>"condition",
				"type" =>"single_line_text_field",
				"value" =>"$condition_val"
               );
	     foreach ($item_specificObj as $key => $value) {
            
            $clean_key = str_replace(' ', '', $key);      
            $clean_value=str_replace(' ', '', $value);
                          
           
            if($key == "Brand" ){
				 $tags[] = $clean_key . '_' . $clean_value;
               $metafields[] = array(  
                "namespace" => "custom",
				"key"  =>"make",
				"type" =>"single_line_text_field",
				"value" =>"$value"
               );
            }
            if($key == "Model"){
				 $tags[] = $clean_key . '_' . $clean_value;
               $metafields[] = array(  
                "namespace" => "custom",
				"key"  =>"model",
				"type" =>"single_line_text_field",
				"value" =>"$value"
               );
            }
            if($key == "Year"){
				 $tags[] = $clean_key . '_' . $clean_value;
               $metafields[] = array(  
                "namespace" => "custom",
				"key"  =>"year",
				"type" =>"list.single_line_text_field",
				"value" =>json_encode(array($value))
               );
            }
            if($key == "Part"){
               $metafields[] = array(  
                "namespace" => "custom",
				"key"  =>"part",
				"type" =>"single_line_text_field",
				"value" =>"$value"
               );
            }
            else{
                $metafields[] = array(  
                "namespace" => "custom",
				"key"  =>"part",
				"type" =>"single_line_text_field",
				"value" =>"$partnew"
               );
            }
            if($key == "Warranty Percentage Values"){
               $metafields[] = array(  
                "namespace" => "custom",
				"key"  =>"warranty_percentage_values",
				"type" =>"json",
				"value" =>json_encode(array($value))
               );
            }
            else{
                $metafields[] = array(  
                "namespace" => "custom",
				"key"  =>"warranty_percentage_values",
				"type" =>"json",
				"value" =>json_encode(array("Not APPLICABLE"))
               );
            }
            if($key == "Make logo"){
               $metafields[] = array(  
                "namespace" => "custom",
				"key"  =>"make_logo",
				"type" =>"file_reference",
				"value" =>"$value"
               );
            }
            
            
        }
		  $storecategorysss = $conn->query("
                    SELECT * 
                    FROM storecategories 
                    WHERE user_id = 50251 
                      AND category_id = " . (int)$storecategoryid
                );
                $catrow = $storecategorysss->fetch_assoc();

                if (!empty($catrow['parent_id'])) {
                    $childcategory = $catrow['category_name'];
                    $catid  = "PartType_" . str_replace(' ', '_', $childcategory);

                    $parentresults = $conn->query("
                        SELECT * 
                        FROM storecategories 
                        WHERE user_id = 50251 
                          AND category_id = " . (int)$catrow['parent_id']
                    );
                    $parentRow = $parentresults->fetch_assoc();

                    $parentCategory = $parentRow['category_name'];
                    $partid = "PartCategory_" . str_replace(' ', '_', $parentCategory);

                    $tags[] = $catid;
                    $tags[] = $partid;

                    if ($parentCategory) {
                        $metafields[] = [
                            "namespace" => "custom",
                            "key"       => "part_category",
                            "type"      => "single_line_text_field",
                            "value"     => $parentCategory
                        ];
                    }

                    if ($childcategory) {
                        $metafields[] = [
                            "namespace" => "custom",
                            "key"       => "part",
                            "type"      => "single_line_text_field",
                            "value"     => $childcategory
                        ];
                    }
                }
      $data["product"]["metafields"] = $metafields;
	  $data["product"]["tags"] = $tags;


	}

	if ($user_id == 46026) {
		$metafields = array();
		$condition_note = $productRow['condition_note'];
		$condition_val = $productRow['condition_val'];
		if ($condition_val != '') {
			$metafields[] = array("key" => "condition", "value" => $condition_val, "type" => "single_line_text_field", "namespace" => "my_fields");
		}
		if ($condition_note != '') {
			$metafields[] = array("key" => "condition_description", "value" => $condition_note, "type" => "single_line_text_field", "namespace" => "my_fields");
		}
		$data["product"]["metafields"] = $metafields;
	}
    


	if ($user_id == 44664 || $user_id == 43040) {
		$metafields = array();

		$result = $conn->query("select * from product_dimensions where  user_id = " . $user_id . " and ebayitemid = " . $productRow['ebayitemid'] . " limit 1");

		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {
				echo  $productRow['ebayitemid'];
				echo  $shopifyproductid;

				$len = $row['length'];
				$width = $row['width'];
				$depth = $row['depth'];

				echo "length $len";
				echo "width $width";
				echo "depth $depth";

				$metafields[] = array("key" => "length", "value" => $len, "type" => "single_line_text_field", "namespace" => "custom");
				$metafields[] = array("key" => "width", "value" => $width, "type" => "single_line_text_field", "namespace" => "custom");
				if ($user_id == 43040) {
					$metafields[] = array("key" => "depth", "value" => $depth, "type" => "single_line_text_field", "namespace" => "custom");
				} else {
					$metafields[] = array("key" => "height", "value" => $depth, "type" => "single_line_text_field", "namespace" => "custom");
				}
				echo "set meta field ";
			}
			$data["product"]["metafields"] = $metafields;
		}
	}

	if ($user_id == 47379) {

		$metafields = array();

		$result = $conn->query("select * from product_dimensions where  user_id = " . $user_id . " and ebayitemid = " . $productRow['ebayitemid'] . " limit 1");

		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {
				echo  $productRow['ebayitemid'];
				echo  $shopifyproductid;

				$len = $row['length'];
				$width = $row['width'];
				$depth = $row['depth'];

				echo "length $len";
				echo "width $width";
				echo "depth $depth";

				$metafields[] = array("key" => "length", "value" => $len, "type" => "single_line_text_field", "namespace" => "custom");
				$metafields[] = array("key" => "width", "value" => $width, "type" => "single_line_text_field", "namespace" => "custom");
				$metafields[] = array("key" => "height", "value" => $depth, "type" => "single_line_text_field", "namespace" => "custom");

				echo "set meta field ";
			}

			$metafields[] = array("key" => "Weight", "value" => $weight, "type" => "single_line_text_field", "namespace" => "custom");
			$data["product"]["metafields"] = $metafields;
		}
	}






	if ($user_id == 44401) {

		$metafields[] = array("key" => "listing_condition", "value" => $condition_val, "type" => "single_line_text_field", "namespace" => "custom");
		$data["product"]["metafields"] = $metafields;
	}

	if (($user_id == 41578 || $user_id == 43633 || $user_id == 42894 || $user_id == 44890) && $inventory_policy == "NO") {
		$data['product']['variants'][0]['inventory_management'] = "";
	}
	if ($user_id == 45000) {
		$productid = $sku;
	}
	if ($user_id == 48043) {
		$data['product']['variants'][0]["inventory_policy"] = "continue";
	}


	if ($user_id == 44719 || $user_id == 45165) {
		$data['product']['variants'][0]['barcode'] = $ebayitemid;
	}

	if ($user_id == 46021) {
		if (is_array($itemSpecificObj)) {
			foreach ($itemSpecificObj as $k => $v) {
				if ($k == "EAN") {
					$data['product']['variants'][0]['barcode']  = $v;
					break;
				}
			}
		}
	}

	if ($user_id == 48102) {

		$metafields = array();
		$metafields[] = array("key" => "snowboard_length", "value" => "{\"value\":1.0,\"unit\":\"mm\"}", "type" => "dimension", "namespace" => "test_data");
		$data["product"]["metafields"] = $metafields;
	}

if ($user_id == 50223) {
    $conditionStrnew = "";
    $decodedDescription = json_decode( $productRow['description'], true);

    // Check if decoded successfully and is an array
    if (json_last_error() === JSON_ERROR_NONE && is_array($decodedDescription)) {
        print_r($decodedDescription); // Optional for debugging

        // Check if 'Manufacturer' key exists and is an array
        if (!empty($decodedDescription['Manufacturer']) && is_array($decodedDescription['Manufacturer'])) {
            $manufacturer = $decodedDescription['Manufacturer'];
            $conditionStrnew = "<br><h2>Informationen zur Produktsicherheit</h2>";

            if (!empty($manufacturer['CompanyName'])) {
                $conditionStrnew .= "<p><strong>CompanyName:</strong> " . htmlspecialchars($manufacturer['CompanyName']) . "</p>";
            }
            if (!empty($manufacturer['Street1'])) {
                $conditionStrnew .= "<p><strong>Street1:</strong> " . htmlspecialchars($manufacturer['Street1']) . "</p>";
            }
            if (!empty($manufacturer['Street2'])) {
                $conditionStrnew .= "<p><strong>Street2:</strong> " . htmlspecialchars($manufacturer['Street2']) . "</p>";
            }
            if (!empty($manufacturer['CityName'])) {
                $conditionStrnew .= "<p><strong>CityName:</strong> " . htmlspecialchars($manufacturer['CityName']) . "</p>";
            }
            if (!empty($manufacturer['PostalCode'])) {
                $conditionStrnew .= "<p><strong>PostalCode:</strong> " . htmlspecialchars($manufacturer['PostalCode']) . "</p>";
            }
            if (!empty($manufacturer['Country'])) {
                $conditionStrnew .= "<p><strong>Country:</strong> " . htmlspecialchars($manufacturer['Country']) . "</p>";
            }
            if (!empty($manufacturer['Email'])) {
                $conditionStrnew .= "<p><strong>Email:</strong> " . htmlspecialchars($manufacturer['Email']) . "</p>";
            }

            // Append to body_html
            $data["product"]['body_html'] .= $conditionStrnew;
        }
    }
}


	if ($user_id == 47617) { //

		$metafields = array();
		$item_specific = $productRow['item_specific'];
		$ebayitemid =  $productRow['ebayitemid'];
		$searchstring12 = $productRow['searchstring'];
		echo $searchstring12 . "\n";
		$category =    $productRow['product_type'];
		$condition_val = $productRow['condition_val'];
		$condition_note = $productRow['condition_note'];
		$title = $productRow['title'];
		$itemSpecificJson = json_decode($item_specific, true);

		if (isset($itemSpecificJson['Non-Domestic Product'])) {
			$NDP = $itemSpecificJson['Non-Domestic Product'];
			$metafields[] = array("key" => "ebay_non_domestic_product", "value" => $NDP, "type" => "single_line_text_field", "namespace" => "custom");
		}


		if (isset($itemSpecificJson['Brand'])) {
			$brand = $itemSpecificJson['Brand'];
			$metafields[] = array("key" => "ebay_brand", "value" => $brand, "type" => "single_line_text_field", "namespace" => "custom");
		}


		if (isset($itemSpecificJson['Type'])) {
			$type = $itemSpecificJson['Type'];
			$metafields[] = array("key" => "ebay_type", "value" => $type, "type" => "single_line_text_field", "namespace" => "custom");
		}

		if (isset($itemSpecificJson['Custom Bundle'])) {
			$Custom_Bundle = $itemSpecificJson['Custom Bundle'];
			$metafields[] = array("key" => "ebay_custom_bundle", "value" => $Custom_Bundle, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson['Modified Item'])) {
			$modified_item = $itemSpecificJson['Modified Item'];
			$metafields[] = array("key" => "ebay_modified_item", "value" => $modified_item, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson['Battery Size'])) {
			$battery_size = $itemSpecificJson['Battery Size'];
			$metafields[] = array("key" => "ebay_battery_size", "value" => $battery_size, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson['MPN'])) {
			$mpn = $itemSpecificJson['MPN'];
			$metafields[] = array("key" => "ebay_mpn", "value" => $mpn, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson['Voltage'])) {
			$voltage = $itemSpecificJson['Voltage'];
			$metafields[] = array("key" => "ebay_voltage", "value" => $voltage, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson['EAN'])) {
			$ean = $itemSpecificJson['EAN'];
			$metafields[] = array("key" => "ebay_ean", "value" => $ean, "type" => "single_line_text_field", "namespace" => "custom");
		}

		if (isset($itemSpecificJson['Chemical Composition'])) {
			$chemical_composition = $itemSpecificJson['Chemical Composition'];
			$metafields[] = array("key" => "ebay_chemical_composition", "value" => $chemical_composition, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson['Expiry Date'])) {
			$expiry_date = $itemSpecificJson['Expiry Date'];
			$metafields[] = array("key" => "ebay_expiry_date", "value" => $expiry_date, "type" => "single_line_text_field", "namespace" => "custom");
		}

		if (isset($itemSpecificJson['Coin\/Button Cell Sub-Type'])) {
			$coin_button_cell_sub_type = $itemSpecificJson['Coin\/Button Cell Sub-Type'];
			$metafields[] = array("key" => "ebay_coin_button_cell_sub_type", "value" => $coin_button_cell_sub_type, "type" => "single_line_text_field", "namespace" => "custom");
		}

		if (isset($searchstring12)) {

			$metafields[] = array("key" => "ebay_shipping_profile_name", "value" => $searchstring12, "type" => "single_line_text_field", "namespace" => "custom");
		}

		if (isset($ebayitemid)) {
			$ebay_link = 'https://www.ebay.co.uk/itm/' . $ebayitemid;
			$metafields[] = array("key" => "ebay_original_ebay_link", "value" => $ebay_link, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($condition_note)) {

			$metafields[] = array("key" => "ebay_condition", "value" =>  $condition_note, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($condition_val)) {

			$metafields[] = array("key" => "ebay_condition", "value" => $condition_val, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Ideale Per:"])) {
			$Ideale = $itemSpecificJson["Ideale Per:"];
			$metafields[] = array("key" => "ebay_ideale_per", "value" => $Ideale, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Carte Opzione"])) {
			$Carte = $itemSpecificJson["Carte Opzione"];
			$metafields[] = array("key" => "ebay_carte_opzione", "value" => $Carte, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Marca"])) {
			$Marca = $itemSpecificJson["Marca"];
			$metafields[] = array("key" => "ebay_marca", "value" => $Marca, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Tipo."])) {
			$Tipo = $itemSpecificJson["Tipo."];
			$metafields[] = array("key" => "ebay_tipo", "value" => $Tipo, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Paese\/Regione di Produzione"])) {
			$Paese = $itemSpecificJson["Paese\/Regione di Produzione"];
			$metafields[] = array("key" => "ebay_paese_regione_di_produzione", "value" => $Paese, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Number of Piece"])) {
			$Piece11 = $itemSpecificJson["Number of Piece"];
			$metafields[] = array("key" => "ebay_number_of_piecese", "value" => $Piece11, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Material"])) {
			$Material = $itemSpecificJson["Material"];
			$metafields[] = array("key" => "ebay_material", "value" => $Material, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Age Level"])) {
			$Level = $itemSpecificJson["Age Level"];
			$metafields[] = array("key" => "ebay_age_level", "value" => $Level, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Personalisation Instructions"])) {
			$Instructions = $itemSpecificJson["Personalisation Instructions"];
			$metafields[] = array("key" => "ebay_personalise", "value" => $Instructions, "type" => "single_line_text_field", "namespace" => "custom");
		}

		if (isset($itemSpecificJson["Colour"])) {
			$Colour = $itemSpecificJson["Colour"];
			$metafields[] = array("key" => "ebay_colour", "value" => $Colour, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Award"])) {
			$Award = $itemSpecificJson["Award"];
			$metafields[] = array("key" => "ebay_award", "value" => $Award, "type" => "single_line_text_field", "namespace" => "custom");
		}

		if (isset($itemSpecificJson["Theme"])) {
			$Theme = $itemSpecificJson["Theme"];
			$metafields[] = array("key" => "ebay_theme", "value" => $Theme, "type" => "single_line_text_field", "namespace" => "custom");
		}

		if (isset($itemSpecificJson["Features"])) {
			$Features = $itemSpecificJson["Features"];
			$metafields[] = array("key" => "ebay_features", "value" => $Features, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Recommended Age Range"])) {
			$Range = $itemSpecificJson["Recommended Age Range"];
			$metafields[] = array("key" => "ebay_recommended_age_range", "value" => $Range, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Country\/Region of Manufacture"])) {
			$Country = $itemSpecificJson["Country\/Region of Manufacture"];
			$metafields[] = array("key" => "ebay_country_region_of_manufacture", "value" => $Country, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Character Family"])) {
			$Family = $itemSpecificJson["Character Family"];
			$metafields[] = array("key" => "ebay_character_family", "value" => $Family, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Personalise"])) {
			$Personalise = $itemSpecificJson["Personalise"];
			$metafields[] = array("key" => "ebay_personalise", "value" => $Personalise, "type" => "single_line_text_field", "namespace" => "custom");
		}


		$data["product"]["metafields"] = $metafields;
		print_r($data);
		//exit(0);
	}


	$imageArr_new = array();
	foreach ($data["product"]['images'] as $v) {
		$image_url = preg_replace('/\?.*/', '', $v['src']);
		$imageArr_new[] = array("alt" => "$product_id", "mediaContentType" => "IMAGE", "originalSource" => trim($image_url));
	}
	echo " user is  " . $user_id . " count  " . $array;
	//print_r($data);
	if ($user_id == 49053 && empty($imageArr_new)) {
		 $imageArr_new[] = [ "alt" => "epi0", "mediaContentType" => "IMAGE", "originalSource" => "https://i.ebayimg.com/images/g/DOcAAOSw8NplLtwK/s-l500.webp" ];
	 }

	echo "total hsgksfhg" . $totalcount;
	addlog("productcreate request" . json_encode($data, true), "INFO");
	
	$jsonstring ="";
	if(count($imageArr) === 1 ){
		$jsonstring = [
			"input" => [
				"title" => $data["product"]['title'],
				'descriptionHtml' => $data["product"]['body_html'],
				'vendor' => $data["product"]['vendor'],
				"tags" => $data["product"]['tags'],
				"status" => strtoupper($data["product"]['status']),
				'productType' => $data["product"]['product_type'],
				'published' => true,
			]
		];
	}else{
	$jsonstring = [
		"input" => [
			"title" => $data["product"]['title'],
			'descriptionHtml' => $data["product"]['body_html'],
			'vendor' => $data["product"]['vendor'],
			"tags" => $data["product"]['tags'],
			"status" => strtoupper($data["product"]['status']),
			'productType' => $data["product"]['product_type'],
			'published' => true,
		],
		"media" => $imageArr_new
	];
    }
	if (isset($data["product"]['metafields'])) {
		$jsonstring["input"]['metafields'] = $data["product"]['metafields'];
	}
	print_r($jsonstring);
	$response = addShopifyProduct_graphql($token, $shopurl, $jsonstring);
	sleep(1);
	$responseDecoded = $response;
	addlog("check response addShopifyProduct: ", "INFO");
	addlog("productcreate response" . json_encode($response, true), "INFO");
	print_r($response);
	if (empty($responseDecoded['data']['productCreate']['userErrors'])) {
		echo 'in if for updated variant';
		print_r($responseDecoded['data']['productCreate']['product']['id']);
		$gid_shopifyproductid = $responseDecoded['data']['productCreate']['product']['id'];
		print_r($shopifyproductidG);
		$shopifyproductid = str_replace("gid://shopify/Product/", "", $gid_shopifyproductid);
		// if($shopifyproductid !== ''){
		$shopifyvariantidG = $responseDecoded['data']['productCreate']['product']['variants']['nodes'][0]['id'];
		$shopifyvariantid = str_replace("gid://shopify/ProductVariant/", "", $shopifyvariantidG);
		$shopifyinventoryidG = $responseDecoded['data']['productCreate']['product']['variants']['nodes'][0]['inventoryItem']['id'];
		$shopifyinventoryid = str_replace("gid://shopify/InventoryItem/", "", $shopifyinventoryidG);
		$location_id=$responseDecoded['data']['productCreate']['product']['variants']['nodes'][0]['inventoryItem']['inventoryLevels']['nodes'][0]['location']['id'];
		$location_id=str_replace("gid://shopify/Location/", "", $location_id);
		echo "LOCATION ID IS  ".$location_id." \n";
		$conn->query("update products set shopifyproductid = '" . mysqli_real_escape_string($conn, $shopifyproductid) . "', status = 'Imported', newflag = 0, updated_at = now() where user_id = '" . $user_id . "' and product_id = " . $product_id);
		$conn->query("update product_variants set shopifyproductid = '" . mysqli_real_escape_string($conn, $shopifyproductid) . "', shopifyvariantid = '" . mysqli_real_escape_string($conn, $shopifyvariantid) . "', shopifyinventoryid = '" . mysqli_real_escape_string($conn, $shopifyinventoryid) . "', shopifylocationid = '" . mysqli_real_escape_string($conn, $location_id) . "', status = 'Imported', newflag = 0, updated_at = now() where user_id = '" . $user_id . "' and id = " . $variant_id);
		$conn->query("insert into shopifyproducts(user_id, productid, variantid, sku, dateofmodification) values ('" . $user_id . "', '" . mysqli_real_escape_string($conn, $shopifyproductid) . "', '" . mysqli_real_escape_string($conn, $shopifyvariantid) . "', '" . mysqli_real_escape_string($conn, $sku) . "', now())");
		addlog("productid " . $shopifyproductid, "INFO");
		$weight_unitnew = $data['product']['variants'][0]['weight_unit'];
		if (isset($weight_unitnew) && $weight > 0) {
			$weight_unitnew = convertWeightUnitToFullForm($weight_unitnew);
		} else {
			$weight = 0;
			$weight_unitnew = "POUNDS";
		}
		if ($user_id == 49161) {
			$weight_unitnew = "POUNDS";
		}
		if ($user_id == 48805) {

			$weight_unitnew = "KILOGRAMS";
		}
		$track =  true;
		if ($data['product']['variants'][0]['inventory_policy'] == "continue") {
			$track = false;
		}
		$variantVariables = '';
		if ($data['product']['variants'][0]['imgurl'] != '') {
			$image_url = preg_replace('/\?.*/', '', $data['product']['variants'][0]['imgurl']);
			$variantVariables = [
				"productId" => "gid://shopify/Product/$shopifyproductid",
				"media" => [
					"alt" => "epi" . $data['product']['variants'][0]['barcode'],
					"mediaContentType" => "IMAGE",
					"originalSource" =>  $image_url,
				],
				"variants" => [
					[
						"id" => "gid://shopify/ProductVariant/$shopifyvariantid",

						"barcode" => $data['product']['variants'][0]['barcode'],
						"inventoryItem" => [
							"measurement" => [
								"weight" => [
									"unit" =>  $weight_unitnew,
									"value" => (float)$data['product']['variants'][0]['weight'],
								],
							],
							"requiresShipping" => true,
							"sku" => $data['product']['variants'][0]['sku'],
							"tracked" => true,
						],
						"inventoryPolicy" => strtoupper($data['product']['variants'][0]['inventory_policy']),
						"price" => $data['product']['variants'][0]['price'],
						"taxable" =>  $data['product']['variants'][0]['taxable'],
						"mediaSrc" => $image_url,
					],
				],
			];
		} else {
			$variantVariables = [
				"productId" => "gid://shopify/Product/$shopifyproductid",

				"variants" => [
					[
						"id" => "gid://shopify/ProductVariant/$shopifyvariantid",

						"barcode" => $data['product']['variants'][0]['barcode'],
						"inventoryItem" => [
							"measurement" => [
								"weight" => [
									"unit" =>  $weight_unitnew,
									"value" => (float)$data['product']['variants'][0]['weight'],
								],
							],
							"requiresShipping" => true,
							"sku" => $data['product']['variants'][0]['sku'],
							"tracked" => true,
						],
						"inventoryPolicy" => strtoupper($data['product']['variants'][0]['inventory_policy']),
						"price" => $data['product']['variants'][0]['price'],
						"taxable" =>  $data['product']['variants'][0]['taxable'],

					],
				],
			];
		}
		addlog("processto uplocat v data" . json_encode($variantVariables, true), "INFO");

		$res = updateShopifyVariant_graphql($token, $shopurl, $variantVariables);
		sleep(1);
		print_r($res);
		addlog("processto uplocat v data default response" . json_encode($res, true), "INFO");
		if ($res) {
			addlog("updateShopifyVariant id true ", "INFO");
			$variables = [
				"input" => [
					"ignoreCompareQuantity" => true,
					"name" => "available",
					"reason" => "correction",
					"quantities" => [
						[
							"inventoryItemId" =>  "gid://shopify/InventoryItem/$shopifyinventoryid",
							"locationId" => "gid://shopify/Location/$location_id",
							"quantity" => (int)$data['product']['variants'][0]['quantity']
						]
					]
				]
			];
			addlog("updateShopifyInventory_graphql ", "INFO");
			addlog("updateinve data" . json_encode($variables, true), "INFO");
			sleep(2);
			$USI = updateShopifyInventory_graphql($token, $shopurl, $variables);
			sleep(1);
			print_r($USI);
			if ($USI) {
				addlog("processto uplocat v data default response inventory" . json_encode($USI, true), "INFO");
				addlog("product imported successfully :" . $product_id, "INFO");
				echo "product imported successfully :" . $product_id;
				$totalcount = $totalcount - 1;
				if ($totalcount == 0) {
					$conn->query("delete from processimports  where user_id =" . $user_id);
				} else {

					$conn->query("update processimports set count ='" . $totalcount . "' where user_id =" . $user_id . "");
				}
				$totalcount--;
			}
		}

		if ($user_id == 48043) {

			$variables = [
                  "id" => $gid_shopifyproductid,
                  "input" => [
                   [ "publicationId" =>  "gid://shopify/Publication/117633089651" ],
                   [ "publicationId" => "gid://shopify/Publication/117633187955" ],
                   [ "publicationId" => "gid://shopify/Publication/118559309939" ]
            
                  ],
                ];
           
                publishedonallchannelnew($user_id, $token, $shopurl, $gid_shopifyproductid, $variables);
			// $allchannel = array("gid://shopify/Publication/117633089651", "gid://shopify/Publication/117633187955", " gid://shopify/Publication/118559309939");
			// foreach ($allchannel  as $published) {

			// 	publishedonallchannel($user_id, $token, $shopurl, $gid_shopifyproductid, $published);
			// }
		}
		if ($user_id == 46190) {

			 $variables = [
                  "id" => $gid_shopifyproductid,
                  "input" => [
                   [ "publicationId" =>  "gid://shopify/Publication/107722571984" ],
                   [ "publicationId" => "gid://shopify/Publication/107722539216" ],
                   [ "publicationId" => "gid://shopify/Publication/107722473680" ]
                  ],
                ];
           
                publishedonallchannelnew($user_id, $token, $shopurl, $gid_shopifyproductid, $variables);
			// $allchannel = array("gid://shopify/Publication/107722571984", "gid://shopify/Publication/107722539216", "gid://shopify/Publication/107722473680");
			// foreach ($allchannel  as $published) {

			// 	publishedonallchannel($user_id, $token, $shopurl, $gid_shopifyproductid, $published);
			// }
		}
	// }else{
	// 	echo 'shopify productid is empty come';
	// 	$conn->query("update products set status = 'error',  errdetails = 'shopify productid is empty', updated_at = now() where user_id = '" . $user_id . "' and product_id = " . $product_id);
	
	// }
	}else {
		$conn->query("update products set status = 'error', updated_at = now() where user_id = '" . $user_id . "' and product_id = " . $product_id);
	}

	return true;
}
function convertWeightUnitToFullForm($abbreviation)
{
	$units = [
		'kg' => 'KILOGRAMS',
		'lb' => 'POUNDS',
		'g' => 'GRAMS',
		'oz' => 'OUNCES'
	];

	$abbreviation = strtolower((string)$abbreviation);

	return $units[$abbreviation] ?? '';
}
function handleProductsWithVariants($user_id, $token, $shopurl, $existingSKUs, $includeoutofstock, $published, $fulfilment_service, $inventory_policy, $productRow, $location_id, $ebtoken, $siteid, $settingsRow, $totalcount)
{
	global $conn, $skipRegx, $categoryLogicArr;
	$catArr = array();
	echo "total count" . $totalcount;
	if (in_array($user_id, $categoryLogicArr)) {
		$result = $conn->query("select * from storecategories where user_id = " . $user_id);
		addlog("import  Started and product found- " . $user_id, "INFO");
		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {
				$category_id = $row['category_id'];
				$category_name = $row['category_name'];
				$parent_id = $row['parent_id'];
				$catArr[$category_id] = array("category_name" => $category_name, "parent_id" => $parent_id);
			}
		}
	}

	$shippingCostArr = array();

	$product_id = $productRow['product_id'];
	$ebayitemid = $productRow['ebayitemid'];
	$title = $productRow['title'];
	$condition_note = $productRow['condition_note'];
	$condition_val = $productRow['condition_val'];
	$brand = $productRow['brand'];
	$category_idv = $productRow['storecategoryid'];
	$product_type = $productRow['product_type'];
	$item_specific = $productRow['item_specific'];
	$searchstring12 = $productRow['searchstring'];
	$itemSpecificObj = json_decode($item_specific, true);
	$option1name = $productRow['option1name'];
	$option2name = $productRow['option2name'];
	$option3name = $productRow['option3name'];
	$option4name = $productRow['option4name'];
	$option5name = $productRow['option5name'];
	$tags = array();

	$option1name = str_replace("/", "-", $option1name);
	$option2name = str_replace("/", "-", $option2name);
	$option3name = str_replace("/", "-", $option3name);
	$option4name = str_replace("/", "-", $option4name);
	$option5name = str_replace("/", "-", $option5name);

	$isSizeType = false;


	if ($user_id == 46190) {

		$tags[] = $searchstring12;
	}
	if ($user_id == 44719 || $user_id == 45165) {
		$tags[] = "eb_" . $searchstring12;
		$tags[] = $ebayitemid;
	}

	if ($user_id == 41867) {
		$option4name = "";
		$option5name = "";
	}

	if ($user_id == 45556) {
		$option4name = "";
		$option5name = "";
	}

	if ($user_id == 41867) {
		$option4name = "";
		$option5name = "";
	}
	if ($user_id == 43398 || $user_id == 48533) {
		$option4name = "";
		$option5name = "";
	}


	$weight = 0;
	$weight_unit = "lb";
	if (strlen($option4name) > 0 || strlen($option5name) > 0) {
		$conn->query("update products set status = 'error', errdetails = 'More than 3 variant options.' where product_id = " . $product_id . " and user_id = " . $user_id);
		echo "more than three variant options.";
		return false;
	}


	if ($user_id == 45000   &&  $searchstring12 != 'UK') {

		$conn->query("update products set block = 1 where user_id = 45000 and ebayitemid =" . $ebayitemid);
		return false;
	}

	$description = "";
	if ($user_id != 18645) {
		$description = getProdDescription($user_id, $product_id);
	}
	if ($description == "") {
		$description = getProdDescriptionNew($user_id, $ebayitemid, $ebtoken, $siteid);
	}
	$is_item_specific = false;
	$is_condition_note = false;
	$is_condition_val =  false;
	if ($settingsRow["desc_cond "] == 1) {
		$is_condition_note = true;
		$is_condition_val =  true;
	}
	if ($settingsRow["desc_itemspecs"] == 1) {
		$is_item_specific = true;
	}

	print_r($is_item_specific);
	echo "  gggggggggggggggggggggggg          ";

	$descFilterResp = applyDescriptionFilter($description, $item_specific, $condition_note, $condition_val, $is_item_specific, $is_condition_note, $is_condition_val, $user_id);
	print_r($descFilterResp);
	if ($descFilterResp["status"] == "fail") {
		if (!$skipRegx) {
			return false;
		}
	} else {
		$description = $descFilterResp["message"];
	}

	if (strlen($title) > 255) {
		$str = wordwrap($title, 254);
		$str = explode("\n", $str);
		$title = $str[0];
	}
	if (strlen($description) == 0) {
		$description = $title;
	}


	if ($user_id == 46926) {
		$temp = explode(":", $product_type);
		$product_type = trim(end($temp));
	}

	// if($user_id == 48908){
	// 	$pTypeArr = explode(":", $product_type);
	// 	foreach($pTypeArr as $pType){
	// 	    $tags[] = trim($pType);
	// 	}   

	//  }

	if ($user_id == 48908) {
		foreach ($itemSpecificObj as $k => $v) {
			$tags[] = $v;
		}
	}


	if ($user_id == 44582 || $user_id == 49161) {

		$temp = explode(":", $product_type);
		$product_type = trim(end($temp));
		$tags[] = $product_type;

		print_r($tags);
	}


	if ($user_id == 47582) {

		// $temp = explode(":", $product_type);
		// $product_type = trim(end($temp));
		// 	$tags[] =$product_type ;

		// 	print_r($tags);


		$temp = explode(":", $product_type);

		$segment1 = $temp[0];
		$tags[] = $segment1;
		$segment2 = $temp[1];
		$tags[] = $segment2;
		$segment3 = $temp[2];
		$tags[] = $segment3;
		$segment4 = $temp[3];
		$tags[] = $segment4;
	}



	if ($user_id == 44582 || $user_id == 49161 || $user_id == 44143 ) {
		$shippingcost = 0;

		$tags = array();

		echo "select * from shippingcost where user_id = " . $user_id . " and product_id=" . $product_id;
		$result = $conn->query("select * from shippingcost where user_id = " . $user_id . " and product_id=" . $product_id);
		if ($result->num_rows > 0) {
			while ($shippingCost = $result->fetch_assoc()) {
				$shippingcost = $shippingCost['cost'];
				$tags[] = "Ship_" . $shippingcost;
			}
		}

		// 

		echo "     valuee tagess        " . "\n";
		print_r($tags);
	}
	if($user_id == 48747){
		$shippingcost = 0;

		$tags = array();

		echo "select * from shippingcost where user_id = " . $user_id . " and product_id=" . $product_id;
		$result = $conn->query("select * from shippingcost where user_id = " . $user_id . "  and cost in (9.95, 14.95, 19.95, 24.95, 29.95, 39.95, 49.95, 74.95, 99.95, 124.95, 149.95, 199.95, 249.95, 274.95, 299.95) and product_id=" . $product_id);
		if ($result->num_rows > 0) {
			while ($shippingCost = $result->fetch_assoc()) {
				$shippingcost = $shippingCost['cost'];
				$tags[] = $shippingcost;
			}
		}
		else{

			$conn->query("UPDATE products SET block = 1 WHERE user_id = " . $user_id . " AND product_id = " . $product_id);
		   return false;
		}
	}
	if ($user_id == 48747) {
		$title = $productRow['title'];
		$tags[] ="Ebay";
		// Corvette models
		$corvetteModels = ["C3 Corvette", "C4 Corvette", "C5 Corvette", "C6 Corvette", "C7 Corvette"];
		foreach ($corvetteModels as $model) {
			if (strpos($title, $model) !== false) {
				$tags[] = $model;
			}
		}

		// General tags
		$keywords = [
			"Corvette",
			"GTO",
			"G8",
			"SS Sedan",
			"SSR",
			"XLR",
			"Camaro",
			"Firebird",
			"Mustang",
			"Suburban",
			"Blazer",
			"Truck",
			"Thunderbird",
			"CTS, CTS-V",
			"Charger",
			"Challenger",
			"H2",
			"NOS"
		];

		foreach ($keywords as $keyword) {
			if (strpos($title, $keyword) !== false) {
				$tags[] = $keyword;
			}
		}
	}

	if ($user_id == 44893) {
		$temp = explode(":", $product_type);
		$product_type = end($temp);
		$tags[] = $product_type;
		echo $product_type . "                        ";
	}

	if ($user_id == 44401) {
		$tempArr = explode(":", $product_type);
		$product_type = trim($tempArr[0]);
		$tags[] = $product_type;
	}

	if ($user_id == 42198) {

		$searchstring = $productRow['searchstring'];
		$tags[] = $searchstring;
		echo "$user_id tags $tags";
	}

	if ($user_id == 42198) {
		$tags[] = $condition_val;
	}

	if ($user_id == 43580) {
		$result = $conn->query("select * from storecategories where category_id ='" . $category_idv . "' and user_id = " . $user_id);
		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {
				$tags = $row['category_name'];
				// $shopifycategoryid = $row['shopifycategoryid'];
				// $catArr[$category_id] = $shopifycategoryid;
			}
		}
	}

	if ($user_id == 44401) {
		$result = $conn->query("select * from storecategories where category_id ='" . $category_idv . "' and user_id = " . $user_id);
		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {
				$tags[] = $row['category_name'];
				// $shopifycategoryid = $row['shopifycategoryid'];
				// $catArr[$category_id] = $shopifycategoryid;
			}
		}
	}
	$shopifyproductid = $productRow['shopifyproductid'];

	if (in_array($user_id, $categoryLogicArr)) {
		$storecategoryid = $productRow['storecategoryid'];
		$storecategoryid2 = $productRow['storecategoryid2'];
		if (array_key_exists($storecategoryid, $catArr)) {
			$temp = $catArr[$storecategoryid];
			$category_name = $temp['category_name'];
			$parent_id = $temp['parent_id'];
			$tags[] = $category_name;
			$product_type = $category_name;
			if ($parent_id != "") {
				if (array_key_exists($parent_id, $catArr)) {
					$temp1 = $catArr[$parent_id];
					$category_name1 = $temp1['category_name'];
					$parent_id = $temp1['parent_id'];
					$tags[] = $category_name1;
					if ($user_id != 19321 && $user_id != 19563) {
						$product_type = $category_name1;
					}
					if ($parent_id != "") {
						if (array_key_exists($parent_id, $catArr)) {
							$temp2 = $catArr[$parent_id];
							$category_name2 = $temp2['category_name'];
							$tags[] = $category_name2;
							if ($user_id != 19321 && $user_id != 19563) {
								$product_type = $category_name2;
							}
						}
					}
				}
			}
		}
		if (array_key_exists($storecategoryid2, $catArr)) {
			$temp = $catArr[$storecategoryid2];
			$category_name = $temp['category_name'];
			$parent_id = $temp['parent_id'];
			$tags[] = $category_name;
			if ($parent_id != "") {
				if (array_key_exists($parent_id, $catArr)) {
					$temp1 = $catArr[$parent_id];
					$category_name1 = $temp1['category_name'];
					$tags[] = $category_name1;
				}
			}
		}
		if ($user_id == 15057) {
			if ($storecategoryid == "27421515014") { // LAMBSKIN LEATHER
				$tags[] = "retail_lambskins";
			} else if ($storecategoryid == "27421516014") { // GOATSKIN LEATHER
				$tags[] = "retail_goatskins";
			} else if ($storecategoryid == "27421517014") { // METALLIC LEATHER
				$tags[] = "retail_metallics";
			} else if ($storecategoryid == "1095742014") { // CALF/COW LEATHER
				$tags[] = "retail_calf/cow";
			} else {
				$conn->query("update products set block = 1, status = 'Ready to Import' where user_id = 15057 and product_id = " . $productRow['product_id']);
				return false;
			}
		}
	}


	$variantResult = $conn->query("select * from product_variants where product_id = " . $product_id . " and user_id = " . $user_id . " and block = 0 and duplicate = 0 and shopifyvariantid = '' ");

	$variants = array();
	$options = array();
	$imageArr = array();
	$invArr = array();
	$imgSecondaryArr = array();
	if ($option1name != '') {
		$options[] = array("name" => $option1name, "position" => 1);
	}
	if ($option2name != '') {
		$options[] = array("name" => $option2name, "position" => 2);
	}
	if ($option3name != '') {
		$options[] = array("name" => $option3name, "position" => 3);
	}

	$variantPosition = 1;
	while ($variantRow = $variantResult->fetch_assoc()) {
		$variant_id = $variantRow['id'];
		$sku = $variantRow['sku'];
		$price = $variantRow['price'];
		$orig_price = $variantRow['orig_price'];
		if ($user_id == 19443) {
			$price = applyPriceMarkupSettingsRow19443($price, $settingsRow);
		} else {

			$price = applyPriceMarkupSettingsRow($price, $settingsRow);
		}

		if ($user_id == 4881) {
			$searchstring = $productRow['searchstring'];
			if ($searchstring == "eBayMotors") {
				$price = $price;
			} else if ($searchstring == "Australia") {
				$price = $price * 0.76;
			} else if ($searchstring == "France") {
				$price = $price * 1.19;
			} else if ($searchstring == "Spain") {
				$price = $price * 1.19;
			} else if ($searchstring == "Germany") {
				$price = $price * 1.19;
			} else if ($searchstring == "Italy") {
				$price = $price * 1.19;
			} else  if ($searchstring == "UK") {
				$price = $price * 1.39;
			} else  if ($searchstring == "Canada") {
				$price = $price * 0.80;
			} else  if ($searchstring == "Ireland") {
				$price = $price * 1.14;
			} else {
				return false;
			}
		}


		if (strlen($sku) == 0) {
			$sku = $ebayitemid . "-" . $variantRow['id'];
		}

		if ($user_id == 44057 && array_key_exists($product_id, $shippingCostArr)) {
			$shippingcost = 0;
			foreach ($shippingCostArr[$product_id] as $k => $v) {
				$shippingcost = $v;
				break;
			}
			$tags = "Ship_" . $shippingcost;

			print_r($tags);
		}

		$quantity = $variantRow['quantity'];
		if ($user_id != 1690) {
			$weight = $variantRow['weight'];
			$weight_unit = $variantRow['weight_unit'];
		}

		if ($user_id == 44841) {
			$weight = 0.02;
			$weight_unit = "kg";
		}

		if ($user_id == 12522) {
			$weight = 0.02;
			$weight_unit = "kg";
		}
		$productid = $variantRow['productid'];
		$option1val = $variantRow['option1val'];
		$option2val = $variantRow['option2val'];
		$option3val = $variantRow['option3val'];
		if ($isSizeType && $user_id == 2481) {
			$option1val = $variantRow['option1val'];
			$option2val = $variantRow['option3val'];
			$option3val = $variantRow['option4val'];
		}
		if ($isSizeType && $user_id == 6364) {
			$option1val = $variantRow['option2val'];
			$option2val = $variantRow['option3val'];
			$option3val = $variantRow['option4val'];
		}
		if ($isSizeType && $user_id == 32) {
			$option1val = $variantRow['option1val'];
			$option2val = $variantRow['option3val'];
			$option3val = $variantRow['option4val'];
		}
		if ($isSizeType && $user_id == 4188) {
			$option1val = $variantRow['option2val'];
			$option2val = $variantRow['option3val'];
			$option3val = $variantRow['option4val'];
		}

		if ($user_id == 45738) {
			$sku =  $sku;
		}

		if ($user_id == 46021) {
			$sku =  $sku;
		}

		$taxable = true;
		if ($settingsRow['taxable'] == 0) {
			$taxable = false;
		}
		if ($user_id == 45000) {
			$productid = $sku;
		}
		if ($user_id == 44841) {
			$productid = "";
			$taxable = false;
		}
		if ($user_id == 46910) {

			$tags[] = $sku;
		}
		if ($user_id == 46668 && $sku == '') {

			$sku = $ebayitemid;
		}
		if ($user_id == 47309) {
			$description = "";
			$item_specific = $productRow['item_specific'];
			$condition_val = $productRow['condition_val'];
			$description = setdescriptiontemplate47309($item_specific, $title, $condition_val);
		}
		if ($user_id == 47617) {
			//GBP TO EURO
			$price = $variantRow['orig_price'];
			if ($price > 0) {
				$price = $price * 1.20;
			} else {
				$price = $variantRow['price'];
				$price = $price * 1.20;
			}
			//$price = round($price);
		}
		if ($user_id  == 48662) {
			$taxable = true;
		}
		if ($user_id == 47855) {
			$sku = $variantRow['sku'];
		}
		if ($user_id == 48805) {
			$weight = $weight * 0.453592;
			$weight_unit = "kg";
		}
		$singleVariant = array(
			"sku" => $sku,
			"position" => $variantPosition++,
			"price" => number_format($price, 2, '.', ''),
			"inventory_policy" => "deny",
			"fulfillment_service" => $fulfilment_service,
			"inventory_management" => $inventory_policy,
			"taxable" => $taxable,
			"weight" => $weight,
			"weight_unit" => $weight_unit,
			"barcode" => $productid,
			"requires_shipping" => true,
			"quantity" => $quantity
		);
		if (($user_id == 41578 || $user_id == 43633 || $user_id == 42894 || $user_id == 44890) && $inventory_policy == "NO") {
			$singleVariant['inventory_management'] = "";
		}

		if ($user_id == 48043) {
			$singleVariant["inventory_policy"] = "continue";
		}
		if ($user_id == 44495) {
			$product_type = $productRow['product_type'];
			if (strpos($product_type, 'Clothes') === false) {
				$singleVariant["requires_shipping"] = false;
			}
		}

		if ($user_id == 46454) {
			if ($brand == 'Oliver People' || $brand == 'Saint Laurent') {
				$comparePrice = $price * 0.35;
				$singleVariant["compare_at_price"] = number_format($comparePrice, 2, '.', '');
			} else if ($brand == 'PUMA') {
				$comparePrice = $price * 0.60;
				$singleVariant["compare_at_price"] = number_format($comparePrice, 2, '.', '');
			}
		}



		if ($user_id == 44719 || $user_id == 45165) {
			$singleVariant["barcode"]  = $ebayitemid;
		}
		if ($user_id == 47617) {
			$price = $variantRow['orig_price'];
			if ($price > 0) {
				$price = $price;
			} else {
				$price = $variantRow['price'];
			}
			$metafieldsvvv = array();
			$metafieldsvvv[] = array("key" => "ebay_item_price", "value" =>  $price, "type" => "single_line_text_field", "namespace" => "custom");
			$singleVariant["metafields"] = $metafieldsvvv;
		}

		if ($user_id == 46021) {
			if (is_array($itemSpecificObj)) {
				foreach ($itemSpecificObj as $k => $v) {
					if ($k == "EAN") {
						$singleVariant["barcode"] = $v;
						break;
					}
				}
			}
		}





		if ($user_id == 45000) {
			$productid = $sku;
		}

		$imgskuArr = array();
		if ($option1val != '') {
			$singleVariant['option1'] = trim($option1val);
			$imgskuArr[] = trim($option1val);
		}
		if ($option2val != '') {
			$singleVariant['option2'] = trim($option2val);
			$imgskuArr[] = trim($option2val);
		}
		if ($option3val != '') {
			$singleVariant['option3'] = trim($option3val);
			$imgskuArr[] = trim($option3val);
		}
		if (count($variants) > 99) {
			continue;
		}

		$imgsku = implode("::", $imgskuArr);
		$invArr[$imgsku] = $quantity;
		$imageResult = $conn->query("select * from product_images where user_id = " . $user_id . " and variant_id = " . $variant_id);
		if ($imageResult->num_rows > 0) {
			$isFirstImage = true;
			while ($imageRow = $imageResult->fetch_assoc()) {
				$imgurl = $imageRow['imgurl'];
				if ($user_id != 15715) {
					$imgurl = str_replace("_12.JPG", "_57.JPG", $imgurl);
					$imgurl = str_replace("_1.JPG", "_57.JPG", $imgurl);
					$imgurl = str_replace("_0.JPG", "_57.JPG", $imgurl);
					$imgurl = str_replace("_6.JPG", "_57.JPG", $imgurl);
				} else {
					if (strpos($imgurl, 'ebayimg') !== false) {
						$imgurl = str_replace("_12.JPG", "_57.JPG", $imgurl);
						$imgurl = str_replace("_1.JPG", "_57.JPG", $imgurl);
						$imgurl = str_replace("_0.JPG", "_57.JPG", $imgurl);
						$imgurl = str_replace("_6.JPG", "_57.JPG", $imgurl);
					}
				}
				if ($user_id == 38128 || $user_id == 50223 || $user_id == 50338 ) {
					$imgurl = str_replace("_12.PNG", "_57.PNG", $imgurl);
					$imgurl = str_replace("_1.PNG", "_57.PNG", $imgurl);
					$imgurl = str_replace("_0.PNG", "_57.PNG", $imgurl);
					$imgurl = str_replace("_6.PNG", "_57.PNG", $imgurl);
				}
				if ($isFirstImage) {
					$imageArr[] = $imgurl;
					$singleVariant['imgurl'] = trim($imgurl);
				} else {
					$imgSecondaryArr[] = trim($imgurl);
				}
				$isFirstImage  = false;
				/*if($user_id == 4752){
			            $imageArr[] = array("src" => trim($imgurl), "position" => $position++,"width" => 1200,"height" => 1600);
                    }*/
			}
		}
		$variants[] = $singleVariant;
	}
	$imgSecondaryArr = array_unique($imgSecondaryArr);
	if (count($variants) == 0) {
		echo "product without variants";
		return false;
	} //
	if (($user_id == 41867 || $user_id == 45245 || $user_id == 45204 || $user_id == 46444 || $user_id == 45549) && count($variants) > 100) {
		$variants = array_slice($variants, 0, 100, TRUE);
	}

	if (count($variants) > 100) {
		$conn->query("update products set status = 'error', errdetails = 'More than 100 variants.' where product_id = " . $product_id . " and user_id = " . $user_id);
		echo "product having more than 100 variants";
		return false;
	}




	if ($user_id == 44582 || $user_id == 49161) {
		$storecategoryid = $productRow['storecategoryid'];
		// $tags = array();
		$product_type = "";
		if (array_key_exists($storecategoryid, $catArr)) {
			$temp = $catArr[$storecategoryid];
			$category_name = $temp['category_name'];
			$parent_id = $temp['parent_id'];
			$tags[] = $category_name;
			$product_type = $category_name;
			if ($parent_id != "") {
				if (array_key_exists($parent_id, $catArr)) {
					$temp1 = $catArr[$parent_id];
					$category_name1 = $temp1['category_name'];
					$tags[] = $category_name1;
					$product_type = $category_name1;
				}
			}
		}
	}

	if ($user_id == 43700) {
		$tags[] = $condition_val;

		$tags[] = $productRow['searchstring'];
	}

	// Add settings from DB
	$dbtags = trim($settingsRow['tags']);
	$dbvendor = trim($settingsRow['vendor']);
	$dbproduct_type = trim($settingsRow['product_type']);
	if ($dbvendor != "") {
		$brand = $dbvendor;
		echo "anjali12 $brand";
	}

	if ($dbproduct_type != "") {
		$product_type = $dbproduct_type;
	}
	if ($dbtags != "") {
		$tempTags = explode(",", $dbtags);
		foreach ($tempTags as $tempTag) {
			$tags[] = trim($tempTag);
		}
	}

	$pubstatus = "active";
	if ($published == 0) {
		$pubstatus = "draft";
	}
	if (strlen($sku) > 255) {
		$str = wordwrap($sku, 254);
		$str = explode("\n", $str);
		$sku = $str[0];
	}
	if ($user_id == 43398) {
		$tags = explode(":", $product_type);
		$product_type = end($tags);
	}




	if ($user_id == 44893) {
		//die();

		$p_category_name = "";
		$product_id = $productRow['product_id'];

		echo "select category_name from storecategories where shopifycategoryid = '' and user_id = '" . $user_id . "'  and category_id in (select storecategoryid from products where user_id= '" . $user_id . "'  and  product_id = '" . $product_id . "')";

		$result = $conn->query("select category_name,category_id from storecategories where shopifycategoryid = '' and user_id = '" . $user_id . "'  and category_id in (select storecategoryid from products where user_id= '" . $user_id . "'  and  product_id = '" . $product_id . "') ");
		if ($result->num_rows > 0) {


			// while (
			$row = $result->fetch_assoc();
			//) {
			$child_category_name = $row['category_name'];
			$child_category_id  = $row['category_id'];

			echo "hkjhhghkhkjhkjh" . $child_category_id;

			$tags[] = $child_category_name;



			$newcategory = $conn->query("select * from storecategories where user_id = 44893");

			//echo "select * from storecategories where user_id = 44893";

			if ($newcategory->num_rows > 0) {


				while ($row = $newcategory->fetch_assoc()) {

					$category_id = $row['category_id'];


					$category_name = $row['category_name'];
					$parent_id = $row['parent_id'];
					$catArr[$category_id] = array("category_name" => $category_name, "parent_id" => $parent_id);
				}

				foreach ($catArr as $categoryId => $categoryData) {
					if ($categoryData['category_name'] == $child_category_name && $categoryId == $child_category_id) {
						$parent_id = $categoryData['parent_id'];
						break;
					}
				}
				if ($parent_id != ' ') {

					foreach ($catArr as $categoryId => $categoryData) {
						if ($categoryId == $parent_id) {
							$p_category_name = $categoryData['category_name'];
							break;
						}
					}
				}

				if ($p_category_name != ' ') {

					echo "herrrrrr isss        ";

					echo "Parent id  " . $parent_id;

					echo "<br></br>";
					echo "                   ";

					echo "Parent Category name " . $p_category_name;

					$tags[] = $p_category_name;
				} else {
					echo "Category not found";
				}


				echo "here is tags ";
				print_r($tags);

				//print_r($catArr);

				//exit(0);

			}



			// 




			//}		



			// echo ".>>>>>soosmayyyrjhkjhkjhgkjh";
			// print_r($tags);




		}





		if ($user_id == 46021) {
			$tags = array();
			if (strpos($title, 'Mens') !== false) {
				$tags[] = "gender_mens";
			}
			if (strpos($title, 'Womens') !== false) {

				$tags[] = "gender_womens";
			}
		}
	}


	if ($user_id == 47617) {
		//die();

		$p_category_name = "";
		$product_id = $productRow['product_id'];

		echo "select category_name from storecategories where shopifycategoryid != '' and user_id = '" . $user_id . "'  and category_id in (select storecategoryid from products where user_id= '" . $user_id . "'  and  product_id = '" . $product_id . "')";

		$result = $conn->query("select category_name,category_id from storecategories where shopifycategoryid != '' and user_id = '" . $user_id . "'  and category_id in (select storecategoryid from products where user_id= '" . $user_id . "'  and  product_id = '" . $product_id . "') ");
		if ($result->num_rows > 0) {


			// while (
			$row = $result->fetch_assoc();
			//) {
			$child_category_name = $row['category_name'];
			$child_category_id  = $row['category_id'];

			echo "hkjhhghkhkjhkjh" . $child_category_id;

			$tags[] = $child_category_name;



			$newcategory = $conn->query("select * from storecategories where user_id = 47617");

			//echo "select * from storecategories where user_id = 41563";

			if ($newcategory->num_rows > 0) {


				while ($row = $newcategory->fetch_assoc()) {

					$category_id = $row['category_id'];


					$category_name = $row['category_name'];
					$parent_id = $row['parent_id'];
					$catArr[$category_id] = array("category_name" => $category_name, "parent_id" => $parent_id);
				}

				foreach ($catArr as $categoryId => $categoryData) {
					if ($categoryData['category_name'] == $child_category_name && $categoryId == $child_category_id) {
						$parent_id = $categoryData['parent_id'];
						break;
					}
				}
				if ($parent_id != ' ') {

					foreach ($catArr as $categoryId => $categoryData) {
						if ($categoryId == $parent_id) {
							$p_category_name = $categoryData['category_name'];
							break;
						}
					}
				}

				if ($p_category_name != ' ') {

					echo "herrrrrr isss        ";

					echo "Parent id  " . $parent_id;

					echo "<br></br>";
					echo "                   ";

					echo "Parent Category name " . $p_category_name;

					$tags[] = $p_category_name;
				} else {
					echo "Category not found";
				}


				echo "here is tags ";
				print_r($tags);

				//print_r($catArr);

				//exit(0);

			}

			// TODO: find child_category_name in catArr with child_category_nameand find parent_id
			//find pparent_id in catArr and get category_name and add in tag

			// 




			//}		



			// echo ".>>>>>soosmayyyrjhkjhkjhgkjh";
			// print_r($tags);




		}
	}


	if ($user_id == 49421) {
		//die();

		$p_category_name = "";
		$product_id = $productRow['product_id'];

		echo "select category_name from storecategories where shopifycategoryid != '' and user_id = '" . $user_id . "'  and category_id in (select storecategoryid from products where user_id= '" . $user_id . "'  and  product_id = '" . $product_id . "')";

		$result = $conn->query("select category_name,category_id from storecategories where shopifycategoryid != '' and user_id = '" . $user_id . "'  and category_id in (select storecategoryid from products where user_id= '" . $user_id . "'  and  product_id = '" . $product_id . "') ");
		if ($result->num_rows > 0) {


			// while (
			$row = $result->fetch_assoc();
			//) {
			$child_category_name = $row['category_name'];
			$child_category_id  = $row['category_id'];

			echo "hkjhhghkhkjhkjh" . $child_category_id;

			$tags[] = $child_category_name;



			$newcategory = $conn->query("select * from storecategories where user_id = 49421");

			
			if ($newcategory->num_rows > 0) {


				while ($row = $newcategory->fetch_assoc()) {

					$category_id = $row['category_id'];


					$category_name = $row['category_name'];
					$parent_id = $row['parent_id'];
					$catArr[$category_id] = array("category_name" => $category_name, "parent_id" => $parent_id);
				}

				foreach ($catArr as $categoryId => $categoryData) {
					if ($categoryData['category_name'] == $child_category_name && $categoryId == $child_category_id) {
						$parent_id = $categoryData['parent_id'];
						break;
					}
				}
				if ($parent_id != ' ') {

					foreach ($catArr as $categoryId => $categoryData) {
						if ($categoryId == $parent_id) {
							$p_category_name = $categoryData['category_name'];
							break;
						}
					}
				}

				if ($p_category_name != ' ') {

					echo "herrrrrr isss        ";

					echo "Parent id  " . $parent_id;

					echo "<br></br>";
					echo "                   ";

					echo "Parent Category name " . $p_category_name;

					$tags[] = $p_category_name;
				} else {
					echo "Category not found";
				}


				echo "here is tags ";
				print_r($tags);

				//print_r($catArr);

				//exit(0);

			}

			// TODO: find child_category_name in catArr with child_category_nameand find parent_id
			//find pparent_id in catArr and get category_name and add in tag

			// 




			//}		



			// echo ".>>>>>soosmayyyrjhkjhkjhgkjh";
			// print_r($tags);




		}
	}

	if ($user_id == 50223) {
		//die();

		$p_category_name = "";
		$product_id = $productRow['product_id'];

		echo "select category_name from storecategories where shopifycategoryid != '' and user_id = '" . $user_id . "'  and category_id in (select storecategoryid from products where user_id= '" . $user_id . "'  and  product_id = '" . $product_id . "')";

		$result = $conn->query("select category_name,category_id from storecategories where shopifycategoryid != '' and user_id = '" . $user_id . "'  and category_id in (select storecategoryid from products where user_id= '" . $user_id . "'  and  product_id = '" . $product_id . "') ");
		if ($result->num_rows > 0) {


			// while (
			$row = $result->fetch_assoc();
			//) {
			$child_category_name = $row['category_name'];
			$child_category_id  = $row['category_id'];

			echo "hkjhhghkhkjhkjh" . $child_category_id;

			$tags[] = $child_category_name;



			$newcategory = $conn->query("select * from storecategories where user_id = 50223");

		

			if ($newcategory->num_rows > 0) {


				while ($row = $newcategory->fetch_assoc()) {

					$category_id = $row['category_id'];


					$category_name = $row['category_name'];
					$parent_id = $row['parent_id'];
					$catArr[$category_id] = array("category_name" => $category_name, "parent_id" => $parent_id);
				}

				foreach ($catArr as $categoryId => $categoryData) {
					if ($categoryData['category_name'] == $child_category_name && $categoryId == $child_category_id) {
						$parent_id = $categoryData['parent_id'];
						break;
					}
				}
				if ($parent_id != ' ') {

					foreach ($catArr as $categoryId => $categoryData) {
						if ($categoryId == $parent_id) {
							$p_category_name = $categoryData['category_name'];
							break;
						}
					}
				}

				if ($p_category_name != ' ') {

					echo "herrrrrr isss        ";

					echo "Parent id  " . $parent_id;

					echo "<br></br>";
					echo "                   ";

					echo "Parent Category name " . $p_category_name;

					$tags[] = $p_category_name;
				} else {
					echo "Category not found";
				}


				echo "here is tags ";
				print_r($tags);
			}
		}
	}


	



	if ($user_id == 46190) {
		//die();

		$p_category_name = "";
		$product_id = $productRow['product_id'];

		echo "select category_name from storecategories where shopifycategoryid = '' and user_id = '" . $user_id . "'  and category_id in (select storecategoryid from products where user_id= '" . $user_id . "'  and  product_id = '" . $product_id . "')";

		$result = $conn->query("select category_name,category_id from storecategories where shopifycategoryid = '' and user_id = '" . $user_id . "'  and category_id in (select storecategoryid from products where user_id= '" . $user_id . "'  and  product_id = '" . $product_id . "') ");
		if ($result->num_rows > 0) {


			// while (
			$row = $result->fetch_assoc();
			//) {
			$child_category_name = $row['category_name'];
			$child_category_id  = $row['category_id'];

			echo "hkjhsomayhghkhkjhkjh" . $child_category_id;

			$tags[] = $child_category_name;



			$newcategory = $conn->query("select * from storecategories where user_id = 46190");

			//echo "select * from storecategories where user_id = 46190";

			if ($newcategory->num_rows > 0) {


				while ($row = $newcategory->fetch_assoc()) {

					$category_id = $row['category_id'];


					$category_name = $row['category_name'];
					$parent_id = $row['parent_id'];
					$catArr[$category_id] = array("category_name" => $category_name, "parent_id" => $parent_id);
				}

				foreach ($catArr as $categoryId => $categoryData) {
					if ($categoryData['category_name'] == $child_category_name && $categoryId == $child_category_id) {
						$parent_id = $categoryData['parent_id'];
						break;
					}
				}
				if ($parent_id != ' ') {

					foreach ($catArr as $categoryId => $categoryData) {
						if ($categoryId == $parent_id) {
							$p_category_name = $categoryData['category_name'];
							break;
						}
					}
				}

				if ($p_category_name != ' ') {

					echo "herrrrrr isss        ";

					echo "Parent id  " . $parent_id;

					echo "<br></br>";
					echo "                   ";

					echo "Parent Category name " . $p_category_name;

					$tags[] = $p_category_name;
				} else {
					echo "Category not found";
				}


				echo "here is tags ";
				print_r($tags);
			}
		}
	}
	if ($user_id == 46127) {

		$description = "";
	}
	if ($user_id == 47855) {
		$item_specific = $productRow['item_specific'];
		$itemSpecArr = json_decode($item_specific, true);
		if (isset($itemSpecArr['Brand'])) {
			$brand = $itemSpecArr['Brand'];
		}
		if (isset($itemSpecArr['Golf Club Type'])) {
			$product_type = $itemSpecArr['Golf Club Type'];
		}
		if (isset($itemSpecArr['Flex'])) {
			$tags[] = $itemSpecArr['Flex'];
		}
	}
	if($user_id == 48805){
		$itemid = $productRow['ebayitemid'];
       $description = $description."<p style=\"word-wrap: break-word\"><strong>Item ID:</strong> $itemid</p>";
	}
	$data = array(
		"product" => array(
			"title" => $title,
			"body_html" => $description,
			"vendor" => $brand,
			"product_type" => $product_type,
			"status" => strtoupper($pubstatus),
			"tags" => $tags,
			"published_scope" => "global",
			"variants" => $variants,
			"options" => $options
		)
	);



	if ($user_id == 44664 || $user_id == 43040) {
		$metafields = array();

		$result = $conn->query("select * from product_dimensions where  user_id = " . $user_id . " and ebayitemid = " . $productRow['ebayitemid'] . "    limit 1");

		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {
				echo  $productRow['ebayitemid'];
				echo  $shopifyproductid;

				$len = $row['length'];
				$width = $row['width'];
				$depth = $row['depth'];

				echo "length $len";
				echo "width $width";
				echo "depth $depth";

				$metafields[] = array("key" => "length", "value" => $len, "type" => "single_line_text_field", "namespace" => "custom");
				$metafields[] = array("key" => "width", "value" => $width, "type" => "single_line_text_field", "namespace" => "custom");
				if ($user_id == 43040) {
					$metafields[] = array("key" => "depth", "value" => $depth, "type" => "single_line_text_field", "namespace" => "custom");
				} else {
					$metafields[] = array("key" => "height", "value" => $depth, "type" => "single_line_text_field", "namespace" => "custom");
				}
				echo "set meta field ";
			}
			$data["product"]["metafields"] = $metafields;
		}
	}

    if ($user_id == 50251) {
        $metafields = array();
		$tags =array();
		$condition_val = $productRow['condition_val'];
		$item_specific = $productRow['item_specific'];
		$title  =$productRow['title'];
		$res = mapItemAttributes($title,$tags,$metafields); // fixed: you don’t need to pass tags/metafields in
        $tags       = array_merge($tags, $res['tags']);
        $metafields = array_merge($metafields, $res['metafields']);
		$storecategoryid  =$productRow['storecategoryid'];
         
		$item_specificObj = json_decode($item_specific, true);
		$productType       = $productRow['product_type'];
		$parts = array_map('trim', explode(':', $productType));
   
        $partGroup = isset($parts[2]) ? 'PartGroup_' . str_replace([' ', ','], '_', $parts[2]) : '';

        $partSubGroup = isset($parts[3]) ? 'PartSubGroup_' . str_replace([' ', ','], '_', $parts[3]) : '';

        if ($partGroup)    $tags[] = $partGroup;
        if ($partSubGroup) $tags[] = $partSubGroup;
		$metafields[] = array(  
                "namespace" => "custom",
				"key"  =>"condition",
				"type" =>"single_line_text_field",
				"value" =>"$condition_val"
               );
	     foreach ($item_specificObj as $key => $value) {
            
            $clean_key = str_replace(' ', '', $key);      
            $clean_value=str_replace(' ', '', $value);
                          
           
            if($key == "Brand" ){
				 $tags[] = $clean_key . '_' . $clean_value;
               $metafields[] = array(  
                "namespace" => "custom",
				"key"  =>"make",
				"type" =>"single_line_text_field",
				"value" =>"$value"
               );
            }
            if($key == "Model"){
				 $tags[] = $clean_key . '_' . $clean_value;
               $metafields[] = array(  
                "namespace" => "custom",
				"key"  =>"model",
				"type" =>"single_line_text_field",
				"value" =>"$value"
               );
            }
            if($key == "Year"){
				 $tags[] = $clean_key . '_' . $clean_value;
               $metafields[] = array(  
                "namespace" => "custom",
				"key"  =>"year",
				"type" =>"list.single_line_text_field",
				"value" =>json_encode(array($value))
               );
            }
            if($key == "Part"){
               $metafields[] = array(  
                "namespace" => "custom",
				"key"  =>"part",
				"type" =>"single_line_text_field",
				"value" =>"$value"
               );
            }
            else{
                $metafields[] = array(  
                "namespace" => "custom",
				"key"  =>"part",
				"type" =>"single_line_text_field",
				"value" =>"$partnew"
               );
            }
            if($key == "Warranty Percentage Values"){
               $metafields[] = array(  
                "namespace" => "custom",
				"key"  =>"warranty_percentage_values",
				"type" =>"json",
				"value" =>json_encode(array($value))
               );
            }
            else{
                $metafields[] = array(  
                "namespace" => "custom",
				"key"  =>"warranty_percentage_values",
				"type" =>"json",
				"value" =>json_encode(array("Not APPLICABLE"))
               );
            }
            if($key == "Make logo"){
               $metafields[] = array(  
                "namespace" => "custom",
				"key"  =>"make_logo",
				"type" =>"file_reference",
				"value" =>"$value"
               );
            }
            
            
        }
		  $storecategorysss = $conn->query("
                    SELECT * 
                    FROM storecategories 
                    WHERE user_id = 50251 
                      AND category_id = " . (int)$storecategoryid
                );
                $catrow = $storecategorysss->fetch_assoc();

                if (!empty($catrow['parent_id'])) {
                    $childcategory = $catrow['category_name'];
                    $catid  = "PartType_" . str_replace(' ', '_', $childcategory);

                    $parentresults = $conn->query("
                        SELECT * 
                        FROM storecategories 
                        WHERE user_id = 50251 
                          AND category_id = " . (int)$catrow['parent_id']
                    );
                    $parentRow = $parentresults->fetch_assoc();

                    $parentCategory = $parentRow['category_name'];
                    $partid = "PartCategory_" . str_replace(' ', '_', $parentCategory);

                    $tags[] = $catid;
                    $tags[] = $partid;

                    if ($parentCategory) {
                        $metafields[] = [
                            "namespace" => "custom",
                            "key"       => "part_category",
                            "type"      => "single_line_text_field",
                            "value"     => $parentCategory
                        ];
                    }

                    if ($childcategory) {
                        $metafields[] = [
                            "namespace" => "custom",
                            "key"       => "part",
                            "type"      => "single_line_text_field",
                            "value"     => $childcategory
                        ];
                    }
                }
      $data["product"]["metafields"] = $metafields;
	  $data["product"]["tags"] = $tags;


	}
	if ($user_id == 46926) {

		$metafieldsvv = array();
		$metafieldsvv[] = array("key" => "condition_", "value" => "Pre-owned", "type" => "list.single_line_text_field", "namespace" => "custom");
		$data["product"]["metafields"] = $metafieldsvv;
	}
	if ($user_id == 47379) {

		$metafields = array();

		$result = $conn->query("select * from product_dimensions where  user_id = " . $user_id . " and ebayitemid = " . $productRow['ebayitemid'] . " limit 1");

		if ($result->num_rows > 0) {
			while ($row = $result->fetch_assoc()) {
				echo  $productRow['ebayitemid'];
				echo  $shopifyproductid;

				$len = $row['length'];
				$width = $row['width'];
				$depth = $row['depth'];

				echo "length $len";
				echo "width $width";
				echo "depth $depth";

				$metafields[] = array("key" => "length", "value" => $len, "type" => "single_line_text_field", "namespace" => "custom");
				$metafields[] = array("key" => "width", "value" => $width, "type" => "single_line_text_field", "namespace" => "custom");
				$metafields[] = array("key" => "height", "value" => $depth, "type" => "single_line_text_field", "namespace" => "custom");

				echo "set meta field ";
			}

			$metafields[] = array("key" => "Weight", "value" => $weight, "type" => "single_line_text_field", "namespace" => "custom");
			$data["product"]["metafields"] = $metafields;
		}
	}

	if ($user_id == 44401) {

		$metafields[] = array("key" => "listing_condition", "value" => $condition_val, "type" => "single_line_text_field", "namespace" => "custom");
		$data["product"]["metafields"] = $metafields;
	}



	if ($user_id == 46026) {
		$metafields = array();
		$condition_note = $productRow['condition_note'];
		$condition_val = $productRow['condition_val'];
		if ($condition_val != '') {
			$metafields[] = array("key" => "condition", "value" => $condition_val, "type" => "single_line_text_field", "namespace" => "my_fields");
		}
		if ($condition_note != '') {
			$metafields[] = array("key" => "condition_description", "value" => $condition_note, "type" => "single_line_text_field", "namespace" => "my_fields");
		}
		$data["product"]["metafields"] = $metafields;
	}
	if($user_id == 48908){
		$metafields = array();
		$item_specific = $productRow['item_specific'];
		$itemSpecificJson = json_decode($item_specific, true);
		foreach ($itemSpecificJson as $k => $v) {
			print_r($k);

			if ($k == "Allergene") {
				$key = "informazioni_sugli_allergeni";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" =>"single_line_text_field", "namespace" => "custom");
			}
			if ($k == "Paese di fabbricazione") {
				$key = "country";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" =>"single_line_text_field", "namespace" => "custom");
			}
			if ($k == "Attributi speciali") {
				$key = "esigenze_alimentari";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" =>"single_line_text_field", "namespace" => "custom");
			}
			if ($k == "Area Geografica") {
				$key = "area_geografica";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" =>"single_line_text_field", "namespace" => "custom");
			}
			if ($k == "Stile") {
				$key = "dolcezza_del_vino";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" =>"single_line_text_field", "namespace" => "custom");
			}
			if ($k == "Tipo") {
				$key = "variet_di_vino";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" =>"single_line_text_field", "namespace" => "custom");
			}
		}
		$data["product"]["metafields"] = $metafields;
		print_r($data["product"]);
	}
	if ($user_id == 46021) {

		$metafields = array();

		$item_specific = $productRow['item_specific'];
		print_r("here are the metafileds " . $item_specific);
		// exit(0);
		$itemSpecificJson = json_decode($item_specific, true);
		foreach ($itemSpecificJson as $k => $v) {
			print_r($k);

			if ($k == "Dress Length") {
				$key = "dress_length";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Theme") {
				$key = "theme";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Fit") {
				$key = "fit";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Rise") {
				$key = "rise";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Waist Size") {
				$key = "waist_size";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Colour") {
				$key = "colour";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Brand") {
				$key = "brand_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Material") {
				$key = "material_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Closure") {
				$key = "closure_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Condition") {
				$key = "condition_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $condition_val, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Department") {
				$key = "department_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Style") {
				$key = "style";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Type") {
				$key = "type_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Size Type") {
				$key = "size_type_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Size") {
				$key = "size_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Sleeve Length") {
				$key = "sleeve_length_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Fabric Type") {
				$key = "fabric_type";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Pattern") {
				$key = "pattern_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Neckline") {
				$key = "neckline_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Graphic Print") {
				$key = "graphic_print_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "sc_attributes");
			}
			if ($k == "Front Type") {
				$key = "front_type";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Chest size") {
				$key = "chest_size_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Pocket Type") {
				$key = "pocket_type";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Strap Type") {
				$key = "strap_type";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Swim Bottom Style") {
				$key = "swim_bottom_style";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Garment Care") {
				$key = "garment_care";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Accent") {
				$key = "accent";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Character") {
				$key = "character";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Season") {
				$key = "season";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Skirt Length") {
				$key = "skirt_length";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Handle/Strap Colour") {
				$key = "handle_strap_colour";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Handle Style") {
				$key = "handle_style";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Exterior Material") {
				$key = "exterior_material";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Exterior Colour") {
				$key = "exterior_colour";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Bag Width") {
				$key = "bag_width";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Bag Heigh") {
				$key = "bag_heigh";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Upper Material") {
				$key = "upper_material";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "UK Shoe Size") {
				$key = "uk_shoe_size";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Toe Shape") {
				$key = "toe_shape";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Outsole Material") {
				$key = "outsole_material";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Outer Shell Material") {
				$key = "outer_shell_material";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Lining Material") {
				$key = "lining_material";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Jacket/Coat Length") {
				$key = "jacket_coat_length";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Leg Style") {
				$key = "leg_style";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Inside Leg") {
				$key = "inside_leg";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Distressed") {
				$key = "distressed";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Other Material") {
				$key = "other_material";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
			if ($k == "Collar To Hem") {
				$key = "collar_to_hem_";
				$value_type = "single_line_text_field";
				$metafields[] = array("key" => $key, "value" => $v, "type" => $value_type, "namespace" => "custom");
			}
		}
		$data["product"]["metafields"] = $metafields;
		print_r($data["product"]);
	}
  if ($user_id == 50223) {
    $conditionStrnew = "";
    $decodedDescription = json_decode( $productRow['description'], true);

    // Check if decoded successfully and is an array
    if (json_last_error() === JSON_ERROR_NONE && is_array($decodedDescription)) {
        print_r($decodedDescription); // Optional for debugging

        // Check if 'Manufacturer' key exists and is an array
        if (!empty($decodedDescription['Manufacturer']) && is_array($decodedDescription['Manufacturer'])) {
            $manufacturer = $decodedDescription['Manufacturer'];
            $conditionStrnew = "<br><h2>Informationen zur Produktsicherheit</h2>";

            if (!empty($manufacturer['CompanyName'])) {
                $conditionStrnew .= "<p><strong>CompanyName:</strong> " . htmlspecialchars($manufacturer['CompanyName']) . "</p>";
            }
            if (!empty($manufacturer['Street1'])) {
                $conditionStrnew .= "<p><strong>Street1:</strong> " . htmlspecialchars($manufacturer['Street1']) . "</p>";
            }
            if (!empty($manufacturer['Street2'])) {
                $conditionStrnew .= "<p><strong>Street2:</strong> " . htmlspecialchars($manufacturer['Street2']) . "</p>";
            }
            if (!empty($manufacturer['CityName'])) {
                $conditionStrnew .= "<p><strong>CityName:</strong> " . htmlspecialchars($manufacturer['CityName']) . "</p>";
            }
            if (!empty($manufacturer['PostalCode'])) {
                $conditionStrnew .= "<p><strong>PostalCode:</strong> " . htmlspecialchars($manufacturer['PostalCode']) . "</p>";
            }
            if (!empty($manufacturer['Country'])) {
                $conditionStrnew .= "<p><strong>Country:</strong> " . htmlspecialchars($manufacturer['Country']) . "</p>";
            }
            if (!empty($manufacturer['Email'])) {
                $conditionStrnew .= "<p><strong>Email:</strong> " . htmlspecialchars($manufacturer['Email']) . "</p>";
            }

            // Append to body_html
            $data["product"]['body_html'] .= $conditionStrnew;
        }
    }
}

	if ($user_id == 47617) { //

		$metafields = array();
		$item_specific = $productRow['item_specific'];
		$ebayitemid =  $productRow['ebayitemid'];
		$searchstring12 = $productRow['searchstring'];
		echo $searchstring12 . "\n";
		$category =    $productRow['product_type'];
		$condition_val = $productRow['condition_val'];
		$condition_note = $productRow['condition_note'];
		$title = $productRow['title'];
		$itemSpecificJson = json_decode($item_specific, true);

		if (isset($itemSpecificJson['Non-Domestic Product'])) {
			$NDP = $itemSpecificJson['Non-Domestic Product'];
			$metafields[] = array("key" => "ebay_non_domestic_product", "value" => $NDP, "type" => "single_line_text_field", "namespace" => "custom");
		}


		if (isset($itemSpecificJson['Brand'])) {
			$brand = $itemSpecificJson['Brand'];
			$metafields[] = array("key" => "ebay_brand", "value" => $brand, "type" => "single_line_text_field", "namespace" => "custom");
		}


		if (isset($itemSpecificJson['Type'])) {
			$type = $itemSpecificJson['Type'];
			$metafields[] = array("key" => "ebay_type", "value" => $type, "type" => "single_line_text_field", "namespace" => "custom");
		}

		if (isset($itemSpecificJson['Custom Bundle'])) {
			$Custom_Bundle = $itemSpecificJson['Custom Bundle'];
			$metafields[] = array("key" => "ebay_custom_bundle", "value" => $Custom_Bundle, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson['Modified Item'])) {
			$modified_item = $itemSpecificJson['Modified Item'];
			$metafields[] = array("key" => "ebay_modified_item", "value" => $modified_item, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson['Battery Size'])) {
			$battery_size = $itemSpecificJson['Battery Size'];
			$metafields[] = array("key" => "ebay_battery_size", "value" => $battery_size, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson['MPN'])) {
			$mpn = $itemSpecificJson['MPN'];
			$metafields[] = array("key" => "ebay_mpn", "value" => $mpn, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson['Voltage'])) {
			$voltage = $itemSpecificJson['Voltage'];
			$metafields[] = array("key" => "ebay_voltage", "value" => $voltage, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson['EAN'])) {
			$ean = $itemSpecificJson['EAN'];
			$metafields[] = array("key" => "ebay_ean", "value" => $ean, "type" => "single_line_text_field", "namespace" => "custom");
		}

		if (isset($itemSpecificJson['Chemical Composition'])) {
			$chemical_composition = $itemSpecificJson['Chemical Composition'];
			$metafields[] = array("key" => "ebay_chemical_composition", "value" => $chemical_composition, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson['Expiry Date'])) {
			$expiry_date = $itemSpecificJson['Expiry Date'];
			$metafields[] = array("key" => "ebay_expiry_date", "value" => $expiry_date, "type" => "single_line_text_field", "namespace" => "custom");
		}

		if (isset($itemSpecificJson["Coin\/Button Cell Sub-Type"])) {
			$coin_button_cell_sub_type = $itemSpecificJson["Coin\/Button Cell Sub-Type"];
			$metafields[] = array("key" => "ebay_coin_button_cell_sub_type", "value" => $coin_button_cell_sub_type, "type" => "single_line_text_field", "namespace" => "custom");
		}

		if (isset($searchstring12)) {

			$metafields[] = array("key" => "ebay_shipping_profile_name", "value" => $searchstring12, "type" => "single_line_text_field", "namespace" => "custom");
		}

		if (isset($ebayitemid)) {
			$ebay_link = 'https://www.ebay.co.uk/itm/' . $ebayitemid;
			$metafields[] = array("key" => "ebay_original_ebay_link", "value" => $ebay_link, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($condition_val)) {

			$metafields[] = array("key" => "ebay_condition", "value" => $condition_val, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Ideale Per:"])) {
			$Ideale = $itemSpecificJson["Ideale Per:"];
			$metafields[] = array("key" => "ebay_ideale_per", "value" => $Ideale, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Carte Opzione"])) {
			$Carte = $itemSpecificJson["Carte Opzione"];
			$metafields[] = array("key" => "ebay_carte_opzione", "value" => $Carte, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Marca"])) {
			$Marca = $itemSpecificJson["Marca"];
			$metafields[] = array("key" => "ebay_marca", "value" => $Marca, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Tipo."])) {
			$Tipo = $itemSpecificJson["Tipo."];
			$metafields[] = array("key" => "ebay_tipo", "value" => $Tipo, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Paese\/Regione di Produzione"])) {
			$Paese = $itemSpecificJson["Paese\/Regione di Produzione"];
			$metafields[] = array("key" => "ebay_paese_regione_di_produzione", "value" => $Paese, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Number of Piece"])) {
			$Piece11 = $itemSpecificJson["Number of Piece"];
			$metafields[] = array("key" => "ebay_number_of_piecese", "value" => $Piece11, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Material"])) {
			$Material = $itemSpecificJson["Material"];
			$metafields[] = array("key" => "ebay_material", "value" => $Material, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Age Level"])) {
			$Level = $itemSpecificJson["Age Level"];
			$metafields[] = array("key" => "ebay_age_level", "value" => $Level, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Personalisation Instructions"])) {
			$Instructions = $itemSpecificJson["Personalisation Instructions"];
			$metafields[] = array("key" => "ebay_personalise", "value" => $Instructions, "type" => "single_line_text_field", "namespace" => "custom");
		}

		if (isset($itemSpecificJson["Colour"])) {
			$Colour = $itemSpecificJson["Colour"];
			$metafields[] = array("key" => "ebay_colour", "value" => $Colour, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Award"])) {
			$Award = $itemSpecificJson["Award"];
			$metafields[] = array("key" => "ebay_award", "value" => $Award, "type" => "single_line_text_field", "namespace" => "custom");
		}

		if (isset($itemSpecificJson["Theme"])) {
			$Theme = $itemSpecificJson["Theme"];
			$metafields[] = array("key" => "ebay_theme", "value" => $Theme, "type" => "single_line_text_field", "namespace" => "custom");
		}

		if (isset($itemSpecificJson["Features"])) {
			$Features = $itemSpecificJson["Features"];
			$metafields[] = array("key" => "ebay_features", "value" => $Features, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Recommended Age Range"])) {
			$Range = $itemSpecificJson["Recommended Age Range"];
			$metafields[] = array("key" => "ebay_recommended_age_range", "value" => $Range, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Country\/Region of Manufacture"])) {
			$Country = $itemSpecificJson["Country\/Region of Manufacture"];
			$metafields[] = array("key" => "ebay_country_region_of_manufacture", "value" => $Country, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Character Family"])) {
			$Family = $itemSpecificJson["Character Family"];
			$metafields[] = array("key" => "ebay_character_family", "value" => $Family, "type" => "single_line_text_field", "namespace" => "custom");
		}
		if (isset($itemSpecificJson["Personalise"])) {
			$Personalise = $itemSpecificJson["Personalise"];
			$metafields[] = array("key" => "ebay_personalise", "value" => $Personalise, "type" => "single_line_text_field", "namespace" => "custom");
		}




		$data["product"]["metafields"] = $metafields;
		//  print_r($data);
		//exit(0);
	}
	if ($user_id == 48102) {

		$metafields = array();
		$metafields[] = array("key" => "snowboard_length", "value" => "{\"value\":1.0,\"unit\":\"mm\"}", "type" => "dimension", "namespace" => "test_data");
		$data["product"]["metafields"] = $metafields;
	}

	addlog("productcreate response" . json_encode($data, true), "INFO");
	$imageArr_new = array();
	print_r("img" . json_encode(array_unique($imageArr)));
	$ismultiimage = array_unique($imageArr);
	if (count($ismultiimage) === 1) {
		$imageArr_new[] = array("alt" => "ep01", "mediaContentType" => "IMAGE", "originalSource" => trim($ismultiimage[0]));
	}
	$imgSecondaryArr = array_unique($imgSecondaryArr);
	foreach ($imgSecondaryArr as $key => $v) {
		$imageArr_new[] = array("alt" => "$key", "mediaContentType" => "IMAGE", "originalSource" => trim(preg_replace('/\?.*/', '', $v)));
	}
	
	$option1valall = array();
	$option2valall = array();
	$option3valall = array();
	if (!empty($data["product"]['options'][0])) {

		$option1valall = [
			"name" => $data["product"]['options'][0]['name'],
			"position" => 1,
			"values" => ["name" => $data["product"]['variants'][0]['option1']],
		];
	}
	if (!empty($data["product"]['options'][1])) {

		$option2valall = [
			"name" => $data["product"]['options'][1]['name'],
			"position" => 2,
			"values" => ["name" => $data["product"]['variants'][0]['option2']],
		];
	}
	if (!empty($data["product"]['options'][2])) {

		$option3valall = [
			"name" => $data["product"]['options'][2]['name'],
			"position" => 3,
			"values" => ["name" => $data["product"]['variants'][0]['option3']],
		];
	}

		$jsonstring = [
		"input" => [
			"title" => $data["product"]['title'],
			'descriptionHtml' => $data["product"]['body_html'],
			'vendor' => $data["product"]['vendor'],
			"tags" => $data["product"]['tags'],
			"status" => strtoupper($data["product"]['status']),
			'productType' => $data["product"]['product_type'],
			'published' => true,
		],
		"media" => $imageArr_new
	];
	
	if (isset($data["product"]['metafields'])) {
		$jsonstring["input"]['metafields'] = $data["product"]['metafields'];
	}
	$productOptions = [];
	if (!empty($option1valall)) $productOptions[] = $option1valall;
	if (!empty($option2valall)) $productOptions[] = $option2valall;
	if (!empty($option3valall)) $productOptions[] = $option3valall;
	if (!empty($productOptions)) {
		$jsonstring['input']['productOptions'] = $productOptions;
	}

	print_r($jsonstring);
	$response = addShopifyProduct_graphql($token, $shopurl, $jsonstring);
	sleep(1);
	$responseDecoded = $response;
	addlog("check response addShopifyProduct: ", "INFO");
	addlog("productcreate response" . json_encode($response, true), "INFO");
	print_r($response);
	if (empty($responseDecoded['data']['productCreate']['userErrors'])) {
		echo 'in if for updated variant';
		print_r($responseDecoded['data']['productCreate']['product']['id']);
		$gid_shopifyproductid = $responseDecoded['data']['productCreate']['product']['id'];
		print_r($shopifyproductidG);
		$shopifyproductid = str_replace("gid://shopify/Product/", "", $gid_shopifyproductid);
	// if($shopifyproductid !== ''){
		$shopifyvariantidG = $responseDecoded['data']['productCreate']['product']['variants']['nodes'][0]['id'];
		$shopifyvariantid = str_replace("gid://shopify/ProductVariant/", "", $shopifyvariantidG);
		$shopifyinventoryidG = $responseDecoded['data']['productCreate']['product']['variants']['nodes'][0]['inventoryItem']['id'];
		$shopifyinventoryid = str_replace("gid://shopify/InventoryItem/", "", $shopifyinventoryidG);
		$location_id=$responseDecoded['data']['productCreate']['product']['variants']['nodes'][0]['inventoryItem']['inventoryLevels']['nodes'][0]['location']['id'];
		$location_id=str_replace("gid://shopify/Location/", "", $location_id);
		echo "LOCATION ID IS  ".$location_id." \n";
		$option1 = "";
		$option2 = "";
		$option3 = "";
		$whereQuery = "";
		if (isset($data["product"]['variants'][0]['option1'])) {
			$option1 = $data["product"]['variants'][0]['option1'];
			$imgskuArr[] = trim($option1);
			$whereQuery .= " and option1val = '" . mysqli_real_escape_string($conn, $option1) . "' ";
		}
		if (isset($data["product"]['variants'][0]['option2'])) {
			$option2 = $data["product"]['variants'][0]['option2'];
			$imgskuArr[] = trim($option2);
			$whereQuery .= " and option2val = '" . mysqli_real_escape_string($conn, $option2) . "' ";
		}
		if (isset($data["product"]['variants'][0]['option3'])) {
			$option3 = $data["product"]['variants'][0]['option3'];
			$imgskuArr[] = trim($option3);
			$whereQuery .= " and option3val = '" . mysqli_real_escape_string($conn, $option3) . "' ";
		}
		$conn->query("update products set shopifyproductid = '" . mysqli_real_escape_string($conn, $shopifyproductid) . "', status = 'Imported', newflag = 0, updated_at = now() where user_id = '" . $user_id . "' and product_id = " . $product_id);

		$conn->query("update product_variants set shopifyproductid = '" . mysqli_real_escape_string($conn, $shopifyproductid) . "', shopifyvariantid = '" . mysqli_real_escape_string($conn, $shopifyvariantid) . "', shopifyinventoryid = '" . mysqli_real_escape_string($conn, $shopifyinventoryid) . "', shopifylocationid = '" . mysqli_real_escape_string($conn, $location_id) . "', status = 'Imported', newflag = 0, updated_at = now() where user_id = '" . $user_id . "' and product_id = " . $product_id . " " . $whereQuery);

		$conn->query("insert into shopifyproducts(user_id, productid, variantid, sku, dateofmodification) values ('" . $user_id . "', '" . mysqli_real_escape_string($conn, $shopifyproductid) . "', '" . mysqli_real_escape_string($conn, $shopifyvariantid) . "', '" . mysqli_real_escape_string($conn, $sku) . "', now())");
		addlog("productid " . $shopifyproductid, "INFO");
		$weight_unitnew = $data['product']['variants'][0]['weight_unit'];
		if (isset($weight_unitnew) && $weight > 0) {
			$weight_unitnew = convertWeightUnitToFullForm($weight_unitnew);
		} else {
			$weight = 0;
			$weight_unitnew = "POUNDS";
		}
		if ($user_id == 49161) {
			$weight_unitnew = "POUNDS";
		}
		if ($user_id == 48805) {
			$weight_unitnew = "KILOGRAMS";
		}
		$track =  true;
		if ($data['product']['variants'][0]['inventory_policy'] == "continue") {
			$track = false;
		}
		$variantVariables = '';
		if ($data['product']['variants'][0]['imgurl'] != '' && count($ismultiimage) != 1) {
			$image_url = preg_replace('/\?.*/', '', $data['product']['variants'][0]['imgurl']);
			$variantVariables = [
				"productId" => "gid://shopify/Product/$shopifyproductid",
				"media" => [
					"alt" => "epi" . $data['product']['variants'][0]['barcode'],
					"mediaContentType" => "IMAGE",
					"originalSource" =>  $image_url,
				],
				"variants" => [
					[
						"id" => "gid://shopify/ProductVariant/$shopifyvariantid",

						"barcode" => $data['product']['variants'][0]['barcode'],
						"inventoryItem" => [
							"measurement" => [
								"weight" => [
									"unit" =>  $weight_unitnew,
									"value" => (float)$data['product']['variants'][0]['weight'],
								],
							],
							"requiresShipping" => true,
							"sku" => $data['product']['variants'][0]['sku'],
							"tracked" => true,
						],
						"inventoryPolicy" => strtoupper($data['product']['variants'][0]['inventory_policy']),
						"price" => $data['product']['variants'][0]['price'],
						"taxable" =>  $data['product']['variants'][0]['taxable'],
						"mediaSrc" => $image_url,
					],
				],
			];
		} else {
			$variantVariables = [
				"productId" => "gid://shopify/Product/$shopifyproductid",

				"variants" => [
					[
						"id" => "gid://shopify/ProductVariant/$shopifyvariantid",

						"barcode" => $data['product']['variants'][0]['barcode'],
						"inventoryItem" => [
							"measurement" => [
								"weight" => [
									"unit" =>  $weight_unitnew,
									"value" => (float)$data['product']['variants'][0]['weight'],
								],
							],
							"requiresShipping" => true,
							"sku" => $data['product']['variants'][0]['sku'],
							"tracked" => true,
						],
						"inventoryPolicy" => strtoupper($data['product']['variants'][0]['inventory_policy']),
						"price" => $data['product']['variants'][0]['price'],
						"taxable" =>  $data['product']['variants'][0]['taxable'],

					],
				],
			];
		}
		addlog("processto uplocat v data" . json_encode($variantVariables, true), "INFO");
		$res = updateShopifyVariant_graphql($token, $shopurl, $variantVariables);
		sleep(1);
		print_r($res);
		addlog("processto uplocat v data response" . json_encode($res, true), "INFO");
		if ($res) {
			addlog("updateShopifyVariant id true ", "INFO");
			$variables = [
				"input" => [
					"ignoreCompareQuantity" => true,
					"name" => "available",
					"reason" => "correction",
					"quantities" => [
						[
							"inventoryItemId" =>  "gid://shopify/InventoryItem/$shopifyinventoryid",
							"locationId" => "gid://shopify/Location/$location_id",
							"quantity" => (int)$data['product']['variants'][0]['quantity']
						]
					]
				]
			];
			addlog("updateShopifyInventory_graphql ", "INFO");
			addlog("updateinve data" . json_encode($variables, true), "INFO");
			sleep(2);
			$USI = updateShopifyInventory_graphql($token, $shopurl, $variables);
			sleep(1);
			print_r($USI);
			addlog("processto uplocat invdata response" . json_encode($USI, true), "INFO");
			if ($USI) {
				// Remove the first variant and re-index the array
				unset($data["product"]['variants'][0]);
				$data["product"]['variants'] = array_values($data["product"]['variants']);
				print_r($data);
				$variantArray = [];
				$media = [];
				foreach ($data["product"]['variants'] as $key => $variant) {
					$productOptions = [];

					// Handle Option 1
					if (!empty($data["product"]['options'][0])) {
						$productOptions[] = [
							"name" => $variant['option1'],
							"optionName" => $data["product"]['options'][0]['name'],
						];
					}

					// Handle Option 2
					if (!empty($data["product"]['options'][1])) {
						$productOptions[] = [
							"name" => $variant['option2'],
							"optionName" => $data["product"]['options'][1]['name'],
						];
					}

					// Handle Option 3
					if (!empty($data["product"]['options'][2])) {
						$productOptions[] = [
							"name" => $variant['option3'],
							"optionName" => $data["product"]['options'][2]['name'],
						];
					}

					$track =  true;
					if ($variant['inventory_policy'] == "continue") {
						$track = false;
					}
					// Convert weight unit to full form or set defaults
					$weight = isset($variant['weight']) ? (float)$variant['weight'] : 0;
					$weightUnit = !empty($variant['weight_unit']) && $weight > 0
						? convertWeightUnitToFullForm($variant['weight_unit'])
						: "POUNDS";
					if ($user_id == 49161) {
						$weightUnit = "POUNDS";
					}
					if ($user_id == 48805) {
							$weight_unitnew = "KILOGRAMS";
						}
					// Build variant array
					if ($variant['imgurl'] != ""  && count($ismultiimage) != 1) {
						$image_url = preg_replace('/\?.*/', '', $variant['imgurl']);
						$variantArray[] = [
							'optionValues' => $productOptions,
							'price' => (float)$variant['price'],
							'inventoryQuantities' => [
								'availableQuantity' => (int)$variant['quantity'],
								'locationId' => 'gid://shopify/Location/' . $location_id,
							],

							'barcode' => $variant['barcode'],
							'inventoryItem' => [
								'measurement' => [
									'weight' => [
										'unit' => $weightUnit,
										'value' => $weight,
									],
								],
								'sku' => $variant['sku'],
								'tracked' => true,
								'requiresShipping' => true,
							],
							'inventoryPolicy' => "DENY",
							'mediaSrc' => $image_url,
						];

						$media[] = [
							"alt" => "epi" . $key,
							"mediaContentType" => "IMAGE",
							"originalSource" => $image_url,
						];
					} else {
						$variantArray[] = [
							'optionValues' => $productOptions,
							'price' => (float)$variant['price'],
							'inventoryQuantities' => [
								'availableQuantity' => (int)$variant['quantity'],
								'locationId' => 'gid://shopify/Location/' . $location_id,
							],

							'barcode' => $variant['barcode'],
							'inventoryItem' => [
								'measurement' => [
									'weight' => [
										'unit' => $weightUnit,
										'value' => $weight,
									],
								],
								'sku' => $variant['sku'],
								'tracked' => true,
								'requiresShipping' => true,
							],
							'inventoryPolicy' => "DENY",

						];
					}
				}
				$jsonVariantsPayload = '';
				// Prepare the JSON payload for Shopify variants
				if (isset($media)) {
					$jsonVariantsPayload = [
						"media" => $media,
						"productId" => "gid://shopify/Product/$shopifyproductid",
						"variants" => $variantArray,

					];
				} else {
					$jsonVariantsPayload = [
						"productId" => "gid://shopify/Product/$shopifyproductid",
						"variants" => $variantArray,
					];
				}

				print_r($jsonVariantsPayload);
				// Create Shopify variants
				$allRemainingVariants = createShopifyVaraint_graphql($shopurl, $token, $jsonVariantsPayload);
				sleep(1);
				print_r($allRemainingVariants);
				addlog("bulkcreatevariants response  " . json_encode($allRemainingVariants), "INFO");
				$productVariants = $allRemainingVariants['data']['productVariantsBulkCreate']['productVariants'];
				addlog("bulkcreatevariants productVariants response  " . json_encode($productVariants), "INFO");
				foreach ($productVariants as $variantnew) {
					print_r($variantnew);
					addlog("bulkcreatevariantssingle productVariants response  " . json_encode($variantnew), "INFO");
					$gid_inventoryItemId = $variantnew['inventoryItem']['id'];
					$gid_shopifyvariantid = $variantnew['id'];
					$shopifyvariantid = str_replace("gid://shopify/ProductVariant/", "", $gid_shopifyvariantid);
					$shopifyinventoryid = str_replace("gid://shopify/InventoryItem/", "", $gid_inventoryItemId);

					$option1 = "";
					$option2 = "";
					$option3 = "";
					$whereQuery = "";
					$selectedOptions = $variantnew['selectedOptions'];
					if (isset($variantnew['selectedOptions'][0]['value'])) {
						$option1 = $variantnew['selectedOptions'][0]['value'];
						$whereQuery .= " and option1val = '" . mysqli_real_escape_string($conn, $option1) . "' ";
					}
					if (isset($variantnew['selectedOptions'][1]['value'])) {
						$option2 = $variantnew['selectedOptions'][1]['value'];
						$whereQuery .= " and option2val = '" . mysqli_real_escape_string($conn, $option2) . "' ";
					}
					if (isset($variantnew['selectedOptions'][2]['value'])) {
						$option3 = $variantnew['selectedOptions'][2]['value'];
						$whereQuery .= " and option3val = '" . mysqli_real_escape_string($conn, $option3) . "' ";
					}
					///
					echo "UPDATE product_variants SET 
										shopifyproductid = '" . mysqli_real_escape_string($conn, $shopifyproductid) . "', 
										shopifyvariantid = '" . mysqli_real_escape_string($conn, $shopifyvariantid) . "', 
										shopifyinventoryid = '" . mysqli_real_escape_string($conn, $shopifyinventoryid) . "', 
										shopifylocationid = '" . mysqli_real_escape_string($conn, $shopifylocationid) . "', 
										status = 'Imported', 
										newflag = 0, 
										updated_at = now() 
									WHERE user_id = '" . $user_id . "' AND product_id = " . $product_id . " " . $whereQuery;
					// Update the product variants
					$conn->query("UPDATE product_variants SET 
					shopifyproductid = '" . mysqli_real_escape_string($conn, $shopifyproductid) . "', 
					shopifyvariantid = '" . mysqli_real_escape_string($conn, $shopifyvariantid) . "', 
					shopifyinventoryid = '" . mysqli_real_escape_string($conn, $shopifyinventoryid) . "', 
					shopifylocationid = '" . mysqli_real_escape_string($conn, $location_id) . "', 
					status = 'Imported', 
					newflag = 0, 
					updated_at = now() 
				  WHERE user_id = '" . $user_id . "' AND product_id = " . $product_id . " " . $whereQuery);

					// Insert into shopifyproducts
					$conn->query("INSERT INTO shopifyproducts(user_id, productid, variantid, gid_shopifyproductid, gid_shopifyvariantid, dateofmodification) 
									VALUES ('" . $user_id . "', '" . mysqli_real_escape_string($conn, $shopifyproductid) . "', '" . mysqli_real_escape_string($conn, $shopifyvariantid) . "', 
											'" . mysqli_real_escape_string($conn, $gid_shopifyproductid) . "', '" . mysqli_real_escape_string($conn, $gid_shopifyvariantid) . "', NOW())");
					addlog("bulkcreatevariantssingle productVariants response comleted  ", "INFO");
				}
			}
		}
		if ($user_id == 48043) {

             $variables = [
                  "id" => $gid_shopifyproductid,
                  "input" => [
                   [ "publicationId" =>  "gid://shopify/Publication/117633089651" ],
                   [ "publicationId" => "gid://shopify/Publication/117633187955" ],
                   [ "publicationId" => "gid://shopify/Publication/118559309939" ]
                  ],
                ];
           
                publishedonallchannelnew($user_id, $token, $shopurl, $gid_shopifyproductid, $variables);

			// $allchannel = array("gid://shopify/Publication/117633089651", "gid://shopify/Publication/117633187955", " gid://shopify/Publication/118559309939");
			// foreach ($allchannel  as $published) {
			// 	publishedonallchannel($user_id, $token, $shopurl, $gid_shopifyproductid, $published);
			// }
		}
		if ($user_id == 46190) {

			 $variables = [
                  "id" => $gid_shopifyproductid,
                  "input" => [
                   [ "publicationId" =>  "gid://shopify/Publication/107722571984" ],
                   [ "publicationId" => "gid://shopify/Publication/107722539216" ],
                   [ "publicationId" => "gid://shopify/Publication/107722473680" ]
                  ],
                ];
           
                publishedonallchannelnew($user_id, $token, $shopurl, $gid_shopifyproductid, $variables);
			// $allchannel = array("gid://shopify/Publication/107722571984", "gid://shopify/Publication/107722539216", "gid://shopify/Publication/107722473680");
			// foreach ($allchannel  as $published) {

			// 	publishedonallchannel($user_id, $token, $shopurl, $gid_shopifyproductid, $published);
			// }
		}
	// }else{
	// 	echo 'shopify productid is empty come';
	// 	$conn->query("update products set status = 'error',  errdetails = 'shopify productid is empty', updated_at = now() where user_id = '" . $user_id . "' and product_id = " . $product_id);
	
	// }	
	}else {
		$conn->query("update products set status = 'error', updated_at = now() where user_id = '" . $user_id . "' and product_id = " . $product_id);
	}
	return true ;
}



function publishedonallchannelnew($user_id, $token, $shopurl, $shopifyproductid, $publishedid)
{
    $query = <<<GQL
            mutation publishablePublish(\$id: ID!, \$input: [PublicationInput!]!) {
                publishablePublish(id: \$id, input: \$input) {
                publishable {
                    availablePublicationsCount {
                    count
                    }
                    resourcePublicationsCount {
                    count
                    }
                }
                shop {
                    publicationCount
                }
                userErrors {
                    field
                    message
                }
                }
            }
            GQL;
   
    $jsondata =  ["query" => $query, "variables" => $publishedid];
    $url = "https://$shopurl/admin/api/2024-10/graphql.json"; // Update the version if needed
 
    // cURL setup
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        "Content-Type: application/json",
        "X-Shopify-Access-Token:  $token",
    ]);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($jsondata));
 
    // Execute the request
    $response = curl_exec($ch);
    sleep(1);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
 
    // Handle the response
    if ($httpCode === 200) {
        $responseData = json_decode($response, true);
		echo 'hitesh mali ';
        print_r($responseData);
        return  $responseData;
    } else {
        echo "HTTP Error Code: $httpCode\n";
        echo "Response: $response\n";
    }
}


function publishedonallchannel($user_id, $token, $shopurl, $shopifyproductid, $publishedid)
{
	$query = <<<GQL
			mutation publishablePublish(\$id: ID!, \$input: [PublicationInput!]!) {
				publishablePublish(id: \$id, input: \$input) {
				publishable {
					availablePublicationsCount {
					count
					}
					resourcePublicationsCount {
					count
					}
				}
				shop {
					publicationCount
				}
				userErrors {
					field
					message
				}
				}
			}
			GQL;
	$variables = [
		"id" => $shopifyproductid,
		"input" => [
			"publicationId" => $publishedid,

		],
	];
	$jsondata =  ["query" => $query, "variables" => $variables];
	$url = "https://$shopurl/admin/api/2024-10/graphql.json"; // Update the version if needed

	// cURL setup
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_HTTPHEADER, [
		"Content-Type: application/json",
		"X-Shopify-Access-Token:  $token",
	]);
	curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($jsondata));

	// Execute the request
	$response = curl_exec($ch);
	sleep(1);
	$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	curl_close($ch);

	// Handle the response
	if ($httpCode === 200) {
		$responseData = json_decode($response, true);
		print_r($responseData);
		return  $responseData;
	} else {
		echo "HTTP Error Code: $httpCode\n";
		echo "Response: $response\n";
	}
}
function mapItemAttributes($title, $tags,$metafields) {
    $titleLower = strtolower($title);
    // $tags = [];
    // $metafields = [];

    // Utility function to add tags and metafields
    $addMapping = function($prefix, $key, $value) use (&$tags, &$metafields) {
        $tag = $prefix . "_" . str_replace(" ", "_", $value);
        if (!in_array($tag, $tags)) {
            $tags[] = $tag;
            $metafields[] = [
                "namespace" => "custom",
                "key" => strtolower($key),
                "type" => "single_line_text_field",
                "value" => str_replace("_", " ", $value)
            ];
        }
    };

    // ----------------- 1) Fuel Type -----------------
    if (preg_match('/\bpetrol\b/i', $title)) {
        $addMapping("FuelType", "fuel_type", "Petrol");
    } elseif (preg_match('/\bdiesel\b/i', $title)) {
        $addMapping("FuelType", "fuel_type", "Diesel");
    } elseif (preg_match('/\bhybrid\b/i', $title)) {
        $addMapping("FuelType", "fuel_type", "Hybrid");
    } elseif (preg_match('/\bnon[- ]?hybrid\b/i', $title)) {
        $addMapping("FuelType", "fuel_type", "Non_Hybrid");
    }

    // ----------------- 2) Transmission Type -----------------
    if (preg_match('/\b(auto|automatic)\b/i', $title)) {
        $addMapping("TransmissionType", "transmission_type", "Auto");
    } elseif (preg_match('/\bmanual\b/i', $title)) {
        $addMapping("TransmissionType", "transmission_type", "Manual");
    }

    // ----------------- 3) Drive Type -----------------
    if (preg_match('/\b4x4\b/i', $title)) {
        $addMapping("DriveType", "drive_type", "4x4");
    } elseif (preg_match('/\bawd\b/i', $title)) {
        $addMapping("DriveType", "drive_type", "AWD");
    } elseif (preg_match('/\brwd\b/i', $title)) {
        $addMapping("DriveType", "drive_type", "RWD");
    } elseif (preg_match('/\bfwd\b/i', $title)) {
        $addMapping("DriveType", "drive_type", "FWD");
    }

    // ----------------- 4) Side -----------------
    $left  = preg_match('/\bleft\b/i', $title);
    $right = preg_match('/\bright\b/i', $title);
    $front = preg_match('/\bfront\b/i', $title);
    $rear  = preg_match('/\brear\b/i', $title);
    $centre = preg_match('/\bcentre\b/i', $title);

    if ($left && $front) {
        $addMapping("Side", "side", "Left_Front");
    } elseif ($right && $front) {
        $addMapping("Side", "side", "Right_Front");
    } elseif ($left && $rear) {
        $addMapping("Side", "side", "Left_Rear");
    } elseif ($right && $rear) {
        $addMapping("Side", "side", "Right_Rear");
    } elseif ($left) {
        $addMapping("Side", "side", "Left");
    } elseif ($right) {
        $addMapping("Side", "side", "Right");
    } elseif ($front) {
        $addMapping("Side", "side", "Front");
    } elseif ($rear) {
        $addMapping("Side", "side", "Rear");
    } elseif ($centre) {
        $addMapping("Side", "side", "Centre");
    }

    // ----------------- 5) Body Type -----------------
    $bodyTypes = [
        "Bus", "Cab Chassis", "Convertible", "Coupe", "Hatch", "Light Truck",
        "People Mover", "Sedan", "SUV", "Ute", "Van", "Wagon"
    ];

    foreach ($bodyTypes as $bt) {
        $pattern = '/\b' . preg_quote(strtolower($bt), '/') . '\b/i';
        if (preg_match($pattern, $titleLower)) {
            $addMapping("BodyType", "body_type", str_replace(" ", "_", $bt));
            break;
        }
    }
    print_r($tags);
    print_r($metafields);
    return ["tags" => $tags, "metafields" => $metafields];
}
function getExistingSKUs($user_id)
{
	global $conn;
	$existingSKUs = array();
	/*$result = $conn->query("select * from shopifyproducts where user_id = ".$user_id);
		if ($result->num_rows > 0) {
			while($row = $result->fetch_assoc()) {	
				$existingSKUs[] = $row['sku'];
			}
		}*/
	return $existingSKUs;
}

function getProdDescription($user_id, $product_id)
{
	global $conn;
	$description = '';
	//return $description;
	/*if($user_id == 8236){
		    return $description;
		}*/
	$result = $conn->query("select description from product_description where product_id = " . $product_id . " and user_id = " . $user_id);
	if ($result->num_rows > 0) {
		while ($row = $result->fetch_assoc()) {
			$description = $row['description'];
		}
	}
	return $description;
}

function getProdDescriptionNew($user_id, $ebayitemid, $ebtoken, $siteid)
{
	global $conn, $ebConfig;
	$description = '';
	try {
		$service = new Services\TradingService([
			'credentials' => $ebConfig['production']['credentials'],
			'sandbox'     => false,
			'siteId'      => $siteid
		]);
		$itemRequest = new Types\GetItemRequestType();
		$itemRequest->RequesterCredentials = new Types\CustomSecurityHeaderType();
		$itemRequest->RequesterCredentials->eBayAuthToken = $ebtoken;
		$itemRequest->ItemID = $ebayitemid;
		$itemRequest->DetailLevel = ['ReturnAll'];
		$itemRequest->IncludeItemCompatibilityList = true;
		$itemRequest->IncludeItemSpecifics = true;
		$itemResponse = $service->getItem($itemRequest);
		if ($itemResponse->Ack !== 'Failure') {
			if (!isset($itemResponse->Item)) {
				$description = '';
			}
			$item = $itemResponse->Item;
			$description = $item->Description;
		}
	} catch (Exception $e) {
	}
	return $description;
}

// Start adding functions to handle multiple location
function getMainLocation($user_id, $shopurl, $token)
{
	global $conn;
	// $result = $conn->query("select * from locations where legacy = 0 and user_id = " . $user_id . " order by shopifylocationid * 1");
	// if ($result->num_rows > 0) {
	// 	$row = $result->fetch_assoc();
	// 	$location_id = $row['shopifylocationid'];
	// 	return $location_id;
	// } else {
		// Try to fetch all possible locations
		$location_id = fetchLocations($user_id, $shopurl, $token);
		if ($location_id) {
			return $location_id;
		}
	//}
	return false;
}

function fetchLocations($user_id, $shopurl, $token)
{
	global $conn;
	$existingLocations = array();
	// $result = $conn->query("select * from locations where user_id = " . $user_id);
	// if ($result->num_rows > 0) {
	// 	while ($row = $result->fetch_assoc()) {
	// 		$existingLocations[] = $row['shopifylocationid'];
	// 	}
	// }
	$apiUrl = "https://$shopurl/admin/api/".GRAPHQL_API_VERSION."/graphql.json";


	$query = <<<QUERY
			{
			locations(first: 10) {
				edges {
				node {
					id
					name
					legacyResourceId
				}
				}
			}
			}
			QUERY;


	$headers = [
		"Content-Type: application/json",
		"X-Shopify-Access-Token: $token"
	];

	$postData = json_encode(['query' => $query]);

	$ch = curl_init($apiUrl);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

	$response = curl_exec($ch);
	$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	curl_close($ch);

	if ($httpCode !== 200) {
		return "Error: Unable to fetch data. HTTP Status Code: $httpCode";
	}

	$data = json_decode($response, true);

	print_r($data);
	$locations = $data['data']['locations']['edges'] ?? [];

	foreach ($locations as $location) {
		$name = $location['node']['name'];



		preg_match('/\d+$/', $location['node']['id'], $matches);

		$locationId = $matches[0];

		echo $locationId;

		$shopifylocationid = $locationId;

		echo $shopifylocationid;

		if (in_array($shopifylocationid, $existingLocations)) {
			continue;
		}
		$legacy = 0;
		if ($location['node']['legacyResourceId']) {
			$legacy = 1;
		}
		$conn->query("insert into locations(name, legacy, status, shopifylocationid, user_id, created_at, updated_at) values('" . mysqli_real_escape_string($conn, $name) . "', " . $legacy . ", 'active', " . mysqli_real_escape_string($conn, $shopifylocationid) . ", " . $user_id . ", now(), now())");
		$existingLocations[] = $shopifylocationid;
	}
	if (count($existingLocations) > 0) {

		//sort($existingLocations, SORT_NUMERIC);
		return $existingLocations[0];
	} else {
		return false;
	}
}
function applyMarkup($price)
{
	$newprice = $price;
	if ($price < 50) {
		$newprice = $price * 0.904;
	} else if ($price < 100) {
		$newprice = $price * 0.906;
	} else if ($price < 250) {
		$newprice = $price * 0.911;
	} else if ($price < 500) {
		$newprice = $price * 0.925;
	} else if ($price < 1000) {
		$newprice = $price * 0.928;
	} else if ($price < 2000) {
		$newprice = $price * 0.9425;
	} else if ($price < 3000) {
		$newprice = $price * 0.949;
	} else if ($price < 6000) {
		$newprice = $price * 0.9545;
	} else if ($price < 999999) {
		$newprice = $price * 0.959;
	}
	if ($price > 100) {
		$newprice = ceil($newprice);
	}
	return number_format($newprice, 2, ".", "");
}

function processConditionText($inputText)
{
	$outputArr = array();
	$titlesuffix = "";
	$conditiontag = "";
	if ($inputText == "Used") {
		$titlesuffix = "Used";
		$conditiontag = "cond-used";
	} else if ($inputText == "Seller refurbished") {
		$titlesuffix = "Refurbished";
		$conditiontag = "cond-refurbished";
	} else if ($inputText == "New other (see details)") {
		$titlesuffix = "New";
		$conditiontag = "cond-new";
	} else if ($inputText == "Pre-owned") {
		$titlesuffix = "Used";
		$conditiontag = "cond-used";
	} else if ($inputText == "New") {
		$titlesuffix = "New";
		$conditiontag = "cond-new";
	} else if ($inputText == "For parts or not working") {
		$titlesuffix = "For Parts";
		$conditiontag = "cond-parts";
	} else if ($inputText == "Manufacturer refurbished") {
		$titlesuffix = "Refurbished";
		$conditiontag = "cond-refurbished";
	}
	if ($titlesuffix != "") {
		$outputArr['titlesuffix'] = $titlesuffix;
	}
	if ($conditiontag != "") {
		$outputArr['conditiontag'] = $conditiontag;
	}
	return $outputArr;
}

function utf8_fopen_read($fileName)
{
	$fc = iconv('windows-1250', 'utf-8', file_get_contents($fileName));
	$handle = fopen("php://memory", "rw");
	fwrite($handle, $fc);
	fseek($handle, 0);
	return $handle;
}

function str_replace_n($search, $replace, $subject, $occurrence)
{
	$search = preg_quote($search);
	return preg_replace("/^((?:(?:.*?$search){" . --$occurrence . "}.*?))$search/", "$1$replace", $subject);
}

function slugify($text)
{
	// Strip html tags
	$text = strip_tags($text);
	// Replace non letter or digits by -
	$text = preg_replace('~[^\pL\d]+~u', '_', $text);
	// Transliterate
	setlocale(LC_ALL, 'en_US.utf8');
	$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
	// Remove unwanted characters
	$text = preg_replace('~[^-\w]+~', '', $text);
	// Trim
	$text = trim($text, '-');
	// Remove duplicate -
	$text = preg_replace('~-+~', '-', $text);
	// Lowercase
	$text = strtolower($text);
	// Check if it is empty
	if (empty($text)) {
		return 'n-a';
	}
	// Return result
	return $text;
}


function RemoveSpecialChar($str)
{

	// Using str_replace() function
	// to replace the word
	$res = str_replace(array(
		'\'',
		'"',
		',',
		';',
		'<',
		'/'
	), ' ', $str);

	// Returning the result
	return $res;
}


function addlog($message, $type)
{
	global $logfile;
	$txt = date("Y-m-d H:i:s") . " [" . $type . "]: " . $message . "\n";
	fwrite($logfile, $txt);
}
addlog("Execution Finished", "INFO");
fclose($logfile);
