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

Commit 15dfd71

Browse files
authored
Merge pull request #23 from afirlejczyk/feature/product-url-path
Add new field for products -> url_path.
2 parents 1020157 + e3504fc commit 15dfd71

File tree

10 files changed

+204
-5
lines changed

10 files changed

+204
-5
lines changed

CHANGELOG.MD

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Changelog
2+
3+
4+
## March 2019
5+
6+
### Added
7+
- Adding 'url_path' for products.
8+
You have to delete existing index from Elasticsearch and run full reindexation.
9+
Need to get correct mapping for new product field.

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
}
2020
],
2121
"require": {
22-
"php": "7.0.2|7.0.4|~7.0.6|~7.1.0",
22+
"php": ">=7.0.2",
2323
"magento/framework": ">=101.0.0",
2424
"magento/module-store": ">=100.2.0",
2525
"magento/module-backend": ">=100.2.0",

src/module-vsbridge-indexer-catalog/Index/Mapping/AbstractMapping.php

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ abstract class AbstractMapping
2222
'level' => FieldInterface::TYPE_INTEGER,
2323
'category_ids' => FieldInterface::TYPE_LONG,
2424
'sku' => FieldInterface::TYPE_KEYWORD,
25+
'url_path' => FieldInterface::TYPE_KEYWORD,
2526
'url_key' => FieldInterface::TYPE_KEYWORD,
2627
];
2728

src/module-vsbridge-indexer-catalog/Model/Attributes/CategoryChildAttributes.php

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class CategoryChildAttributes
1919
private $requiredAttributes = [
2020
'name',
2121
'is_active',
22+
'url_path',
2223
'url_key',
2324
];
2425

src/module-vsbridge-indexer-catalog/Model/ConfigSettings.php

