diff --git a/app/code/Magento/CatalogExportApi/Api/Data/Product.php b/app/code/Magento/CatalogExportApi/Api/Data/Product.php index dc29beb8b..2385d8c1f 100644 --- a/app/code/Magento/CatalogExportApi/Api/Data/Product.php +++ b/app/code/Magento/CatalogExportApi/Api/Data/Product.php @@ -188,6 +188,9 @@ class Product /** @var bool */ private $lowStock; + /** @var \Magento\CatalogExportApi\Api\Data\UrlRewrite[]|null */ + private $urlRewrites; + /** * Get sku * @@ -1342,4 +1345,25 @@ public function setLowStock(?bool $lowStock): void { $this->lowStock = $lowStock; } + + /** + * Get url rewrites + * + * @return \Magento\CatalogExportApi\Api\Data\UrlRewrite[]|null + */ + public function getUrlRewrites(): ?array + { + return $this->urlRewrites; + } + + /** + * Set url rewrites + * + * @param \Magento\CatalogExportApi\Api\Data\UrlRewrite[] $urlRewrites + * @return void + */ + public function setUrlRewrites(?array $urlRewrites = null): void + { + $this->urlRewrites = $urlRewrites; + } } diff --git a/app/code/Magento/CatalogExportApi/Api/Data/UrlRewrite.php b/app/code/Magento/CatalogExportApi/Api/Data/UrlRewrite.php new file mode 100644 index 000000000..1a7e62d65 --- /dev/null +++ b/app/code/Magento/CatalogExportApi/Api/Data/UrlRewrite.php @@ -0,0 +1,73 @@ +url; + } + + /** + * Set url + * + * @param string $url + * @return void + */ + public function setUrl(?string $url): void + { + $this->url = $url; + } + + /** + * Get parameters + * + * @return \Magento\CatalogExportApi\Api\Data\UrlRewriteParameter[]|null + */ + public function getParameters(): ?array + { + return $this->parameters; + } + + /** + * Set parameters + * + * @param \Magento\CatalogExportApi\Api\Data\UrlRewriteParameter[] $parameters + * @return void + */ + public function setParameters(?array $parameters = null): void + { + $this->parameters = $parameters; + } +} diff --git a/app/code/Magento/CatalogExportApi/Api/Data/UrlRewriteParameter.php b/app/code/Magento/CatalogExportApi/Api/Data/UrlRewriteParameter.php new file mode 100644 index 000000000..43583360c --- /dev/null +++ b/app/code/Magento/CatalogExportApi/Api/Data/UrlRewriteParameter.php @@ -0,0 +1,73 @@ +name; + } + + /** + * Set name + * + * @param string $name + * @return void + */ + public function setName(?string $name): void + { + $this->name = $name; + } + + /** + * Get value + * + * @return string + */ + public function getValue(): ?string + { + return $this->value; + } + + /** + * Set value + * + * @param string $value + * @return void + */ + public function setValue(?string $value): void + { + $this->value = $value; + } +} diff --git a/app/code/Magento/CatalogUrlRewriteDataExporter/Model/Provider/Product/UrlRewrites.php b/app/code/Magento/CatalogUrlRewriteDataExporter/Model/Provider/Product/UrlRewrites.php new file mode 100644 index 000000000..67bec3d9f --- /dev/null +++ b/app/code/Magento/CatalogUrlRewriteDataExporter/Model/Provider/Product/UrlRewrites.php @@ -0,0 +1,153 @@ +resourceConnection = $resourceConnection; + $this->urlRewritesQuery = $urlRewritesQuery; + $this->storeManager = $storeManager; + $this->logger = $logger; + } + + /** + * Format UrlRewrite data + * + * @throws \Magento\Framework\Exception\NoSuchEntityException + * @param array $urlRewrite + * @param string $storeViewCode + * @return array + */ + private function format(array $urlRewrite, string $storeViewCode) : array + { + $baseUrl = $this->storeManager->getStore($storeViewCode)->getBaseUrl(UrlInterface::URL_TYPE_WEB); + + return [ + 'productId' => $urlRewrite[UrlRewrite::ENTITY_ID], + 'storeViewCode' => $storeViewCode, + 'url_rewrites' => [ + 'url' => $baseUrl . $urlRewrite[UrlRewrite::REQUEST_PATH], + 'parameters' => $this->getUrlParameters($urlRewrite[UrlRewrite::TARGET_PATH]) + ] + ]; + } + + /** + * Get provider data + * + * @param array $values + * @return array + * @throws UnableRetrieveData + */ + public function get(array $values): array + { + $output = []; + $queryArguments = []; + + try { + foreach ($values as $value) { + $queryArguments['productId'][$value['productId']] = $value['productId']; + $queryArguments['storeViewCode'][$value['storeViewCode']] = $value['storeViewCode']; + } + foreach ($queryArguments['storeViewCode'] as $storeViewCode) { + $urlRewrites = $this->getUrlRewrites($queryArguments, $storeViewCode); + foreach ($urlRewrites ?? [] as $urlRewrite) { + $output[] = $this->format($urlRewrite, $storeViewCode); + } + } + } catch (\Exception $exception) { + $this->logger->error($exception->getMessage()); + throw new UnableRetrieveData('Unable to retrieve url rewrites data'); + } + + return $output; + } + + /** + * Get url rewrites for products and given store. + * + * @param array $queryArguments + * @param string $storeViewCode + * @return array + * @throws \Magento\Framework\Exception\NoSuchEntityException + */ + private function getUrlRewrites(array $queryArguments, string $storeViewCode): array + { + $storeId = (int) $this->storeManager->getStore($storeViewCode)->getId(); + $urlRewritesSelect = $this->urlRewritesQuery->getQuery($queryArguments['productId'], $storeId); + $connection = $this->resourceConnection->getConnection(); + + return $connection->fetchAll($urlRewritesSelect); + } + + /** + * Parses target path and extracts parameters + * + * @param string $targetPath + * @return array + */ + private function getUrlParameters(string $targetPath): array + { + $urlParameters = []; + $targetPathParts = explode('/', trim($targetPath, '/')); + $targetPathPartsCount = count($targetPathParts); + + for ($i = 3; $i < $targetPathPartsCount - 1; $i += 2) { + $urlParameters[] = [ + 'name' => $targetPathParts[$i], + 'value' => $targetPathParts[$i + 1] + ]; + } + + return $urlParameters; + } +} diff --git a/app/code/Magento/CatalogUrlRewriteDataExporter/Model/Query/ProductUrlRewritesQuery.php b/app/code/Magento/CatalogUrlRewriteDataExporter/Model/Query/ProductUrlRewritesQuery.php new file mode 100644 index 000000000..afbfcef3d --- /dev/null +++ b/app/code/Magento/CatalogUrlRewriteDataExporter/Model/Query/ProductUrlRewritesQuery.php @@ -0,0 +1,59 @@ +resourceConnection = $resourceConnection; + } + + /** + * Return query that fetches a list of products' url rewrites. + * + * @param int[] $productIds + * @param int $storeId + * @return Select + */ + public function getQuery(array $productIds, int $storeId): Select + { + $resourceConnection = $this->resourceConnection; + $connection = $resourceConnection->getConnection(); + $urlRewritesTable = $resourceConnection->getTableName('url_rewrite'); + + return $connection->select() + ->from( + ['e' => $urlRewritesTable], + [ + \Magento\UrlRewrite\Service\V1\Data\UrlRewrite::ENTITY_ID, + \Magento\UrlRewrite\Service\V1\Data\UrlRewrite::REQUEST_PATH, + \Magento\UrlRewrite\Service\V1\Data\UrlRewrite::TARGET_PATH, + ] + ) + ->where('entity_id IN (?)', $productIds) + ->where('entity_type = ?', 'product') + ->where('store_id = ?', $storeId); + } +} diff --git a/app/code/Magento/CatalogUrlRewriteDataExporter/etc/et_schema.xml b/app/code/Magento/CatalogUrlRewriteDataExporter/etc/et_schema.xml index 183dab706..9dfb9f5c9 100644 --- a/app/code/Magento/CatalogUrlRewriteDataExporter/etc/et_schema.xml +++ b/app/code/Magento/CatalogUrlRewriteDataExporter/etc/et_schema.xml @@ -14,6 +14,12 @@ + + + + + + + + + + + +