Jump to content

Edit History

WisQQ

WisQQ

6 hours ago, djinni said:

Thank you. I gave it a try and get this:
connected!.
connected!.
array error.
exit

I Just have a simple test csv, with reference & quantity.

I would like to get both simple and combinations working to adjust quantities, but I'm still learning, so one step at a time.

Ah sorry, i gave you wrong query :)

17 hours ago, WisQQ said:

Function shop_products(){ $db = connect(); $array=array(); $query= $db->prepare('select ps_product.id_manufacturer AS manuid,ps_product_attribute.reference AS REF,ps_stock_available.quantity AS QTY from ps_product LEFT JOIN ps_product_attribute on ps_product_attribute.id_product=ps_product.id_product LEFT JOIN ps_stock_available on ps_stock_available.id_product_attribute=ps_product_attribute.id_product_attribute'); try{ $query->execute(); while (($row = $query->fetch(PDO::FETCH_BOTH)) !== false) { if(!empty($row['REF']) && $row['manuid'] == 6){ $array[]=$row; } } echo 'array filled.<br />'; }catch(PDOException $e){ echo 'array error.<br />'; } return $array; }

change this to

Function shop_products(){
	$db = connect();	
	$array=array();
	$array2=array();
	$query= $db->prepare('select ps_product.id_manufacturer AS manuid,ps_product_attribute.reference AS REF,ps_stock_available.quantity AS QTY from ps_product LEFT JOIN ps_product_attribute on ps_product_attribute.id_product=ps_product.id_product LEFT JOIN ps_stock_available on ps_stock_available.id_product_attribute=ps_product_attribute.id_product_attribute');
	try{
		$query->execute();
		while (($row = $query->fetch(PDO::FETCH_BOTH)) !== false) {
			if(!empty($row['REF'])){
				$array2[]=$row;
			}
		}
		echo 'array filled.<br />';		
	}catch(PDOException $e){
		echo 'array error.<br />';
	}
	$query= $db->prepare('SELECT ps_product.reference AS REF, ps_product.id_manufacturer AS manuid,ps_stock_available.id_product_attribute AS BaseP, ps_stock_available.quantity AS QTY FROM ps_product LEFT JOIN ps_stock_available ON ps_stock_available.id_product=ps_product.id_product');
	try{
		$query->execute();
		while (($row = $query->fetch(PDO::FETCH_BOTH)) !== false) {
			if(!empty($row['REF']) && $row['BaseP'] == 0){
					$array[]=$row;
			}
		}
		echo 'array filled.<br />';		
	}catch(PDOException $e){
		echo 'array error.<br />';
	}
	foreach($array as $f=>$val){
		foreach($array2 as $f1=>$val2){
			if($val['REF'] == $val2['REF']){
				unset($array[$f]);
			}
		}
	}
	$stock = array_merge($array,$array2);
	return $stock;
}

this function gets all products and product attributes with reference from your shop.
BaseP is base product. In my case some products have same reference for base product and the default combination. So after it gets all products it removes duplicates(BaseP and leaves combination) and merge arrays. 
 

The update function to update simple and products with combination looks like this.

