OpenCart 🇺🇦

Схема

public mixed putStockUpdateBulk ( $product_id_array, $endInactive = false )

Аргументы

Аргумент Возможный тип Описание
$product_id_array
$endInactive = false

Описание

Метод пока еще не документирован.

Исходный код

// We know is that these product ID's have been modified. They should only be passed if the stock has changed so we can assume this.
$this->log('putStockUpdateBulk()');

$openstock = false;
if($this->openbay->addonLoad('openstock') == true) {
	$this->load->model('openstock/openstock');
	$openstock = true;
}

// Get the active OpenCart items that were linked to eBay If they have stock now, relist them.
$endedData = $this->getEndedListingArray();

/**
 * Get the active OpenCart items that are also linked
 * Compare against the stock from eBay
 * If listing active and local stock = 0, end it
 * If listing inactive, remove link
 * If listing active and local stock not the same, update it
 */
$ebay_listings = $this->getEbayActiveListings();
$live_data = $this->getLiveListingArray();

$linkedItems        = array();
$linkedEndedItems   = array();

foreach($product_id_array as $product_id) {
	if(array_key_exists((int)$product_id, $live_data)) {
		//product has been passed and is linked to active item
		$linkedItems[] = array('productId' => (int)$product_id, 'itemId' => $live_data[$product_id]);
	}elseif(array_key_exists((int)$product_id, $endedData)) {
		//product has been passed and is not currently active
		$linkedEndedItems[] = array('productId' => (int)$product_id, 'itemId' => $endedData[$product_id]);
	}else{
		//product does not exist in live or ended links so has never been linked.
	}
}

//loop through ended listings, if back in stock and not multi var - relist it
foreach($linkedEndedItems as $item) {
	if($openstock == true) {
		$options = $this->model_openstock_openstock->getProductOptionStocks($item['productId']);
	} else {
		$options = array();
	}

	if(empty($options)) {
		//get the stock level of the linked items
		$local_stock = $this->getProductStockLevel($item['productId']);

		if((int)$local_stock['quantity'] > 0 && $local_stock['status'] == 1) {
			//product has stock and is enabled, so re list it.
			$reserve = $this->getReserve($item['productId'], $item['itemId']);

			if($reserve != false) {
				if($local_stock['quantity'] > $reserve) {
					$local_stock['quantity'] = $reserve;
				}
			}

			if($this->config->get('openbaypro_relistitems') == 1) {
				//relist item with new stock
				$this->relistItem($item['itemId'], $item['productId'],(int)$local_stock['quantity']);
			}
		}
	}else{
		$this->log('putStockUpdateBulk() - options existed for item ('.$item['itemId'].') when trying to relist');
		// @todo - support relisting of variant items, if possible with ebay!
	}
}

//loop through the active listings and update the store or end the item
foreach($linkedItems as $item) {
	//get the stock level of the linked item
	$local_stock = $this->getProductStockLevel($item['productId']);

	//check if the itemid was returned by ebay, if not unlink it as it is ended.
	if(!isset($ebay_listings[$item['itemId']])) {
		$this->log('eBay item was not returned, removing link ('.$item['itemId'].')');
		$this->removeItemByItemId($item['itemId']);
	}else{
		//check if the local item is now inactive - end if it is
		if($endInactive == true && $local_stock['status'] == 0) {
			$this->endItem($item['itemId']);
		}else{
			//get any options that are set for this product
			if($openstock == true) {
				$options = $this->model_openstock_openstock->getProductOptionStocks($item['productId']);
			} else {
				$options = array();
			}

			if(empty($options) && empty($ebay_listings[$item['itemId']]['variants'])) {
				$this->log('putStockUpdateBulk() - Item has no variants');

				//compare to the ebay data get retrieved
				if((int)$local_stock['quantity'] != (int)$ebay_listings[$item['itemId']]['qty']) {
					$reserve = $this->getReserve($item['productId'], $item['itemId']);

					if($reserve != false) {
						if($local_stock['quantity'] > $reserve) {
							$local_stock['quantity'] = $reserve;
						}
					}

					$this->putStockUpdate($item['itemId'], (int)$local_stock['quantity']);
				}
			}elseif(!empty($options) && !empty($ebay_listings[$item['itemId']]['variants'])) {
				// This item has variants
				$this->log('putStockUpdateBulk() - Variants found');

				//create an index of var codes to search against
				$var_ids = array();
				foreach($options as $k => $v) {
					$var_ids[$k] = $v['var'];
				}

				//loop over eBay variants
				foreach($ebay_listings[$item['itemId']]['variants'] as $ebay_variant) {
					$this->log('Checking eBay SKU: '.$ebay_variant['sku'].' for item: '.$item['itemId']);

					if(in_array($ebay_variant['sku'], $var_ids)) {
						$option_id = array_search($ebay_variant['sku'], $var_ids);

						//compare the stock - if different trigger update
						if($ebay_variant['qty'] != $options[$option_id]['stock']) {
							$this->log('putStockUpdateBulk() - Revising variant item: '.$item['itemId'].',Stock: '.$options[$option_id]['stock'].', SKU '.$ebay_variant['sku']);
							$this->call('item/reviseStock/', array('itemId' => $item['itemId'], 'stock' => $options[$option_id]['stock'], 'sku' => $ebay_variant['sku']));
						}
					}
				}
			}else{
				$this->log('Unsure if this item has variants, debug:');
				$this->log('Local: ' . $options);
				$this->log('eBay: ' . serialize($ebay_listings[$item['itemId']]['variants']));
			}
		}
	}
}