26 private static $instance;
28 private array $documentProductPositionListCollection = [];
29 private int $catalogId;
36 if (is_null(static::$instance))
38 static::$instance =
new static();
41 return static::$instance;
51 public function getList(
int $documentId,
int $limit = 10): array
53 $productPositionList = $this->fetchDocumentProductPositionList($documentId);
54 return array_slice($productPositionList, 0, $limit);
65 return count($this->fetchDocumentProductPositionList($documentId));
69 private function fetchDocumentProductPositionList(
int $documentId): array
71 if (!Loader::includeModule(
'sale'))
76 if (!empty($this->documentProductPositionListCollection[$documentId]))
78 return $this->documentProductPositionListCollection[$documentId];
81 $documentProductListData = \CCatalogStoreDocsElement::getList(
83 [
'DOC_ID' => $documentId],
86 [
'ELEMENT_ID',
'ELEMENT_NAME',
'AMOUNT',
'PURCHASING_PRICE',
'BASE_PRICE']
89 $documentProductList = [];
90 while ($product = $documentProductListData->Fetch())
92 $documentProductList[] = $this->formProductPositionData($product);
95 $this->documentProductPositionListCollection[$documentId] = $documentProductList;
96 return $documentProductList;
99 private function formProductPositionData(array $product): array
101 $productPositionData = [
102 'PRODUCT_NAME' => $product[
'ELEMENT_NAME'],
106 $productId = $product[
'ELEMENT_ID'];
107 $productVariation = ServiceContainer::getRepositoryFacade()->loadVariation($product[
'ELEMENT_ID']);
108 if ($productVariation)
110 $image = $productVariation->getFrontImageCollection()->getFrontImage();
111 $productPositionData[
'PHOTO_URL'] = $image ? $image->getSource() :
null;
114 if (!State::isProductCardSliderEnabled())
116 $productId = $productVariation->getParent() ? $productVariation->getParent()->getId() : $productId;
120 $productPositionData[
'URL'] = $this->buildPositionUrl($productId);
122 return $productPositionData;
125 private function buildPositionUrl(
int $productId): string
130 $urlBuilder->setIblockId($this->getCatalogId());
131 return $urlBuilder->getElementDetailUrl($productId);
137 private function getCatalogId(): int
139 if (!isset($this->catalogId))
141 $this->catalogId = 0;
142 if (\
Bitrix\Main\Loader::includeModule(
'crm'))
144 $this->catalogId = \Bitrix\Crm\Product\Catalog::getDefaultId() ?? 0;
148 return $this->catalogId;