localProducts[] = (array) $localProduct; } } } public function update($remoteStock) { \Core\Log\FileLog::write('Facturis Stock: ' . json_encode($remoteStock), \Core\Log\FileLog::DATA); \Core\Log\FileLog::write('Shop Products: ' . json_encode($this->localProducts), \Core\Log\FileLog::DATA); $this->updateStock($remoteStock); } public function getShopLocations() { $shopLocations = array(); $locations = $this->getLocations(); if(!empty($locations)) { foreach($locations as $location) { $shopLocations[] = array('id' => $location['id'], 'name' => $location['name']); } } return $shopLocations; } public function setStockLocations($locations) { $this->stockLocations = $locations; } //update private function updateStock($remoteStock) { if(!empty($remoteStock)) { foreach($remoteStock as $remoteStockItem) { if (in_array($remoteStockItem['product'], array_column($this->localProducts, $remoteStockItem['type']))) { $index = array_search($remoteStockItem['product'], array_column($this->localProducts, $remoteStockItem['type'])); $product_ids = explode('_', $this->localProducts[$index]['prod_cod1']); $variant_ids = $product_ids; unset($variant_ids[0]); unset($variant_ids[1]); if(!empty($variant_ids)) { foreach($variant_ids as $variant_id) { $this->updateQty($remoteStockItem['qty'], $product_ids[1], $variant_id); } } } } } } private function updateQty($qty, $product_id, $variant_id) { $productVariant = $this->getProductVariant($product_id, $variant_id); if(!empty($productVariant)) { $resultInventoryLevels = $this->getInventoryLevels($productVariant['inventory_item_id']); if(!empty($resultInventoryLevels)) { foreach($resultInventoryLevels as $inventoryLevel) { $adjustQty = ((-1) * $inventoryLevel['available']) + (int) $qty; if((is_array($this->stockLocations) && in_array($inventoryLevel['location_id'], $this->stockLocations)) || ($this->stockLocations == \Core\App\Factory\SettingsFactory::DEFAULT_LOCATIONS)) { $this->adjustInventoryLevel($inventoryLevel['location_id'], $inventoryLevel['inventory_item_id'], $adjustQty, $inventoryLevel['available']); } } } } } }