From 3e5a04af62226c4efcce771e847b230aa8baeea3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=A7=D0=B0=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2?= <45812598+Chazovs@users.noreply.github.com> Date: Tue, 2 Nov 2021 11:25:26 +0300 Subject: [PATCH] Catalog fix * add empty array Instead of and add xml-header with charsets * fix catalog purchasePrice * use file_put_contents for flush --- intaro.retailcrm/export/export_setup.php | 2 +- intaro.retailcrm/lib/icml/icmldirector.php | 10 ++- intaro.retailcrm/lib/icml/icmlwriter.php | 76 ++++++++++--------- .../lib/icml/queryparamsmolder.php | 1 + intaro.retailcrm/lib/icml/xmlofferbuilder.php | 1 + .../lib/model/bitrix/xml/xmloffer.php | 46 ++++++----- .../lib/repository/catalogrepository.php | 2 +- 7 files changed, 82 insertions(+), 56 deletions(-) diff --git a/intaro.retailcrm/export/export_setup.php b/intaro.retailcrm/export/export_setup.php index 9ee49c77..d0eea8cd 100644 --- a/intaro.retailcrm/export/export_setup.php +++ b/intaro.retailcrm/export/export_setup.php @@ -25,7 +25,7 @@ CModule::IncludeModule('intaro.retailcrm'); //TODO заменить вызов на сервис-локатор, когда он приедет $settingsService = SettingsService::getInstance( - $arOldSetupVars, + $arOldSetupVars ?? [], $ACTION ); diff --git a/intaro.retailcrm/lib/icml/icmldirector.php b/intaro.retailcrm/lib/icml/icmldirector.php index 8519d55f..5d03f4a6 100644 --- a/intaro.retailcrm/lib/icml/icmldirector.php +++ b/intaro.retailcrm/lib/icml/icmldirector.php @@ -2,8 +2,7 @@ namespace Intaro\RetailCrm\Icml; -use COption; -use Intaro\RetailCrm\Icml\Utils\IcmlUtils; +use Bitrix\Catalog\ProductTable; use Intaro\RetailCrm\Model\Bitrix\Orm\CatalogIblockInfo; use Intaro\RetailCrm\Model\Bitrix\Xml\SelectParams; use Intaro\RetailCrm\Model\Bitrix\Xml\XmlData; @@ -178,6 +177,7 @@ class IcmlDirector $this->setup->properties->products->names[$productIblockId], $this->setup->basePriceId ); + $paramsForOffer = $this->queryBuilder->getSelectParams( $this->setup->properties->sku->names[$productIblockId], @@ -249,6 +249,12 @@ class IcmlDirector $paramsForOffer->allParams = array_merge($paramsForOffer->configurable, $paramsForOffer->main); do { + //Если каталог проиндексирован, у товара есть Тип и это простой товар, то просто записываем его + if ($product->productType = ProductTable::TYPE_PRODUCT) { + $this->icmlWriter->writeOffers([$product]); + break; + } + $xmlOffersPart = $this->xmlOfferDirector->getXmlOffersBySingleProduct($paramsForOffer, $catalogIblockInfo, $product); diff --git a/intaro.retailcrm/lib/icml/icmlwriter.php b/intaro.retailcrm/lib/icml/icmlwriter.php index c6ff60db..a9688f1b 100644 --- a/intaro.retailcrm/lib/icml/icmlwriter.php +++ b/intaro.retailcrm/lib/icml/icmlwriter.php @@ -18,23 +18,30 @@ class IcmlWriter { public const INFO = 'INFO'; public const CATEGORY_PART = 1000; - + /** * @var \XMLWriter */ private $writer; - + /** + * @var string + */ + private $filePath; + /** * IcmlWriter constructor. - * @param $filePath + * + * @param string $filePath */ - public function __construct($filePath) + public function __construct(string $filePath) { + $this->filePath = $filePath; $this->writer = new XMLWriter(); - $this->writer->openURI($_SERVER['DOCUMENT_ROOT'] . $filePath); - $this->writer->setIndent(true); + $this->writer->openMemory(); + $this->writer->setIndent(false); + $this->writer->startDocument('1.0', LANG_CHARSET); } - + /** * @param \Intaro\RetailCrm\Model\Bitrix\Xml\XmlData $data */ @@ -42,51 +49,52 @@ class IcmlWriter { $this->writer->startElement('yml_catalog'); $this->writeSimpleAttribute('date', Date('Y-m-d H:i:s')); - + $this->writer->startElement('shop'); $this->writeSimpleElement('name', $data->shopName); $this->writeSimpleElement('company', $data->company); } - + public function writeToXmlBottom(): void { $this->writer->endElement(); $this->writer->endElement(); - $this->writer->flush(); + file_put_contents($this->filePath, $this->writer->flush(true), FILE_APPEND); + $this->writer->endDocument(); } - + /** * @param \Intaro\RetailCrm\Model\Bitrix\Xml\XmlData $data */ public function writeToXmlHeaderAndCategories(XmlData $data): void { $this->writer->startElement('categories'); - + foreach ($data->categories as $key => $category) { $this->writeCategory($category); - + if ( count($data->categories) === $key + 1 || is_int(count($data->categories) / self::CATEGORY_PART) ) { - $this->writer->flush(); + file_put_contents($this->filePath, $this->writer->flush(true), FILE_APPEND); } } - + $this->writer->endElement(); - $this->writer->flush(); + file_put_contents($this->filePath, $this->writer->flush(true), FILE_APPEND); } - + public function startOffersBlock(): void { $this->writer->startElement('offers'); } - + public function endBlock(): void { $this->writer->endElement(); } - + /** * @param XmlOffer[] $offers */ @@ -95,10 +103,10 @@ class IcmlWriter foreach ($offers as $offer) { $this->writeOffer($offer); } - - $this->writer->flush(); + + file_put_contents($this->filePath, $this->writer->flush(true), FILE_APPEND); } - + /** * @param \Intaro\RetailCrm\Model\Bitrix\Xml\XmlOffer $offer */ @@ -108,15 +116,15 @@ class IcmlWriter $this->writeSimpleAttribute('id', $offer->id); $this->writeSimpleAttribute('productId', $offer->productId); $this->writeSimpleAttribute('quantity', $offer->quantity); - + foreach ($offer->categoryIds as $categoryId) { $this->writeSimpleElement('categoryId', $categoryId); } - + if (!empty($offer->picture)) { $this->writeSimpleElement('picture', $offer->picture); } - + if (!empty($offer->unitCode->code)) { $this->writer->startElement('unit'); $this->writeSimpleAttribute('code', $offer->unitCode->code); @@ -124,11 +132,11 @@ class IcmlWriter $this->writeSimpleAttribute('sym', $offer->unitCode->sym); $this->writer->endElement(); } - + foreach ($offer->params as $param) { $this->writeParam($param); } - + $this->writeSimpleElement('url', $offer->url); $this->writeSimpleElement('price', $offer->price); $this->writeSimpleElement('name', $offer->name); @@ -142,7 +150,7 @@ class IcmlWriter $this->writeOptionalSimpleElement('purchasePrice', $offer->purchasePrice); $this->writer->endElement(); } - + /** * Создает ноду, если значение не пустое * @@ -155,7 +163,7 @@ class IcmlWriter $this->writeSimpleElement($name, $value); } } - + /** * @param string $name * @param $value @@ -166,7 +174,7 @@ class IcmlWriter $this->writer->text($this->prepareValue($value)); $this->writer->endElement(); } - + /** * @param string $name * @param $value @@ -177,7 +185,7 @@ class IcmlWriter $this->writer->text($this->prepareValue($value)); $this->writer->endAttribute(); } - + /** * @param $text * @@ -189,7 +197,7 @@ class IcmlWriter return strip_tags($APPLICATION->ConvertCharset($text, 'utf-8', 'utf-8')); } - + /** * @param \Intaro\RetailCrm\Model\Bitrix\Xml\XmlCategory $category */ @@ -202,7 +210,7 @@ class IcmlWriter $this->writeSimpleElement('picture', $category->picture); $this->writer->endElement(); } - + /** * @param \Intaro\RetailCrm\Model\Bitrix\Xml\OfferParam $param */ @@ -214,7 +222,7 @@ class IcmlWriter $this->writer->text($this->prepareValue($param->value)); $this->writer->endElement(); } - + /** * @param string $parentId */ diff --git a/intaro.retailcrm/lib/icml/queryparamsmolder.php b/intaro.retailcrm/lib/icml/queryparamsmolder.php index e5775c50..d7ccd55c 100644 --- a/intaro.retailcrm/lib/icml/queryparamsmolder.php +++ b/intaro.retailcrm/lib/icml/queryparamsmolder.php @@ -50,6 +50,7 @@ class QueryParamsMolder 'PREVIEW_PICTURE', 'DETAIL_PAGE_URL', 'CATALOG_QUANTITY', + 'CATALOG_TYPE', 'CATALOG_PRICE_' . $basePriceId, 'CATALOG_PURCHASING_PRICE', 'EXTERNAL_ID', diff --git a/intaro.retailcrm/lib/icml/xmlofferbuilder.php b/intaro.retailcrm/lib/icml/xmlofferbuilder.php index 2dc20cd4..4d8d3c85 100644 --- a/intaro.retailcrm/lib/icml/xmlofferbuilder.php +++ b/intaro.retailcrm/lib/icml/xmlofferbuilder.php @@ -225,6 +225,7 @@ class XmlOfferBuilder { $this->xmlOffer->id = $item['ID']; $this->xmlOffer->productId = $item['ID']; + $this->xmlOffer->productType = $item['CATALOG_TYPE']; $this->xmlOffer->quantity = $item['CATALOG_QUANTITY'] ?? ''; $this->xmlOffer->url = $item['DETAIL_PAGE_URL'] ? $this->defaultServerName . $item['DETAIL_PAGE_URL'] diff --git a/intaro.retailcrm/lib/model/bitrix/xml/xmloffer.php b/intaro.retailcrm/lib/model/bitrix/xml/xmloffer.php index 9f619b0e..54b63980 100644 --- a/intaro.retailcrm/lib/model/bitrix/xml/xmloffer.php +++ b/intaro.retailcrm/lib/model/bitrix/xml/xmloffer.php @@ -21,104 +21,114 @@ class XmlOffer * @var int */ public $id; - + /** * @var int */ public $productId; - + /** * @var int */ public $quantity; - + /** * @var string */ public $picture; - + /** * @var string */ public $url; - + /** * @var float */ public $price; - + /** * Категории, к которым относится товар * * @var array */ public $categoryIds; - + /** * @var string */ public $name; - + /** * @var int */ public $xmlId; - + /** * @var string */ public $productName; - + /** * @var OfferParam[] */ public $params; - + /** * @var string */ public $vendor; - + /** * @var Unit */ public $unitCode; - + /** * ставка налога (НДС) * * @var string */ public $vatRate; - + /** * штрих-код * * @var string */ public $barcode; - + /** * Закупочная цена * * @var mixed|null */ public $purchasePrice; - + /** * Вес товара * * @var int */ public $weight; - + /** * Габариты товара * * @var string */ public $dimensions; - + + /** + * Тип каталога + * \Bitrix\Catalog\ProductTable::TYPE_PRODUCT - простой товар + * \Bitrix\Catalog\ProductTable::TYPE_SKU – товар с торговыми предложениями + * \Bitrix\Catalog\ProductTable::TYPE_OFFER – торговое предложение + * + * @var mixed + */ + public $productType; + /** * @param $productValue * @param $offerValue diff --git a/intaro.retailcrm/lib/repository/catalogrepository.php b/intaro.retailcrm/lib/repository/catalogrepository.php index e07a044d..8a2a3411 100644 --- a/intaro.retailcrm/lib/repository/catalogrepository.php +++ b/intaro.retailcrm/lib/repository/catalogrepository.php @@ -93,7 +93,7 @@ class CatalogRepository public function getProductPage(SelectParams $param, CatalogIblockInfo $catalogIblockInfo) { return CIBlockElement::GetList( - [], + ['ID' => 'ASC'], $this->builder->getWhereForOfferPart($param->parentId, $catalogIblockInfo), false, ['nPageSize' => $param->nPageSize, 'iNumPage' => $param->pageNumber, 'checkOutOfRange' => true],