Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Add new field for products -> url_path. #23

Merged
merged 1 commit into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Changelog


## March 2019

### Added
- Adding 'url_path' for products.
You have to delete existing index from Elasticsearch and run full reindexation.
Need to get correct mapping for new product field.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
],
"require": {
"php": "7.0.2|7.0.4|~7.0.6|~7.1.0",
"php": ">=7.0.2",
"magento/framework": ">=101.0.0",
"magento/module-store": ">=100.2.0",
"magento/module-backend": ">=100.2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ abstract class AbstractMapping
'level' => FieldInterface::TYPE_INTEGER,
'category_ids' => FieldInterface::TYPE_LONG,
'sku' => FieldInterface::TYPE_KEYWORD,
'url_path' => FieldInterface::TYPE_KEYWORD,
'url_key' => FieldInterface::TYPE_KEYWORD,
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CategoryChildAttributes
private $requiredAttributes = [
'name',
'is_active',
'url_path',
'url_key',
];

Expand Down
20 changes: 16 additions & 4 deletions src/module-vsbridge-indexer-catalog/Model/ConfigSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class ConfigSettings
{
const CATALOG_SETTINGS_XML_PREFIX = 'vsbridge_indexer_settings/catalog_settings';

/**
* @var array
*/
private $settings = [];

/**
* @var ScopeConfigInterface
*/
Expand Down Expand Up @@ -74,12 +79,19 @@ public function getAllowedProductTypes($storeId)
*/
private function getConfigParam(string $configField, $storeId = null)
{
$path = self::CATALOG_SETTINGS_XML_PREFIX . '/' . $configField;
$key = $configField . (string)$storeId;

if (!isset($this->settings[$key])) {
$path = self::CATALOG_SETTINGS_XML_PREFIX . '/' . $configField;

if ($storeId) {
$configValue = $this->scopeConfig->getValue($path, 'stores', $storeId);
}

if ($storeId) {
return $this->scopeConfig->getValue($path, 'stores', $storeId);
$configValue = $this->scopeConfig->getValue($path);
$this->settings[$key] = $configValue;
}

return $this->scopeConfig->getValue($path);
return $this->settings[$key];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,13 @@ private function plotTree(array $categories, $rootId)
private function prepareCategory(array $categoryDTO)
{
$categoryDTO['id'] = (int)$categoryDTO['entity_id'];

$categoryDTO = $this->addSlug($categoryDTO);

if (!isset($categoryDTO['url_path'])) {
$categoryDTO['url_path'] = $categoryDTO['slug'];
}

$categoryDTO = array_diff_key($categoryDTO, array_flip($this->fieldsToRemove));
$categoryDTO = $this->filterData($categoryDTO);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Divante\VsbridgeIndexerCore\Indexer\DataFilter;
use Divante\VsbridgeIndexerCatalog\Model\ConfigSettings;
use Divante\VsbridgeIndexerCatalog\Model\SlugGenerator;
use Divante\VsbridgeIndexerCatalog\Model\ProductUrlPathGenerator;

/**
* Class AttributeData
Expand All @@ -39,29 +40,40 @@ class AttributeData implements DataProviderInterface
*/
private $slugGenerator;

/**
* @var AttributeDataProvider
*/
private $productUrlPathGenerator;

/**
* AttributeData constructor.
*
* @param ConfigSettings $configSettings
* @param SlugGenerator\Proxy $slugGenerator
* @param ProductUrlPathGenerator $productUrlPathGenerator
* @param DataFilter $dataFilter
* @param AttributeDataProvider $resourceModel
*/
public function __construct(
ConfigSettings $configSettings,
SlugGenerator\Proxy $slugGenerator,
ProductUrlPathGenerator $productUrlPathGenerator,
DataFilter $dataFilter,
AttributeDataProvider $resourceModel
) {
$this->slugGenerator = $slugGenerator;
$this->settings = $configSettings;
$this->resourceModel = $resourceModel;
$this->dataFilter = $dataFilter;
$this->productUrlPathGenerator = $productUrlPathGenerator;
}

/**
* @param array $indexData
* @param int $storeId
*
* @return array
* @throws \Exception
*/
public function addData(array $indexData, $storeId)
{
Expand All @@ -81,6 +93,7 @@ public function addData(array $indexData, $storeId)
}

$attributes = null;
$indexData = $this->productUrlPathGenerator->addUrlPath($indexData, $storeId);

return $indexData;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* @package Divante\VsbridgeIndexerCatalog
* @author Agata Firlejczyk <[email protected]>
* @copyright 2019 Divante Sp. z o.o.
* @license See LICENSE_DIVANTE.txt for license details.
*/

namespace Divante\VsbridgeIndexerCatalog\Model;

use Divante\VsbridgeIndexerCatalog\Model\ResourceModel\Product\Rewrite as RewriteResource;
use Magento\Framework\App\Config\ScopeConfigInterface as ScopeConfigInterface;

/**
* Class ProductUrlPathGenerator
*/
class ProductUrlPathGenerator
{
/**
* @var RewriteResource
*/
private $rewriteResource;

/**
* @var string
*/
private $productUrlSuffix;

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* ProductUrlPathGenerator constructor.
*
* @param RewriteResource $rewrite
* @param ScopeConfigInterface $config
*/
public function __construct(
RewriteResource $rewrite,
ScopeConfigInterface $config
) {
$this->scopeConfig = $config;
$this->rewriteResource = $rewrite;
}

/**
* @param array $products
* @param $storeId
*
* @return array
*/
public function addUrlPath(array $products, $storeId)
{
$productIds = array_keys($products);
$urlSuffix = $this->getProductUrlSuffix();

$rewrites = $this->rewriteResource->getRawRewritesData($productIds, $storeId);

foreach ($rewrites as $productId => $rewrite) {
$rewrite = mb_substr($rewrite, 0, -strlen($urlSuffix));
$products[$productId]['url_path'] = $rewrite;
}

return $products;
}

/**
* Retrieve product rewrite suffix for store
*
* @return string
*/
private function getProductUrlSuffix()
{
if (null === $this->productUrlSuffix) {
$this->productUrlSuffix = $this->scopeConfig->getValue(
\Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::XML_PATH_PRODUCT_URL_SUFFIX
);
}

return $this->productUrlSuffix;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Prices
*
* @param ResourceConnection $resourceModel
* @param StoreManagerInterface $storeManager
* @param ProductMetaData $productMetaData
*/
public function __construct(
ResourceConnection $resourceModel,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* @package Divante\VsbridgeIndexerCatalog
* @author Agata Firlejczyk <[email protected]>
* @copyright 2019 Divante Sp. z o.o.
* @license See LICENSE_DIVANTE.txt for license details.
*/

namespace Divante\VsbridgeIndexerCatalog\Model\ResourceModel\Product;

use Magento\Framework\App\ResourceConnection;
use Divante\VsbridgeIndexerCatalog\Model\ProductMetaData;
use Magento\UrlRewrite\Model\Storage\DbStorage;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;

/**
* Class Rewrite
*/
class Rewrite
{
/**
* @var ResourceConnection
*/
private $resource;

/**
* @var ProductMetaData
*/
private $productMetaData;

/**
* Prices constructor.
*
* @param ResourceConnection $resourceModel
* @param ProductMetaData $productMetaData
*/
public function __construct(
ResourceConnection $resourceModel,
ProductMetaData $productMetaData
) {
$this->resource = $resourceModel;
$this->productMetaData = $productMetaData;
}

/**
* @param array $productIds
* @param int $storeId
*
* @return array
*/
public function getRawRewritesData(array $productIds, $storeId)
{
$connection = $this->resource->getConnection();
$select = $this->resource->getConnection()->select();
$select->from(
$this->resource->getTableName(DbStorage::TABLE_NAME),
[
'entity_id',
'request_path',
]
);

$select->where(
UrlRewrite::ENTITY_TYPE . ' = ? ',
\Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::ENTITY_TYPE
);
$select->where('entity_id IN (?)', $productIds);
$select->where('store_id = ? ', $storeId);
$select->where('metadata IS NULL');

return $connection->fetchPairs($select);
}
}