Function Update(){
	$db = connect();
	$csvFile = 'path to csv file';
	$csv = readCSV($csvFile);
	$sp = shop_products();
	foreach($sp as $ps){
		$indeks = $ps['REF'];
		$qty = $ps['QTY'];
		$base = $ps['BaseP'];
		if(!empty($indeks)){
			foreach($csv as $stock){
				$index = $stock['Reference'];
				$avbl = $stock['Quantity'];
				if($indeks == $index){
					if($qty != $avbl){
						if(isset($base)){
							try{
								$query = $db->prepare("UPDATE ps_product LEFT JOIN ps_stock_available on ps_stock_available.id_product=ps_product.id_product SET ps_stock_available.quantity = '".$avbl."' WHERE ps_product.reference = '".$indeks."' AND ps_stock_available.id_product_attribute = 0");
								$query->execute();
								$log  = $indeks.PHP_EOL.
									"simple product - quantity change from ".$qty." to ".$avbl.PHP_EOL.
									"-------------------------".PHP_EOL;
								file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
							}catch(PDOException $e){
								$log  = $indeks.PHP_EOL.
									"error ".$e->getMessage().PHP_EOL.
									"-------------------------".PHP_EOL;
								file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
								die();
							}							
						}else{
							try{
								$query = $db->prepare("UPDATE ps_product_attribute LEFT JOIN ps_stock_available on ps_stock_available.id_product_attribute=ps_product_attribute.id_product_attribute LEFT JOIN ps_product on ps_product_attribute.id_product=ps_product.id_product SET ps_stock_available.quantity = '".$avbl."' WHERE ps_product_attribute.reference = '".$indeks."'");
								$query->execute();
								$log  = $indeks.PHP_EOL.
									"product combination - quantity change from ".$qty." to ".$avbl.PHP_EOL.
									"-------------------------".PHP_EOL;
								file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
							}catch(PDOException $e){
								$log  = $indeks.PHP_EOL.
									"error ".$e->getMessage().PHP_EOL.
									"-------------------------".PHP_EOL;
								file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
								die();
							}							
						}
					}
				}
			}
		}	
	}
}

 

WisQQ

WisQQ

6 hours ago, djinni said:

Thank you. I gave it a try and get this:
connected!.
connected!.
array error.
exit

I Just have a simple test csv, with reference & quantity.

I would like to get both simple and combinations working to adjust quantities, but I'm still learning, so one step at a time.

Ah sorry, i gave you wrong query :)

17 hours ago, WisQQ said:

Function shop_products(){ $db = connect(); $array=array(); $query= $db->prepare('select ps_product.id_manufacturer AS manuid,ps_product_attribute.reference AS REF,ps_stock_available.quantity AS QTY from ps_product LEFT JOIN ps_product_attribute on ps_product_attribute.id_product=ps_product.id_product LEFT JOIN ps_stock_available on ps_stock_available.id_product_attribute=ps_product_attribute.id_product_attribute'); try{ $query->execute(); while (($row = $query->fetch(PDO::FETCH_BOTH)) !== false) { if(!empty($row['REF']) && $row['manuid'] == 6){ $array[]=$row; } } echo 'array filled.<br />'; }catch(PDOException $e){ echo 'array error.<br />'; } return $array; }

change this to

Function shop_products(){
	$db = connect();	
	$array=array();
	$array2=array();
	$query= $db->prepare('select ps_product.id_manufacturer AS manuid,ps_product_attribute.reference AS REF,ps_stock_available.quantity AS QTY from ps_product LEFT JOIN ps_product_attribute on ps_product_attribute.id_product=ps_product.id_product LEFT JOIN ps_stock_available on ps_stock_available.id_product_attribute=ps_product_attribute.id_product_attribute');
	try{
		$query->execute();
		while (($row = $query->fetch(PDO::FETCH_BOTH)) !== false) {
					$array2[]=$row;
		}
		echo 'array filled.<br />';		
	}catch(PDOException $e){
		echo 'array error.<br />';
	}
	$query= $db->prepare('SELECT ps_product.reference AS REF, ps_product.id_manufacturer AS manuid,ps_stock_available.id_product_attribute AS BaseP, ps_stock_available.quantity AS QTY FROM ps_product LEFT JOIN ps_stock_available ON ps_stock_available.id_product=ps_product.id_product');
	try{
		$query->execute();
		while (($row = $query->fetch(PDO::FETCH_BOTH)) !== false) {
			if(!empty($row['REF']) && $row['BaseP'] == 0){
					$array[]=$row;
			}
		}
		echo 'array filled.<br />';		
	}catch(PDOException $e){
		echo 'array error.<br />';
	}
	foreach($array as $f=>$val){
		foreach($array2 as $f1=>$val2){
			if($val['REF'] == $val2['REF']){
				unset($array[$f]);
			}
		}
	}
	$stock = array_merge($array,$array2);
	return $stock;
}

this function gets all products and product attributes with reference from your shop.
BaseP is base product. In my case some products have same reference for base product and the default combination. So after it gets all products it removes duplicates(BaseP and leaves combination) and merge arrays. 
 