+16-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ class ConfigSettings
1717
{
1818
const CATALOG_SETTINGS_XML_PREFIX = 'vsbridge_indexer_settings/catalog_settings';
1919

20+
/**
21+
* @var array
22+
*/
23+
private $settings = [];
24+
2025
/**
2126
* @var ScopeConfigInterface
2227
*/
@@ -74,12 +79,19 @@ public function getAllowedProductTypes($storeId)
7479
*/
7580
private function getConfigParam(string $configField, $storeId = null)
7681
{
77-
$path = self::CATALOG_SETTINGS_XML_PREFIX . '/' . $configField;
82+
$key = $configField . (string)$storeId;
83+
84+
if (!isset($this->settings[$key])) {
85+
$path = self::CATALOG_SETTINGS_XML_PREFIX . '/' . $configField;
86+
87+
if ($storeId) {
88+
$configValue = $this->scopeConfig->getValue($path, 'stores', $storeId);
89+
}
7890

79-
if ($storeId) {
80-
return $this->scopeConfig->getValue($path, 'stores', $storeId);
91+
$configValue = $this->scopeConfig->getValue($path);
92+
$this->settings[$key] = $configValue;
8193
}
8294

83-
return $this->scopeConfig->getValue($path);
95+
return $this->settings[$key];
8496
}
8597
}

src/module-vsbridge-indexer-catalog/Model/Indexer/DataProvider/Category/AttributeData.php

+5
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,13 @@ private function plotTree(array $categories, $rootId)
189189
private function prepareCategory(array $categoryDTO)
190190
{
191191
$categoryDTO['id'] = (int)$categoryDTO['entity_id'];
192+
192193
$categoryDTO = $this->addSlug($categoryDTO);
193194

195+
if (!isset($categoryDTO['url_path'])) {
196+
$categoryDTO['url_path'] = $categoryDTO['slug'];
197+
}
198+
194199
$categoryDTO = array_diff_key($categoryDTO, array_flip($this->fieldsToRemove));
195200
$categoryDTO = $this->filterData($categoryDTO);
196201

src/module-vsbridge-indexer-catalog/Model/Indexer/DataProvider/Product/AttributeData.php

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Divante\VsbridgeIndexerCore\Indexer\DataFilter;
1414
use Divante\VsbridgeIndexerCatalog\Model\ConfigSettings;
1515
use Divante\VsbridgeIndexerCatalog\Model\SlugGenerator;
16+
use Divante\VsbridgeIndexerCatalog\Model\ProductUrlPathGenerator;
1617

1718
/**
1819
* Class AttributeData
@@ -39,29 +40,40 @@ class AttributeData implements DataProviderInterface
3940
*/
4041
private $slugGenerator;
4142

43+
/**
44+
* @var AttributeDataProvider
45+
*/
46+
private $productUrlPathGenerator;
47+
4248
/**
4349
* AttributeData constructor.
4450
*
51+
* @param ConfigSettings $configSettings
52+
* @param SlugGenerator\Proxy $slugGenerator
53+
* @param ProductUrlPathGenerator $productUrlPathGenerator
4554
* @param DataFilter $dataFilter
4655
* @param AttributeDataProvider $resourceModel
4756
*/
4857
public function __construct(
4958
ConfigSettings $configSettings,
5059
SlugGenerator\Proxy $slugGenerator,
60+
ProductUrlPathGenerator $productUrlPathGenerator,
5161
DataFilter $dataFilter,
5262
AttributeDataProvider $resourceModel
5363
) {
5464
$this->slugGenerator = $slugGenerator;
5565
$this->settings = $configSettings;
5666
$this->resourceModel = $resourceModel;
5767
$this->dataFilter = $dataFilter;
68+
$this->productUrlPathGenerator = $productUrlPathGenerator;
5869
}
5970

6071
/**
6172
* @param array $indexData
6273
* @param int $storeId
6374
*
6475
* @return array
76+
* @throws \Exception
6577
*/
6678
public function addData(array $indexData, $storeId)
6779
{
@@ -81,6 +93,7 @@ public function addData(array $indexData, $storeId)
8193
}
8294

8395
$attributes = null;
96+
$indexData = $this->productUrlPathGenerator->addUrlPath($indexData, $storeId);
8497

8598
return $indexData;
8699
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* @package Divante\VsbridgeIndexerCatalog
4+
* @author Agata Firlejczyk <[email protected]>
5+
* @copyright 2019 Divante Sp. z o.o.
6+
* @license See LICENSE_DIVANTE.txt for license details.
7+
*/
8+
9+
namespace Divante\VsbridgeIndexerCatalog\Model;
10+
11+
use Divante\VsbridgeIndexerCatalog\Model\ResourceModel\Product\Rewrite as RewriteResource;
12+
use Magento\Framework\App\Config\ScopeConfigInterface as ScopeConfigInterface;
13+
14+
/**
15+
* Class ProductUrlPathGenerator
16+
*/
17+
class ProductUrlPathGenerator
18+
{
19+
/**
20+
* @var RewriteResource
21+
*/
22+
private $rewriteResource;
23+
24+
/**
25+
* @var string
26+
*/
27+
private $productUrlSuffix;
28+
29+
/**
30+
* @var ScopeConfigInterface
31+
*/
32+
private $scopeConfig;
33+
34+
/**
35+
* ProductUrlPathGenerator constructor.
36+
*
37+
* @param RewriteResource $rewrite
38+
* @param ScopeConfigInterface $config
39+
*/
40+
public function __construct(
41+
RewriteResource $rewrite,
42+
ScopeConfigInterface $config
43+
) {
44+
$this->scopeConfig = $config;
45+
$this->rewriteResource = $rewrite;
46+
}
47+
48+
/**
49+
* @param array $products
50+
* @param $storeId
51+
*
52+
* @return array
53+
*/
54+
public function addUrlPath(array $products, $storeId)
55+
{
56+
$productIds = array_keys($products);
57+
$urlSuffix = $this->getProductUrlSuffix();
58+
59+
$rewrites = $this->rewriteResource->getRawRewritesData($productIds, $storeId);
60+
61+
foreach ($rewrites as $productId => $rewrite) {
62+
$rewrite = mb_substr($rewrite, 0, -strlen($urlSuffix));
63+
$products[$productId]['url_path'] = $rewrite;
64+
}
65+
66+
return $products;
67+
}
68+
69+
/**
70+
* Retrieve product rewrite suffix for store
71+
*
72+
* @return string
73+
*/
74+
private function getProductUrlSuffix()
75+
{
76+
if (null === $this->productUrlSuffix) {
77+
$this->productUrlSuffix = $this->scopeConfig->getValue(
78+
\Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::XML_PATH_PRODUCT_URL_SUFFIX
79+
);
80+
}
81+
82+
return $this->productUrlSuffix;
83+
}
84+
}

src/module-vsbridge-indexer-catalog/Model/ResourceModel/Product/Prices.php

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Prices
3737
*
3838
* @param ResourceConnection $resourceModel
3939
* @param StoreManagerInterface $storeManager
40+
* @param ProductMetaData $productMetaData
4041
*/
4142
public function __construct(
4243
ResourceConnection $resourceModel,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* @package Divante\VsbridgeIndexerCatalog
4+
* @author Agata Firlejczyk <[email protected]>
5+
* @copyright 2019 Divante Sp. z o.o.
6+
* @license See LICENSE_DIVANTE.txt for license details.
7+
*/
8+
9+
namespace Divante\VsbridgeIndexerCatalog\Model\ResourceModel\Product;
10+
11+
use Magento\Framework\App\ResourceConnection;
12+
use Divante\VsbridgeIndexerCatalog\Model\ProductMetaData;
13+
use Magento\UrlRewrite\Model\Storage\DbStorage;
14+
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
15+
16+
/**
17+
* Class Rewrite
18+
*/
19+
class Rewrite
20+
{
21+
/**
22+
* @var ResourceConnection
23+
*/
24+
private $resource;
25+
26+
/**
27+
* @var ProductMetaData
28+
*/
29+
private $productMetaData;
30+
31+
/**
32+
* Prices constructor.
33+
*
34+
* @param ResourceConnection $resourceModel
35+
* @param ProductMetaData $productMetaData
36+
*/
37+
public function __construct(
38+
ResourceConnection $resourceModel,
39+
ProductMetaData $productMetaData
40+
) {
41+
$this->resource = $resourceModel;
42+
$this->productMetaData = $productMetaData;
43+
}
44+
45+
/**
46+
* @param array $productIds
47+
* @param int $storeId
48+
*
49+
* @return array
50+
*/
51+
public function getRawRewritesData(array $productIds, $storeId)
52+
{
53+
$connection = $this->resource->getConnection();
54+
$select = $this->resource->getConnection()->select();
55+
$select->from(
56+
$this->resource->getTableName(DbStorage::TABLE_NAME),
57+
[
58+
'entity_id',
59+
'request_path',
60+
]
61+
);
62+
63+
$select->where(
64+
UrlRewrite::ENTITY_TYPE . ' = ? ',
65+
\Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::ENTITY_TYPE
66+
);
67+
$select->where('entity_id IN (?)', $productIds);
68+
$select->where('store_id = ? ', $storeId);
69+
$select->where('metadata IS NULL');
70+
71+
return $connection->fetchPairs($select);
72+
}
73+
}

0 commit comments

Comments
 (0)