1
0
mirror of synced 2024-11-29 00:36:07 +03:00

Catalog fix

* add empty array Instead of  and add xml-header with charsets
* fix catalog purchasePrice
* use file_put_contents for flush
This commit is contained in:
Сергей Чазов 2021-11-02 11:25:26 +03:00 committed by GitHub
parent 380e1f0081
commit 3e5a04af62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 82 additions and 56 deletions

View File

@ -25,7 +25,7 @@ CModule::IncludeModule('intaro.retailcrm');
//TODO заменить вызов на сервис-локатор, когда он приедет //TODO заменить вызов на сервис-локатор, когда он приедет
$settingsService = SettingsService::getInstance( $settingsService = SettingsService::getInstance(
$arOldSetupVars, $arOldSetupVars ?? [],
$ACTION $ACTION
); );

View File

@ -2,8 +2,7 @@
namespace Intaro\RetailCrm\Icml; namespace Intaro\RetailCrm\Icml;
use COption; use Bitrix\Catalog\ProductTable;
use Intaro\RetailCrm\Icml\Utils\IcmlUtils;
use Intaro\RetailCrm\Model\Bitrix\Orm\CatalogIblockInfo; use Intaro\RetailCrm\Model\Bitrix\Orm\CatalogIblockInfo;
use Intaro\RetailCrm\Model\Bitrix\Xml\SelectParams; use Intaro\RetailCrm\Model\Bitrix\Xml\SelectParams;
use Intaro\RetailCrm\Model\Bitrix\Xml\XmlData; use Intaro\RetailCrm\Model\Bitrix\Xml\XmlData;
@ -178,6 +177,7 @@ class IcmlDirector
$this->setup->properties->products->names[$productIblockId], $this->setup->properties->products->names[$productIblockId],
$this->setup->basePriceId $this->setup->basePriceId
); );
$paramsForOffer $paramsForOffer
= $this->queryBuilder->getSelectParams( = $this->queryBuilder->getSelectParams(
$this->setup->properties->sku->names[$productIblockId], $this->setup->properties->sku->names[$productIblockId],
@ -249,6 +249,12 @@ class IcmlDirector
$paramsForOffer->allParams = array_merge($paramsForOffer->configurable, $paramsForOffer->main); $paramsForOffer->allParams = array_merge($paramsForOffer->configurable, $paramsForOffer->main);
do { do {
//Если каталог проиндексирован, у товара есть Тип и это простой товар, то просто записываем его
if ($product->productType = ProductTable::TYPE_PRODUCT) {
$this->icmlWriter->writeOffers([$product]);
break;
}
$xmlOffersPart $xmlOffersPart
= $this->xmlOfferDirector->getXmlOffersBySingleProduct($paramsForOffer, $catalogIblockInfo, $product); = $this->xmlOfferDirector->getXmlOffersBySingleProduct($paramsForOffer, $catalogIblockInfo, $product);

View File

@ -18,23 +18,30 @@ class IcmlWriter
{ {
public const INFO = 'INFO'; public const INFO = 'INFO';
public const CATEGORY_PART = 1000; public const CATEGORY_PART = 1000;
/** /**
* @var \XMLWriter * @var \XMLWriter
*/ */
private $writer; private $writer;
/**
* @var string
*/
private $filePath;
/** /**
* IcmlWriter constructor. * 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 = new XMLWriter();
$this->writer->openURI($_SERVER['DOCUMENT_ROOT'] . $filePath); $this->writer->openMemory();
$this->writer->setIndent(true); $this->writer->setIndent(false);
$this->writer->startDocument('1.0', LANG_CHARSET);
} }
/** /**
* @param \Intaro\RetailCrm\Model\Bitrix\Xml\XmlData $data * @param \Intaro\RetailCrm\Model\Bitrix\Xml\XmlData $data
*/ */
@ -42,51 +49,52 @@ class IcmlWriter
{ {
$this->writer->startElement('yml_catalog'); $this->writer->startElement('yml_catalog');
$this->writeSimpleAttribute('date', Date('Y-m-d H:i:s')); $this->writeSimpleAttribute('date', Date('Y-m-d H:i:s'));
$this->writer->startElement('shop'); $this->writer->startElement('shop');
$this->writeSimpleElement('name', $data->shopName); $this->writeSimpleElement('name', $data->shopName);
$this->writeSimpleElement('company', $data->company); $this->writeSimpleElement('company', $data->company);
} }
public function writeToXmlBottom(): void public function writeToXmlBottom(): void
{ {
$this->writer->endElement(); $this->writer->endElement();
$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 * @param \Intaro\RetailCrm\Model\Bitrix\Xml\XmlData $data
*/ */
public function writeToXmlHeaderAndCategories(XmlData $data): void public function writeToXmlHeaderAndCategories(XmlData $data): void
{ {
$this->writer->startElement('categories'); $this->writer->startElement('categories');
foreach ($data->categories as $key => $category) { foreach ($data->categories as $key => $category) {
$this->writeCategory($category); $this->writeCategory($category);
if ( if (
count($data->categories) === $key + 1 count($data->categories) === $key + 1
|| is_int(count($data->categories) / self::CATEGORY_PART) || 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->endElement();
$this->writer->flush(); file_put_contents($this->filePath, $this->writer->flush(true), FILE_APPEND);
} }
public function startOffersBlock(): void public function startOffersBlock(): void
{ {
$this->writer->startElement('offers'); $this->writer->startElement('offers');
} }
public function endBlock(): void public function endBlock(): void
{ {
$this->writer->endElement(); $this->writer->endElement();
} }
/** /**
* @param XmlOffer[] $offers * @param XmlOffer[] $offers
*/ */
@ -95,10 +103,10 @@ class IcmlWriter
foreach ($offers as $offer) { foreach ($offers as $offer) {
$this->writeOffer($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 * @param \Intaro\RetailCrm\Model\Bitrix\Xml\XmlOffer $offer
*/ */
@ -108,15 +116,15 @@ class IcmlWriter
$this->writeSimpleAttribute('id', $offer->id); $this->writeSimpleAttribute('id', $offer->id);
$this->writeSimpleAttribute('productId', $offer->productId); $this->writeSimpleAttribute('productId', $offer->productId);
$this->writeSimpleAttribute('quantity', $offer->quantity); $this->writeSimpleAttribute('quantity', $offer->quantity);
foreach ($offer->categoryIds as $categoryId) { foreach ($offer->categoryIds as $categoryId) {
$this->writeSimpleElement('categoryId', $categoryId); $this->writeSimpleElement('categoryId', $categoryId);
} }
if (!empty($offer->picture)) { if (!empty($offer->picture)) {
$this->writeSimpleElement('picture', $offer->picture); $this->writeSimpleElement('picture', $offer->picture);
} }
if (!empty($offer->unitCode->code)) { if (!empty($offer->unitCode->code)) {
$this->writer->startElement('unit'); $this->writer->startElement('unit');
$this->writeSimpleAttribute('code', $offer->unitCode->code); $this->writeSimpleAttribute('code', $offer->unitCode->code);
@ -124,11 +132,11 @@ class IcmlWriter
$this->writeSimpleAttribute('sym', $offer->unitCode->sym); $this->writeSimpleAttribute('sym', $offer->unitCode->sym);
$this->writer->endElement(); $this->writer->endElement();
} }
foreach ($offer->params as $param) { foreach ($offer->params as $param) {
$this->writeParam($param); $this->writeParam($param);
} }
$this->writeSimpleElement('url', $offer->url); $this->writeSimpleElement('url', $offer->url);
$this->writeSimpleElement('price', $offer->price); $this->writeSimpleElement('price', $offer->price);
$this->writeSimpleElement('name', $offer->name); $this->writeSimpleElement('name', $offer->name);
@ -142,7 +150,7 @@ class IcmlWriter
$this->writeOptionalSimpleElement('purchasePrice', $offer->purchasePrice); $this->writeOptionalSimpleElement('purchasePrice', $offer->purchasePrice);
$this->writer->endElement(); $this->writer->endElement();
} }
/** /**
* Создает ноду, если значение не пустое * Создает ноду, если значение не пустое
* *
@ -155,7 +163,7 @@ class IcmlWriter
$this->writeSimpleElement($name, $value); $this->writeSimpleElement($name, $value);
} }
} }
/** /**
* @param string $name * @param string $name
* @param $value * @param $value
@ -166,7 +174,7 @@ class IcmlWriter
$this->writer->text($this->prepareValue($value)); $this->writer->text($this->prepareValue($value));
$this->writer->endElement(); $this->writer->endElement();
} }
/** /**
* @param string $name * @param string $name
* @param $value * @param $value
@ -177,7 +185,7 @@ class IcmlWriter
$this->writer->text($this->prepareValue($value)); $this->writer->text($this->prepareValue($value));
$this->writer->endAttribute(); $this->writer->endAttribute();
} }
/** /**
* @param $text * @param $text
* *
@ -189,7 +197,7 @@ class IcmlWriter
return strip_tags($APPLICATION->ConvertCharset($text, 'utf-8', 'utf-8')); return strip_tags($APPLICATION->ConvertCharset($text, 'utf-8', 'utf-8'));
} }
/** /**
* @param \Intaro\RetailCrm\Model\Bitrix\Xml\XmlCategory $category * @param \Intaro\RetailCrm\Model\Bitrix\Xml\XmlCategory $category
*/ */
@ -202,7 +210,7 @@ class IcmlWriter
$this->writeSimpleElement('picture', $category->picture); $this->writeSimpleElement('picture', $category->picture);
$this->writer->endElement(); $this->writer->endElement();
} }
/** /**
* @param \Intaro\RetailCrm\Model\Bitrix\Xml\OfferParam $param * @param \Intaro\RetailCrm\Model\Bitrix\Xml\OfferParam $param
*/ */
@ -214,7 +222,7 @@ class IcmlWriter
$this->writer->text($this->prepareValue($param->value)); $this->writer->text($this->prepareValue($param->value));
$this->writer->endElement(); $this->writer->endElement();
} }
/** /**
* @param string $parentId * @param string $parentId
*/ */

View File

@ -50,6 +50,7 @@ class QueryParamsMolder
'PREVIEW_PICTURE', 'PREVIEW_PICTURE',
'DETAIL_PAGE_URL', 'DETAIL_PAGE_URL',
'CATALOG_QUANTITY', 'CATALOG_QUANTITY',
'CATALOG_TYPE',
'CATALOG_PRICE_' . $basePriceId, 'CATALOG_PRICE_' . $basePriceId,
'CATALOG_PURCHASING_PRICE', 'CATALOG_PURCHASING_PRICE',
'EXTERNAL_ID', 'EXTERNAL_ID',

View File

@ -225,6 +225,7 @@ class XmlOfferBuilder
{ {
$this->xmlOffer->id = $item['ID']; $this->xmlOffer->id = $item['ID'];
$this->xmlOffer->productId = $item['ID']; $this->xmlOffer->productId = $item['ID'];
$this->xmlOffer->productType = $item['CATALOG_TYPE'];
$this->xmlOffer->quantity = $item['CATALOG_QUANTITY'] ?? ''; $this->xmlOffer->quantity = $item['CATALOG_QUANTITY'] ?? '';
$this->xmlOffer->url = $item['DETAIL_PAGE_URL'] $this->xmlOffer->url = $item['DETAIL_PAGE_URL']
? $this->defaultServerName . $item['DETAIL_PAGE_URL'] ? $this->defaultServerName . $item['DETAIL_PAGE_URL']

View File

@ -21,104 +21,114 @@ class XmlOffer
* @var int * @var int
*/ */
public $id; public $id;
/** /**
* @var int * @var int
*/ */
public $productId; public $productId;
/** /**
* @var int * @var int
*/ */
public $quantity; public $quantity;
/** /**
* @var string * @var string
*/ */
public $picture; public $picture;
/** /**
* @var string * @var string
*/ */
public $url; public $url;
/** /**
* @var float * @var float
*/ */
public $price; public $price;
/** /**
* Категории, к которым относится товар * Категории, к которым относится товар
* *
* @var array * @var array
*/ */
public $categoryIds; public $categoryIds;
/** /**
* @var string * @var string
*/ */
public $name; public $name;
/** /**
* @var int * @var int
*/ */
public $xmlId; public $xmlId;
/** /**
* @var string * @var string
*/ */
public $productName; public $productName;
/** /**
* @var OfferParam[] * @var OfferParam[]
*/ */
public $params; public $params;
/** /**
* @var string * @var string
*/ */
public $vendor; public $vendor;
/** /**
* @var Unit * @var Unit
*/ */
public $unitCode; public $unitCode;
/** /**
* ставка налога (НДС) * ставка налога (НДС)
* *
* @var string * @var string
*/ */
public $vatRate; public $vatRate;
/** /**
* штрих-код * штрих-код
* *
* @var string * @var string
*/ */
public $barcode; public $barcode;
/** /**
* Закупочная цена * Закупочная цена
* *
* @var mixed|null * @var mixed|null
*/ */
public $purchasePrice; public $purchasePrice;
/** /**
* Вес товара * Вес товара
* *
* @var int * @var int
*/ */
public $weight; public $weight;
/** /**
* Габариты товара * Габариты товара
* *
* @var string * @var string
*/ */
public $dimensions; public $dimensions;
/**
* Тип каталога
* \Bitrix\Catalog\ProductTable::TYPE_PRODUCT - простой товар
* \Bitrix\Catalog\ProductTable::TYPE_SKU товар с торговыми предложениями
* \Bitrix\Catalog\ProductTable::TYPE_OFFER торговое предложение
*
* @var mixed
*/
public $productType;
/** /**
* @param $productValue * @param $productValue
* @param $offerValue * @param $offerValue

View File

@ -93,7 +93,7 @@ class CatalogRepository
public function getProductPage(SelectParams $param, CatalogIblockInfo $catalogIblockInfo) public function getProductPage(SelectParams $param, CatalogIblockInfo $catalogIblockInfo)
{ {
return CIBlockElement::GetList( return CIBlockElement::GetList(
[], ['ID' => 'ASC'],
$this->builder->getWhereForOfferPart($param->parentId, $catalogIblockInfo), $this->builder->getWhereForOfferPart($param->parentId, $catalogIblockInfo),
false, false,
['nPageSize' => $param->nPageSize, 'iNumPage' => $param->pageNumber, 'checkOutOfRange' => true], ['nPageSize' => $param->nPageSize, 'iNumPage' => $param->pageNumber, 'checkOutOfRange' => true],