The update function to update simple and products with combination looks like this.

Function Update(){
	$db = connect();
	$csvFile = 'path to csv file';
	$csv = readCSV($csvFile);
	$sp = shop_products();
	foreach($sp as $ps){
		$indeks = $ps['REF'];
		$qty = $ps['QTY'];
		$base = $ps['BaseP'];
		if(!empty($indeks)){
			foreach($csv as $stock){
				$index = $stock['Reference'];
				$avbl = $stock['Quantity'];
				if($indeks == $index){
					if($qty != $avbl){
						if(isset($base)){
							try{
								$query = $db->prepare("UPDATE ps_product LEFT JOIN ps_stock_available on ps_stock_available.id_product=ps_product.id_product SET ps_stock_available.quantity = '".$avbl."' WHERE ps_product.reference = '".$indeks."' AND ps_stock_available.id_product_attribute = 0");
								$query->execute();
								$log  = $indeks.PHP_EOL.
									"simple product - quantity change from ".$qty." to ".$avbl.PHP_EOL.
									"-------------------------".PHP_EOL;
								file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
							}catch(PDOException $e){
								$log  = $indeks.PHP_EOL.
									"error ".$e->getMessage().PHP_EOL.
									"-------------------------".PHP_EOL;
								file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
								die();
							}							
						}else{
							try{
								$query = $db->prepare("UPDATE ps_product_attribute LEFT JOIN ps_stock_available on ps_stock_available.id_product_attribute=ps_product_attribute.id_product_attribute LEFT JOIN ps_product on ps_product_attribute.id_product=ps_product.id_product SET ps_stock_available.quantity = '".$avbl."' WHERE ps_product_attribute.reference = '".$indeks."'");
								$query->execute();
								$log  = $indeks.PHP_EOL.
									"product combination - quantity change from ".$qty." to ".$avbl.PHP_EOL.
									"-------------------------".PHP_EOL;
								file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
							}catch(PDOException $e){
								$log  = $indeks.PHP_EOL.
									"error ".$e->getMessage().PHP_EOL.
									"-------------------------".PHP_EOL;
								file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
								die();
							}							
						}
					}
				}
			}
		}	
	}
}

 

WisQQ

WisQQ

6 hours ago, djinni said:

Thank you. I gave it a try and get this:
connected!.
connected!.
array error.
exit

I Just have a simple test csv, with reference & quantity.

I would like to get both simple and combinations working to adjust quantities, but I'm still learning, so one step at a time.

Ah sorry, i gave you wrong query :)

17 hours ago, WisQQ said:

Function shop_products(){ $db = connect(); $array=array(); $query= $db->prepare('select ps_product.id_manufacturer AS manuid,ps_product_attribute.reference AS REF,ps_stock_available.quantity AS QTY from ps_product LEFT JOIN ps_product_attribute on ps_product_attribute.id_product=ps_product.id_product LEFT JOIN ps_stock_available on ps_stock_available.id_product_attribute=ps_product_attribute.id_product_attribute'); try{ $query->execute(); while (($row = $query->fetch(PDO::FETCH_BOTH)) !== false) { if(!empty($row['REF']) && $row['manuid'] == 6){ $array[]=$row; } } echo 'array filled.<br />'; }catch(PDOException $e){ echo 'array error.<br />'; } return $array; }

change this to

