"value", ... ] public $combinations = []; // Array of MiraviaCombination class public $info = []; // Internal info (for filters) protected $private_vars = ['private_vars', 'error', 'id', 'combinations', 'info']; public $error = ''; public function __construct() { // } public function addAttr($key = false, $value = false) { if($key and $value) { $this->extra_attributes[$key] = $value; } } public function getData() { $product = []; foreach ($this as $key => $value){ if(!in_array($key, $this->private_vars)){ if($key === 'images' && is_array($value) && !empty($value)){ $product['Images'] = ['Image' => $value]; } else if($key !== 'images') { $product[$key] = $value; } } } $combinations = $this->getCombinations(); $product['combinations'] = $combinations; return $product; } public function getStockData() { $combis = []; foreach ($this->combinations as $combination){ $combis[] = $combination->getStockData(); } if(count($combis)==0){ $combis =[[ 'sku' => $this->sku, 'price' => $this->price, 'special_price' => $this->special_price, 'quantity' => $this->quantity >= 0 ? $this->quantity : 0 ]]; $this->reference_sku = $this->sku; } $ret = [ 'id' => $this->id, 'id_miravia' => $this->id_miravia, 'sku' => $this->sku, 'combinations' => $combis ]; if(!empty($this->warehouse)){ $ret['warehouse'] = $this->warehouse; } return $ret; } public function getOnlyStockData() { $combis = []; foreach ($this->combinations as $combination){ $combis[] = $combination->getOnlyStockData(); } if(count($combis)==0){ $combis =[[ 'sku' => $this->sku, 'quantity' => $this->quantity >= 0 ? $this->quantity : 0 ]]; $this->reference_sku = $this->sku; } $ret = [ 'id' => $this->id, 'id_miravia' => $this->id_miravia, 'sku' => $this->sku, 'combinations' => $combis ]; if(!empty($this->warehouse)){ $ret['warehouse'] = $this->warehouse; } return $ret; } public function getJSON($pretty = false) { $flags = $pretty ? JSON_PRETTY_PRINT : 0; return json_encode($this->getData(), $flags); } public function getCombinations() { $ret = []; foreach ($this->combinations as $combination){ $ret[] = $combination->getData(); } return $ret; } public function createProduct($apiKey) { if(!empty($this->id_miravia)){ return $this->updateProduct($apiKey); } $jreq = json_encode($this->getData()); $link = new MiraviaLink($apiKey); $ret = $link->createProduct($jreq); if($ret == false) { $this->error = $link->last_error; } if(isset($ret['data'])){ if(isset($ret['data']['item_id'])){ $this->id_miravia = $ret['data']['item_id']; } } return true; } public function updateProduct($apiKey) { if(empty($this->id_miravia)){ $this->error = 'Invalid Miravia Id Product'; return false; } $jreq = json_encode($this->getData()); $link = new MiraviaLink($apiKey); $ret = $link->updateProduct($this->id_miravia, $jreq); if($ret == false) { $this->error = $link->last_error; } if(isset($ret['data'])){ if(isset($ret['data']['item_id'])){ $this->id_miravia = $ret['data']['item_id']; } return true; } if(isset($ret['message'])){ $this->error = $ret['message']; } return false; } public function getInfoValue($field) { if(isset($this->info[$field])){ return $this->info[$field]; } return ''; } }