• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/helpers/order/builder/basketbuilder.php
  • Класс: BitrixSaleHelpersOrderBuilderBasketBuilder
  • Вызов: BasketBuilder::setItemsFields
public function setItemsFields()
{
	$order = $this->getOrder();
	$basketItems = $this->getBasket()->getBasketItems();

	$this->setItemsFieldsByFormData();
	$this->obtainCatalogProductsData();
	$this->obtainProviderProductsData();

	$productProviderData = array();

	/** @var BasketItem $item */
	foreach($basketItems as $item)
	{
		$basketCode = $item->getBasketCode();
		$productFormData = $this->formData['PRODUCT'][$basketCode] ?? [];
		$isProductDataNeedUpdate = in_array($basketCode, $this->needDataUpdate);
		$productProviderData[$basketCode] = $item->getFieldValues();

		if(empty($productFormData))
			continue;

		if(!empty($this->providerData[$basketCode]))
		{
			if ($this->builder->getSettingsContainer()->getItemValue('isRefreshData') === true)
			{
				unset($this->providerData[$basketCode]['QUANTITY']);
			}

			$productProviderData[$basketCode] = $this->providerData[$basketCode];
		}
		elseif(!empty($trustData[$basketCode]))
		{
			$productProviderData[$basketCode] = $trustData[$basketCode];
		}
		else
		{
			$productProviderData = Provider::getProductData($this->getBasket(), array("PRICE", "AVAILABLE_QUANTITY"), $item);

			if(is_array($productProviderData[$basketCode]) && !empty($productProviderData[$basketCode]))
				OrderEdit::setProviderTrustData($item, $order, $productProviderData[$basketCode]);
		}

		/* Get actual info from provider
		 *	cases:
		 *	 1) add new product to basket;
		 *	 2) saving operation;
		 * 	 3) changing quantity;
		 *   4) changing buyerId
		 */

		if(!$this->isProductAvailable($basketCode, $productFormData, $productProviderData, $isProductDataNeedUpdate))
		{
			$this->getErrorsContainer()->addError(
				new Error(
					Loc::getMessage(
						"SALE_HLP_ORDERBUILDER_PRODUCT_NOT_AVILABLE",
						array(
							"#NAME#" => $productFormData["NAME"] . (!empty($productFormData["PRODUCT_ID"]) ? " (".$productFormData['PRODUCT_ID'].")" : "")
						)
					)
				)
			);
		}

		$product = array();

		//merge catalog data
		if($item->getField("MODULE") == "catalog")
		{
			if(!empty($catalogData[$item->getProductId()]))
			{
				$product = array_merge($product, $catalogData[$item->getProductId()]);
				unset($productFormData["CURRENCY"]);
			}
		}

		//merge form data
		if(!$this->cacheProductProviderData || $isProductDataNeedUpdate)
		{
			$product = array_merge($productFormData, $product);
		}
		else
		{
			$basePrice = $productFormData['BASE_PRICE'] ?? 0;
			$price = $productFormData['PRICE'] ?? 0;

			$needUpdateItemPrice = $this->isNeedUpdateNewProductPrice() && $this->isBasketItemNew($basketCode);
			$isPriceCustom = isset($productFormData['CUSTOM_PRICE']) && $productFormData['CUSTOM_PRICE'] == 'Y';

			if ($isPriceCustom)
			{
				$productFormData['DISCOUNT_PRICE'] = 0;
				if ($basePrice > $price)
				{
					$productFormData['DISCOUNT_PRICE'] = $basePrice - $price;
				}
			}

			if (($order->getId() === 0 && !$isPriceCustom) || $needUpdateItemPrice)
			{
				unset($productFormData['PRICE'], $productFormData['PRICE_BASE'], $productFormData['BASE_PRICE']);
			}

			$product = array_merge($product, $productFormData);
		}

		if (isset($product["OFFER_ID"]) && intval($product["OFFER_ID"]) > 0)
		{
			$product["PRODUCT_ID"] = $product["OFFER_ID"];
		}

		//discard BasketItem redundant fields
		$product = array_intersect_key($product, array_flip($item::getAvailableFields()));

		if (isset($product["MEASURE_CODE"]) && $product["MEASURE_CODE"] <> '')
		{
			$measures = OrderBasket::getCatalogMeasures();

			if (!empty($measures[$product["MEASURE_CODE"]]))
			{
				$product["MEASURE_NAME"] = $measures[$product["MEASURE_CODE"]];
			}
		}

		if (empty($product["CURRENCY"]))
		{
			$product["CURRENCY"] = $order->getCurrency();
		}

		if (
			isset($productFormData["IS_SET_PARENT"])
			&& $productFormData["IS_SET_PARENT"] === "Y"
		)
		{
			$product["TYPE"] = BasketItem::TYPE_SET;
		}

		OrderEdit::setProductDetails(
			$productFormData["OFFER_ID"],
			$order->getUserId(),
			$order->getSiteId(),
			array_merge($product, $productFormData)
		);

		if ($product["CURRENCY"] !== $order->getCurrency())
		{
			$product["PRICE"] = CCurrencyRates::ConvertCurrency($product["PRICE"], $product["CURRENCY"], $order->getCurrency());
			if ($product["BASE_PRICE"] > 0)
			{
				$product["BASE_PRICE"] = CCurrencyRates::ConvertCurrency($product["BASE_PRICE"], $product["CURRENCY"], $order->getCurrency());
			}
			if ($product["DISCOUNT_PRICE"] > 0)
			{
				$product["DISCOUNT_PRICE"] = CCurrencyRates::ConvertCurrency($product["DISCOUNT_PRICE"], $product["CURRENCY"], $order->getCurrency());
			}
			if ($product["VAT_RATE"] > 0)
			{
				$product["VAT_RATE"] = CCurrencyRates::ConvertCurrency($product["VAT_RATE"], $product["CURRENCY"], $order->getCurrency());
			}

			$product["CURRENCY"] = $order->getCurrency();
		}

		$this->setBasketItemFields($item, $product);

		if ($item->isReservableItem())
		{
			if (!empty($productFormData['RESERVE']) && is_array($productFormData['RESERVE']))
			{
				$reserveData = $productFormData['RESERVE'];
				$this->setReserveDataForItem($item, $reserveData);
			}
			elseif ($this->getSettingsContainer()->getItemValue('clearReservesIfEmpty') === true)
			{
				$this->clearReservesForItem($item);
			}
		}
	}

	return $this;
}