<?php

if(!isset($_SESSION["cart"])) { $_SESSION["cart"]=array(); }

if(!empty($_POST)){
	$products = ProductData::getFavorites();
	foreach($products as $p){
		$add = false;
		if(isset($_POST["q-".$p->id]) && $_POST["q-".$p->id]!=0){
			if($p->use_inventory){
				$q = OperationData::getQByStock($p->id,StockData::getPrincipal()->id);
				if($q>0){
					$add=true;
				}
			}else{
				$add =true;

			}
		}
		if($add){
			$cart = $_SESSION["cart"];

			$index = 0;
			$f=false;
			if(!empty($cart)){ foreach($cart as $c){ if($c["product_id"]==$p->id){$f=true; break; } $index++; }}

			if($f){
				$q1 = $cart[$index]["q"];
				$q2 = $_POST["q-".$p->id];
				$cart[$index]["q"]=$q1+$q2;
				$_SESSION["cart"] = $cart;
			}else{
				$product = array("product_id"=>$p->id,"q"=>$_POST["q-".$p->id]);
				$cart[] = $product;
				$_SESSION["cart"] = $cart;			

			}

		}
	}
}

Core::redir("./?view=selldirect");
?>