Function shop_products(){
	$db = connect();	
	$array=array();
	$array2=array();
	$query= $db->prepare('select ps_product.id_manufacturer AS manuid,ps_product_attribute.reference AS REF,ps_stock_available.quantity AS QTY from ps_product LEFT JOIN ps_product_attribute on ps_product_attribute.id_product=ps_product.id_product LEFT JOIN ps_stock_available on ps_stock_available.id_product_attribute=ps_product_attribute.id_product_attribute');
	try{
		$query->execute();
		while (($row = $query->fetch(PDO::FETCH_BOTH)) !== false) {
					$array2[]=$row;
		}
		echo 'array filled.<br />';		
	}catch(PDOException $e){
		echo 'array error.<br />';
	}
	$query= $db->prepare('SELECT ps_product.reference AS REF, ps_product.id_manufacturer AS manuid,ps_stock_available.id_product_attribute AS BaseP, ps_stock_available.quantity AS QTY FROM ps_product LEFT JOIN ps_stock_available ON ps_stock_available.id_product=ps_product.id_product');
	try{
		$query->execute();
		while (($row = $query->fetch(PDO::FETCH_BOTH)) !== false) {
			if(!empty($row['REF']) && $row['BaseP'] == 0){
					$array[]=$row;
			}
		}
		echo 'array filled.<br />';		
	}catch(PDOException $e){
		echo 'array error.<br />';
	}
	foreach($array as $f=>$val){
		foreach($array2 as $f1=>$val2){
			if($val['REF'] == $val2['REF']){
				unset($array[$f]);
			}
		}
	}
	$stock = array_merge($array,$array2);
	return $stock;
}

this function gets all products and product attributes with reference from your shop.
BaseP is base product. In my case some products have same reference for base product and the default combination. So after it gets all products it removes duplicates and merge arrays. 
 

The update function to update simple and products with combination looks like this.

Function Update(){
	$db = connect();
	$csvFile = 'path to csv file';
	$csv = readCSV($csvFile);
	$sp = shop_products();
	foreach($sp as $ps){
		$indeks = $ps['REF'];
		$qty = $ps['QTY'];
		$base = $ps['BaseP'];
		if(!empty($indeks)){
			foreach($csv as $stock){
				$index = $stock['Reference'];
				$avbl = $stock['Quantity'];
				if($indeks == $index){
					if($qty != $avbl){
						if(isset($base)){
							try{
								$query = $db->prepare("UPDATE ps_product LEFT JOIN ps_stock_available on ps_stock_available.id_product=ps_product.id_product SET ps_stock_available.quantity = '".$avbl."' WHERE ps_product.reference = '".$indeks."' AND ps_stock_available.id_product_attribute = 0");
								$query->execute();
								$log  = $indeks.PHP_EOL.
									"simple product - quantity change from ".$qty." to ".$avbl.PHP_EOL.
									"-------------------------".PHP_EOL;
								file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
							}catch(PDOException $e){
								$log  = $indeks.PHP_EOL.
									"error ".$e->getMessage().PHP_EOL.
									"-------------------------".PHP_EOL;
								file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
								die();
							}							
						}else{
							try{
								$query = $db->prepare("UPDATE ps_product_attribute LEFT JOIN ps_stock_available on ps_stock_available.id_product_attribute=ps_product_attribute.id_product_attribute LEFT JOIN ps_product on ps_product_attribute.id_product=ps_product.id_product SET ps_stock_available.quantity = '".$avbl."' WHERE ps_product_attribute.reference = '".$indeks."'");
								$query->execute();
								$log  = $indeks.PHP_EOL.
									"product combination - quantity change from ".$qty." to ".$avbl.PHP_EOL.
									"-------------------------".PHP_EOL;
								file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
							}catch(PDOException $e){
								$log  = $indeks.PHP_EOL.
									"error ".$e->getMessage().PHP_EOL.
									"-------------------------".PHP_EOL;
								file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
								die();
							}							
						}
					}
				}
			}
		}	
	}
}

 

WisQQ

WisQQ

Ah sorry, i gave you wrong query :)

17 hours ago, WisQQ said:

Function shop_products(){ $db = connect(); $array=array(); $query= $db->prepare('select ps_product.id_manufacturer AS manuid,ps_product_attribute.reference AS REF,ps_stock_available.quantity AS QTY from ps_product LEFT JOIN ps_product_attribute on ps_product_attribute.id_product=ps_product.id_product LEFT JOIN ps_stock_available on ps_stock_available.id_product_attribute=ps_product_attribute.id_product_attribute'); try{ $query->execute(); while (($row = $query->fetch(PDO::FETCH_BOTH)) !== false) { if(!empty($row['REF']) && $row['manuid'] == 6){ $array[]=$row; } } echo 'array filled.<br />'; }catch(PDOException $e){ echo 'array error.<br />'; } return $array; }

