|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\CatalogUrlRewrite\Model\Products; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 11 | +use Magento\Catalog\Model\Product; |
| 12 | +use Magento\Catalog\Model\Product\Visibility; |
| 13 | +use Magento\CatalogUrlRewrite\Model\Product\GetProductUrlRewriteDataByStore; |
| 14 | +use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator; |
| 15 | +use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator; |
| 16 | +use Magento\CatalogUrlRewrite\Service\V1\StoreViewService; |
| 17 | +use Magento\Store\Model\Store; |
| 18 | +use Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException; |
| 19 | +use Magento\UrlRewrite\Model\UrlPersistInterface; |
| 20 | + |
| 21 | +/** |
| 22 | + * Update existing url rewrites or create new ones if needed |
| 23 | + */ |
| 24 | +class AppendUrlRewritesToProducts |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var ProductUrlRewriteGenerator |
| 28 | + */ |
| 29 | + private $productUrlRewriteGenerator; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var StoreViewService |
| 33 | + */ |
| 34 | + private $storeViewService; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var ProductUrlPathGenerator |
| 38 | + */ |
| 39 | + private $productUrlPathGenerator; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var UrlPersistInterface |
| 43 | + */ |
| 44 | + private $urlPersist; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var GetProductUrlRewriteDataByStore |
| 48 | + */ |
| 49 | + private $getDataByStore; |
| 50 | + |
| 51 | + /** |
| 52 | + * @param ProductUrlRewriteGenerator $urlRewriteGenerator |
| 53 | + * @param StoreViewService $storeViewService |
| 54 | + * @param ProductUrlPathGenerator $urlPathGenerator |
| 55 | + * @param UrlPersistInterface $urlPersist |
| 56 | + * @param GetProductUrlRewriteDataByStore $getDataByStore |
| 57 | + */ |
| 58 | + public function __construct( |
| 59 | + ProductUrlRewriteGenerator $urlRewriteGenerator, |
| 60 | + StoreViewService $storeViewService, |
| 61 | + ProductUrlPathGenerator $urlPathGenerator, |
| 62 | + UrlPersistInterface $urlPersist, |
| 63 | + GetProductUrlRewriteDataByStore $getDataByStore |
| 64 | + ) { |
| 65 | + $this->productUrlRewriteGenerator = $urlRewriteGenerator; |
| 66 | + $this->storeViewService = $storeViewService; |
| 67 | + $this->productUrlPathGenerator = $urlPathGenerator; |
| 68 | + $this->urlPersist = $urlPersist; |
| 69 | + $this->getDataByStore = $getDataByStore; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Update existing rewrites and add for specific stores websites |
| 74 | + * |
| 75 | + * @param ProductInterface[] $products |
| 76 | + * @param array $storesToAdd |
| 77 | + * @throws UrlAlreadyExistsException |
| 78 | + */ |
| 79 | + public function execute(array $products, array $storesToAdd): void |
| 80 | + { |
| 81 | + foreach ($products as $product) { |
| 82 | + $forceGenerateDefault = false; |
| 83 | + foreach ($storesToAdd as $storeId) { |
| 84 | + if ($this->needGenerateUrlForStore($product, (int)$storeId)) { |
| 85 | + $urls[] = $this->generateUrls($product, (int)$storeId); |
| 86 | + } elseif ((int)$product->getStoreId() !== Store::DEFAULT_STORE_ID) { |
| 87 | + $forceGenerateDefault = true; |
| 88 | + } |
| 89 | + } |
| 90 | + if ($product->getStoreId() === Store::DEFAULT_STORE_ID |
| 91 | + || $this->isProductAssignedToStore($product)) { |
| 92 | + $product->unsUrlPath(); |
| 93 | + $product->setUrlPath($this->productUrlPathGenerator->getUrlPath($product)); |
| 94 | + $urls[] = $this->productUrlRewriteGenerator->generate($product); |
| 95 | + } |
| 96 | + if ($forceGenerateDefault && $product->getStoreId() !== Store::DEFAULT_STORE_ID) { |
| 97 | + $urls[] = $this->generateUrls($product, Store::DEFAULT_STORE_ID); |
| 98 | + } |
| 99 | + $this->getDataByStore->clearProductUrlRewriteDataCache($product); |
| 100 | + } |
| 101 | + if (!empty($urls)) { |
| 102 | + $this->urlPersist->replace(array_merge(...$urls)); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Generate urls for specific store |
| 108 | + * |
| 109 | + * @param ProductInterface $product |
| 110 | + * @param int $storeId |
| 111 | + * @return array |
| 112 | + */ |
| 113 | + private function generateUrls(ProductInterface $product, int $storeId): array |
| 114 | + { |
| 115 | + $storeData = $this->getDataByStore->execute($product, $storeId); |
| 116 | + $origStoreId = $product->getStoreId(); |
| 117 | + $origVisibility = $product->getVisibility(); |
| 118 | + $origUrlKey = $product->getUrlKey(); |
| 119 | + $product->setStoreId($storeId); |
| 120 | + $product->setVisibility($storeData['visibility'] ?? Visibility::VISIBILITY_NOT_VISIBLE); |
| 121 | + $product->setUrlKey($storeData['url_key'] ?? ''); |
| 122 | + $product->unsUrlPath(); |
| 123 | + $product->setUrlPath($this->productUrlPathGenerator->getUrlPath($product)); |
| 124 | + $urls = $this->productUrlRewriteGenerator->generate($product); |
| 125 | + $product->setStoreId($origStoreId); |
| 126 | + $product->setVisibility($origVisibility); |
| 127 | + $product->setUrlKey($origUrlKey); |
| 128 | + |
| 129 | + return $urls; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Does product has scope overridden url key value |
| 134 | + * |
| 135 | + * @param ProductInterface $product |
| 136 | + * @param int $storeId |
| 137 | + * @return bool |
| 138 | + */ |
| 139 | + private function needGenerateUrlForStore(ProductInterface $product, int $storeId): bool |
| 140 | + { |
| 141 | + return (int)$product->getStoreId() !== $storeId |
| 142 | + && $this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore( |
| 143 | + $storeId, |
| 144 | + $product->getId(), |
| 145 | + Product::ENTITY |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Is product still assigned to store which request is performed from |
| 151 | + * |
| 152 | + * @param ProductInterface $product |
| 153 | + * @return bool |
| 154 | + */ |
| 155 | + private function isProductAssignedToStore(ProductInterface $product): bool |
| 156 | + { |
| 157 | + return in_array($product->getStoreId(), $product->getStoreIds()); |
| 158 | + } |
| 159 | +} |
0 commit comments