1
0
mirror of synced 2025-02-21 14:33:17 +03:00

395 lines
12 KiB
PHP
Raw Normal View History

2017-05-31 15:24:46 +03:00
<?php
2018-03-12 16:34:48 +03:00
2017-05-31 15:24:46 +03:00
namespace Retailcrm\Retailcrm\Model\Icml;
class Icml
{
2018-04-28 11:39:23 +03:00
private $dd;
private $eCategories;
private $eOffers;
private $shop;
private $manager;
private $category;
private $product;
private $storeManager;
private $StockState;
private $configurable;
private $config;
private $dirList;
private $ddFactory;
public function __construct(
\Magento\Store\Model\StoreManagerInterface $manager,
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $product,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\CatalogInventory\Api\StockStateInterface $StockState,
\Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurable,
\Magento\Framework\App\Config\ScopeConfigInterface $config,
\Magento\Framework\DomDocument\DomDocumentFactory $ddFactory,
\Magento\Framework\Filesystem\DirectoryList $dirList
) {
$this->configurable = $configurable;
$this->StockState = $StockState;
$this->storeManager = $storeManager;
$this->product = $product;
$this->category = $categoryCollectionFactory;
$this->manager = $manager;
$this->config = $config;
$this->ddFactory = $ddFactory;
$this->dirList = $dirList;
2017-05-31 15:24:46 +03:00
}
2018-04-28 11:39:23 +03:00
/**
* Generate icml catelog
*
* @return void
*/
2018-03-12 16:34:48 +03:00
public function generate()
{
2018-04-28 11:39:23 +03:00
$this->shop = $this->manager->getStore()->getId();
2017-05-31 15:24:46 +03:00
$string = '<?xml version="1.0" encoding="UTF-8"?>
<yml_catalog date="'.date('Y-m-d H:i:s').'">
<shop>
2018-04-28 11:39:23 +03:00
<name>'.$this->manager->getStore()->getName().'</name>
2017-05-31 15:24:46 +03:00
<categories/>
<offers/>
</shop>
</yml_catalog>
';
2018-04-28 11:39:23 +03:00
$xml = simplexml_load_string(
2017-05-31 15:24:46 +03:00
$string,
2018-04-28 11:39:23 +03:00
'\Magento\Framework\Simplexml\Element',
2017-05-31 15:24:46 +03:00
LIBXML_NOENT | LIBXML_NOCDATA | LIBXML_COMPACT | LIBXML_PARSEHUGE
);
2018-04-28 11:39:23 +03:00
$this->dd = $this->ddFactory->create();
$this->dd->preserveWhiteSpace = false;
$this->dd->formatOutput = true;
$this->dd->loadXML($xml->asXML());
$this->eCategories = $this->dd
->getElementsByTagName('categories')->item(0);
$this->eOffers = $this->dd
2017-05-31 15:24:46 +03:00
->getElementsByTagName('offers')->item(0);
2018-04-28 11:39:23 +03:00
2017-05-31 15:24:46 +03:00
$this->addCategories();
$this->addOffers();
2018-04-28 11:39:23 +03:00
$this->dd->saveXML();
$shopCode = $this->manager->getStore()->getCode();
$this->dd->save($this->dirList->getRoot() . '/retailcrm_' . $shopCode . '.xml');
2017-05-31 15:24:46 +03:00
}
2018-03-12 16:34:48 +03:00
2018-04-28 11:39:23 +03:00
/**
* Add product categories in icml catalog
*
* @return void
*/
2017-05-31 15:24:46 +03:00
private function addCategories()
{
2018-04-28 11:39:23 +03:00
$collection = $this->category->create();
2018-03-12 16:34:48 +03:00
$collection->addAttributeToSelect('*');
foreach ($collection as $category) {
2018-04-28 11:39:23 +03:00
if ($category->getId() > 1) {
$e = $this->eCategories->appendChild(
$this->dd->createElement('category')
2018-03-12 16:34:48 +03:00
);
2018-04-28 11:39:23 +03:00
$e->appendChild($this->dd->createTextNode($category->getName()));
2018-03-12 16:34:48 +03:00
$e->setAttribute('id', $category->getId());
if ($category->getParentId() > 1) {
$e->setAttribute('parentId', $category->getParentId());
2018-04-28 11:39:23 +03:00
}
2018-03-12 16:34:48 +03:00
}
2018-04-28 11:39:23 +03:00
}
2017-05-31 15:24:46 +03:00
}
2018-03-12 16:34:48 +03:00
2018-04-28 11:39:23 +03:00
/**
* Write products in icml catalog
*
* @return void
*/
2017-05-31 15:24:46 +03:00
private function addOffers()
{
2018-04-28 11:39:23 +03:00
$offers = $this->buildOffers();
2018-03-12 16:34:48 +03:00
2018-04-28 11:39:23 +03:00
foreach ($offers as $offer) {
$this->addOffer($offer);
}
}
2018-03-12 16:34:48 +03:00
2018-04-28 11:39:23 +03:00
/**
* Write product in icml catalog
*
* @param array $offer
*
* @return void
*/
private function addOffer($offer)
{
$e = $this->eOffers->appendChild(
$this->dd->createElement('offer')
);
2018-03-12 16:34:48 +03:00
2018-04-28 11:39:23 +03:00
$e->setAttribute('id', $offer['id']);
$e->setAttribute('productId', $offer['productId']);
2018-03-12 16:34:48 +03:00
2018-04-28 11:39:23 +03:00
if (!empty($offer['quantity'])) {
$e->setAttribute('quantity', (int) $offer['quantity']);
} else {
$e->setAttribute('quantity', 0);
}
2018-03-12 16:34:48 +03:00
2018-04-28 11:39:23 +03:00
if (!empty($offer['categoryId'])) {
foreach ($offer['categoryId'] as $categoryId) {
$e->appendChild(
$this->dd->createElement('categoryId')
)->appendChild(
$this->dd->createTextNode($categoryId)
);
2018-03-12 16:34:48 +03:00
}
2018-04-28 11:39:23 +03:00
} else {
$e->appendChild($this->dd->createElement('categoryId', 1));
2018-03-12 16:34:48 +03:00
}
2018-04-28 11:39:23 +03:00
$e->appendChild($this->dd->createElement('productActivity'))
->appendChild(
$this->dd->createTextNode($offer['productActivity'])
);
2018-03-12 16:34:48 +03:00
2018-04-28 11:39:23 +03:00
$e->appendChild($this->dd->createElement('name'))
->appendChild(
$this->dd->createTextNode($offer['name'])
);
2018-03-12 16:34:48 +03:00
2018-04-28 11:39:23 +03:00
$e->appendChild($this->dd->createElement('productName'))
->appendChild(
$this->dd->createTextNode($offer['productName'])
);
2018-03-12 16:34:48 +03:00
2018-04-28 11:39:23 +03:00
$e->appendChild($this->dd->createElement('price'))
->appendChild(
$this->dd->createTextNode($offer['initialPrice'])
);
2018-03-12 16:34:48 +03:00
2018-04-28 11:39:23 +03:00
if (!empty($offer['purchasePrice'])) {
$e->appendChild($this->dd->createElement('purchasePrice'))
2018-03-12 16:34:48 +03:00
->appendChild(
2018-04-28 11:39:23 +03:00
$this->dd->createTextNode($offer['purchasePrice'])
2018-03-12 16:34:48 +03:00
);
2018-04-28 11:39:23 +03:00
}
2018-03-12 16:34:48 +03:00
2018-04-28 11:39:23 +03:00
if (!empty($offer['picture'])) {
$e->appendChild($this->dd->createElement('picture'))
2018-03-12 16:34:48 +03:00
->appendChild(
2018-04-28 11:39:23 +03:00
$this->dd->createTextNode($offer['picture'])
2018-03-12 16:34:48 +03:00
);
2018-04-28 11:39:23 +03:00
}
2018-03-12 16:34:48 +03:00
2018-04-28 11:39:23 +03:00
if (!empty($offer['url'])) {
$e->appendChild($this->dd->createElement('url'))
2018-03-12 16:34:48 +03:00
->appendChild(
2018-04-28 11:39:23 +03:00
$this->dd->createTextNode($offer['url'])
2018-03-12 16:34:48 +03:00
);
2018-04-28 11:39:23 +03:00
}
2018-03-12 16:34:48 +03:00
2018-04-28 11:39:23 +03:00
if (!empty($offer['vendor'])) {
$e->appendChild($this->dd->createElement('vendor'))
2018-03-12 16:34:48 +03:00
->appendChild(
2018-04-28 11:39:23 +03:00
$this->dd->createTextNode($offer['vendor'])
2018-03-12 16:34:48 +03:00
);
2018-04-28 11:39:23 +03:00
}
2018-03-12 16:34:48 +03:00
2018-04-28 11:39:23 +03:00
if (!empty($offer['params'])) {
foreach ($offer['params'] as $param) {
$paramNode = $this->dd->createElement('param');
$paramNode->setAttribute('name', $param['name']);
$paramNode->setAttribute('code', $param['code']);
$paramNode->appendChild(
$this->dd->createTextNode($param['value'])
2018-03-12 16:34:48 +03:00
);
2018-04-28 11:39:23 +03:00
$e->appendChild($paramNode);
2018-03-12 16:34:48 +03:00
}
2018-04-28 11:39:23 +03:00
}
}
2018-03-12 16:34:48 +03:00
2018-04-28 11:39:23 +03:00
/**
* Build offers array
*
* @return array $offers
*/
private function buildOffers()
{
$offers = [];
$collection = $this->product->create();
$collection->addFieldToFilter('visibility', 4);//catalog, search visible
$collection->addAttributeToSelect('*');
$picUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
$customAdditionalAttributes = $this->config->getValue('retailcrm/Misc/attributes_to_export_into_icml');
foreach ($collection as $product) {
if ($product->getTypeId() == 'simple') {
$offers[] = $this->buildOffer($product);
2018-03-12 16:34:48 +03:00
}
2018-04-28 11:39:23 +03:00
if ($product->getTypeId() == 'configurable') {
$associated_products = $this->getAssociatedProducts($product);
foreach ($associated_products as $associatedProduct) {
$offers[] = $this->buildOffer($product, $associatedProduct);
}
2018-03-12 16:34:48 +03:00
}
2018-04-28 11:39:23 +03:00
}
2018-03-12 16:34:48 +03:00
2018-04-28 11:39:23 +03:00
return $offers;
}
/**
* Build offer array
*
* @param object $product
* @param object $associatedProduct
* @return array $offer
*/
private function buildOffer($product, $associatedProduct = null)
{
$offer = [];
$picUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
$offer['id'] = $associatedProduct === null ? $product->getId() : $associatedProduct->getId();
$offer['productId'] = $product->getId();
if ($associatedProduct === null) {
$offer['productActivity'] = $product->isAvailable() ? 'Y' : 'N';
} else {
$offer['productActivity'] = $associatedProduct->isAvailable() ? 'Y' : 'N';
}
$offer['name'] = $associatedProduct === null ? $product->getName() : $associatedProduct->getName();
$offer['productName'] = $product->getName();
$offer['initialPrice'] = $associatedProduct === null
? $product->getFinalPrice()
: $associatedProduct->getFinalPrice();
$offer['url'] = $product->getProductUrl();
if ($associatedProduct === null) {
$offer['picture'] = $picUrl . 'catalog/product' . $product->getImage();
} else {
$offer['picture'] = $picUrl . 'catalog/product' . $associatedProduct->getImage();
}
$offer['quantity'] = $associatedProduct === null
? $this->getStockQuantity($product)
: $this->getStockQuantity($associatedProduct);
$offer['categoryId'] = $associatedProduct === null
? $product->getCategoryIds()
: $associatedProduct->getCategoryIds();
$offer['vendor'] = $associatedProduct === null
? $product->getAttributeText('manufacturer')
: $associatedProduct->getAttributeText('manufacturer');
$offer['params'] = $this->getOfferParams($product, $associatedProduct);
return $offer;
}
/**
* Get parameters offers
*
* @param object $product
* @param object $associatedProduct
* @return array $params
*/
private function getOfferParams($product, $associatedProduct = null)
{
$params = [];
if ($associatedProduct !== null) {
if ($associatedProduct->getResource()->getAttribute('color')) {
$colorAttribute = $associatedProduct->getResource()->getAttribute('color');
$color = $colorAttribute->getSource()->getOptionText($associatedProduct->getColor());
2018-03-12 16:34:48 +03:00
}
2018-04-28 11:39:23 +03:00
if (isset($color)) {
$params[] = [
'name' => 'Color',
'code' => 'color',
'value' => $color
];
2018-03-12 16:34:48 +03:00
}
2018-04-28 11:39:23 +03:00
if ($associatedProduct->getResource()->getAttribute('size')) {
$sizeAttribute = $associatedProduct->getResource()->getAttribute('size');
$size = $sizeAttribute->getSource()->getOptionText($associatedProduct->getSize());
}
if (isset($size)) {
$params[] = [
'name' => 'Size',
'code' => 'size',
'value' => $size
];
}
}
$article = $associatedProduct === null ? $product->getSku() : $associatedProduct->getSku();
if (!empty($article)) {
$params[] = [
'name' => 'Article',
'code' => 'article',
'value' => $article
];
}
$weight = $associatedProduct === null ? $product->getWeight() : $associatedProduct->getWeight();
if (!empty($weight)) {
$params[] = [
'name' => 'Weight',
'code' => 'weight',
'value' => $weight
];
}
return $params;
}
/**
* Get associated products
*
* @param object $product
*
* @return object
*/
private function getAssociatedProducts($product)
{
return $this->configurable
->getUsedProductCollection($product)
->addAttributeToSelect('*')
->addFilterByRequiredOptions();
}
/**
* Get product stock quantity
*
* @param object $offer
* @return int $quantity
*/
private function getStockQuantity($offer)
{
$quantity = $this->StockState->getStockQty(
$offer->getId(),
$offer->getStore()->getWebsiteId()
);
return $quantity;
}
2018-03-12 16:34:48 +03:00
}