<?php 

set_time_limit(0);
ini_set('memory_limit', '-1');
require_once("includes/config.php");
$user_id =561;
$result12 = $conn->query("select * from users where  id = 561 ");
if ($result12->num_rows > 0) {// 
	while($row12 = $result12->fetch_assoc()) {
$id = $row12['user_id'];
		$shopurl = $row12['shopurl'];
		$token = $row12['token'];		
		$shop_id = $row12['shop_id'];

        $result = $conn->query("select * from product_variants where shopifyproductid != '' and quantity = 0 and user_id = ".$user_id);
        if($result->num_rows > 0){
            while($row = $result->fetch_assoc()){
                $shopifyproductid = $row['shopifyproductid'];
                 $pid =  $row['product_id'];
                $data = array(
                    "product"=>array(
                        "id" => $shopifyproductid,		
                        "status" => 'draft'
                    )
                );
                print_r($data);
                updateShopifyproduct($token, $shopurl, $shopifyproductid, $data,$user_id);
              
                echo "updated".$shopifyproductid;
            }
        }
    }

}
function updateShopifyproduct($token, $shopurl, $shopifyproductid, $data,$user_id){
	global $conn;
	$url = "https://".$shopurl."/admin/api/2023-04/products/".$shopifyproductid.".json";
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-Shopify-Access-Token:'.$token, 'Content-Type: application/json; charset=utf-8'));
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_VERBOSE, 0);
	curl_setopt($curl, CURLOPT_HEADER, 1);
	curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
	curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	$response = curl_exec ($curl);
	curl_close ($curl);
    print_r($response);
	$response_arr = explode("\n",$response);	
	$climit = -1;
	foreach($response_arr as $obj){
		if (strpos($obj, 'X-Shopify-Shop-Api-Call-Limit') !== false) {
			$tempArr = explode(":", $obj);
			$climit = substr(trim(end($tempArr)), 0, -3);
		}
	}	
	if(intval($climit) > 35){
		sleep(5);
	} else {
	    sleep(1);
	}
	
	return false;
}
?>