change this to

Function shop_products(){
	$db = connect();	
	$array=array();
	$array2=array();
	$query= $db->prepare('select ps_product.id_manufacturer AS manuid,ps_product_attribute.reference AS REF,ps_stock_available.quantity AS QTY from ps_product LEFT JOIN ps_product_attribute on ps_product_attribute.id_product=ps_product.id_product LEFT JOIN ps_stock_available on ps_stock_available.id_product_attribute=ps_product_attribute.id_product_attribute');
	try{
		$query->execute();
		while (($row = $query->fetch(PDO::FETCH_BOTH)) !== false) {
					$array2[]=$row;
		}
		echo 'array filled.<br />';		
	}catch(PDOException $e){
		echo 'array error.<br />';
	}
	$query= $db->prepare('SELECT ps_product.reference AS REF, ps_product.id_manufacturer AS manuid,ps_stock_available.id_product_attribute AS BaseP, ps_stock_available.quantity AS QTY FROM ps_product LEFT JOIN ps_stock_available ON ps_stock_available.id_product=ps_product.id_product');
	try{
		$query->execute();
		while (($row = $query->fetch(PDO::FETCH_BOTH)) !== false) {
			if(!empty($row['REF']) && $row['BaseP'] == 0){
					$array[]=$row;
			}
		}
		echo 'array filled.<br />';		
	}catch(PDOException $e){
		echo 'array error.<br />';
	}
	foreach($array as $f=>$val){
		foreach($array2 as $f1=>$val2){
			if($val['REF'] == $val2['REF']){
				unset($array[$f]);
			}
		}
	}
	$stock = array_merge($array,$array2);
	return $stock;
}

this function gets all products and product attributes with reference from your shop.
BaseP is base product. In my case some products have same reference for base product and the default combination. So after it gets all products it removes duplicates and merge arrays. 
 

The update function to update simple and products with combination looks like this.

Function Update(){
	$db = connect();
	$csvFile = 'path to csv file';
	$csv = readCSV($csvFile);
	$sp = shop_products();
	foreach($sp as $ps){
		$indeks = $ps['REF'];
		$qty = $ps['QTY'];
		$base = $ps['BaseP'];
		if(!empty($indeks)){
			foreach($csv as $stock){
				$index = $stock['Reference'];
				$avbl = $stock['Quantity'];
				if($indeks == $index){
					if($qty != $avbl){
						if(isset($base)){
							try{
								$query = $db->prepare("UPDATE ps_product LEFT JOIN ps_stock_available on ps_stock_available.id_product=ps_product.id_product SET ps_stock_available.quantity = '".$avbl."' WHERE ps_product.reference = '".$indeks."' AND ps_stock_available.id_product_attribute = 0");
								$query->execute();
								$log  = $indeks.PHP_EOL.
									"simple product - quantity change from ".$qty." to ".$avbl.PHP_EOL.
									"-------------------------".PHP_EOL;
								file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
							}catch(PDOException $e){
								$log  = $indeks.PHP_EOL.
									"error ".$e->getMessage().PHP_EOL.
									"-------------------------".PHP_EOL;
								file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
								die();
							}							
						}else{
							try{
								$query = $db->prepare("UPDATE ps_product_attribute LEFT JOIN ps_stock_available on ps_stock_available.id_product_attribute=ps_product_attribute.id_product_attribute LEFT JOIN ps_product on ps_product_attribute.id_product=ps_product.id_product SET ps_stock_available.quantity = '".$avbl."' WHERE ps_product_attribute.reference = '".$indeks."'");
								$query->execute();
								$log  = $indeks.PHP_EOL.
									"product combination - quantity change from ".$qty." to ".$avbl.PHP_EOL.
									"-------------------------".PHP_EOL;
								file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
							}catch(PDOException $e){
								$log  = $indeks.PHP_EOL.
									"error ".$e->getMessage().PHP_EOL.
									"-------------------------".PHP_EOL;
								file_put_contents('./log/log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
								die();
							}							
						}
					}
				}
			}
		}	
	}
}

 

×
×
  • Create New...