This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/2.3-develop' into 2.3-develop-gr…
…aphql-PR-yogesh-5
- Loading branch information
Showing
67 changed files
with
1,910 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
app/code/Magento/CatalogUrlRewrite/Model/Products/AdaptUrlRewritesToVisibilityAttribute.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogUrlRewrite\Model\Products; | ||
|
||
use Magento\Catalog\Api\Data\ProductInterface; | ||
use Magento\Catalog\Model\Product; | ||
use Magento\Catalog\Model\Product\Visibility; | ||
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; | ||
use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator; | ||
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator; | ||
use Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException; | ||
use Magento\UrlRewrite\Model\UrlPersistInterface; | ||
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; | ||
|
||
/** | ||
* Save/Delete UrlRewrites by Product ID's and visibility | ||
*/ | ||
class AdaptUrlRewritesToVisibilityAttribute | ||
{ | ||
/** | ||
* @var CollectionFactory | ||
*/ | ||
private $productCollectionFactory; | ||
|
||
/** | ||
* @var ProductUrlRewriteGenerator | ||
*/ | ||
private $urlRewriteGenerator; | ||
|
||
/** | ||
* @var UrlPersistInterface | ||
*/ | ||
private $urlPersist; | ||
|
||
/** | ||
* @var ProductUrlPathGenerator | ||
*/ | ||
private $urlPathGenerator; | ||
|
||
/** | ||
* @param CollectionFactory $collectionFactory | ||
* @param ProductUrlRewriteGenerator $urlRewriteGenerator | ||
* @param UrlPersistInterface $urlPersist | ||
* @param ProductUrlPathGenerator|null $urlPathGenerator | ||
*/ | ||
public function __construct( | ||
CollectionFactory $collectionFactory, | ||
ProductUrlRewriteGenerator $urlRewriteGenerator, | ||
UrlPersistInterface $urlPersist, | ||
ProductUrlPathGenerator $urlPathGenerator | ||
) { | ||
$this->productCollectionFactory = $collectionFactory; | ||
$this->urlRewriteGenerator = $urlRewriteGenerator; | ||
$this->urlPersist = $urlPersist; | ||
$this->urlPathGenerator = $urlPathGenerator; | ||
} | ||
|
||
/** | ||
* Process Url Rewrites according to the products visibility attribute | ||
* | ||
* @param array $productIds | ||
* @param int $visibility | ||
* @throws UrlAlreadyExistsException | ||
*/ | ||
public function execute(array $productIds, int $visibility): void | ||
{ | ||
$products = $this->getProductsByIds($productIds); | ||
|
||
/** @var Product $product */ | ||
foreach ($products as $product) { | ||
if ($visibility == Visibility::VISIBILITY_NOT_VISIBLE) { | ||
$this->urlPersist->deleteByData( | ||
[ | ||
UrlRewrite::ENTITY_ID => $product->getId(), | ||
UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE, | ||
] | ||
); | ||
} elseif ($visibility !== Visibility::VISIBILITY_NOT_VISIBLE) { | ||
$product->setVisibility($visibility); | ||
$productUrlPath = $this->urlPathGenerator->getUrlPath($product); | ||
$productUrlRewrite = $this->urlRewriteGenerator->generate($product); | ||
$product->unsUrlPath(); | ||
$product->setUrlPath($productUrlPath); | ||
|
||
try { | ||
$this->urlPersist->replace($productUrlRewrite); | ||
} catch (UrlAlreadyExistsException $e) { | ||
throw new UrlAlreadyExistsException( | ||
__( | ||
'Can not change the visibility of the product with SKU equals "%1". ' | ||
. 'URL key "%2" for specified store already exists.', | ||
$product->getSku(), | ||
$product->getUrlKey() | ||
), | ||
$e, | ||
$e->getCode(), | ||
$e->getUrls() | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Get Product Models by Id's | ||
* | ||
* @param array $productIds | ||
* @return array | ||
*/ | ||
private function getProductsByIds(array $productIds): array | ||
{ | ||
$productCollection = $this->productCollectionFactory->create(); | ||
$productCollection->addAttributeToSelect(ProductInterface::VISIBILITY); | ||
$productCollection->addAttributeToSelect('url_key'); | ||
$productCollection->addFieldToFilter( | ||
'entity_id', | ||
['in' => array_unique($productIds)] | ||
); | ||
|
||
return $productCollection->getItems(); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...Magento/CatalogUrlRewrite/Observer/ProcessUrlRewriteOnChangeProductVisibilityObserver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogUrlRewrite\Observer; | ||
|
||
use Magento\Catalog\Api\Data\ProductInterface; | ||
use Magento\CatalogUrlRewrite\Model\Products\AdaptUrlRewritesToVisibilityAttribute; | ||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Event\ObserverInterface; | ||
use Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException; | ||
|
||
/** | ||
* Consider URL rewrites on change product visibility via mass action | ||
*/ | ||
class ProcessUrlRewriteOnChangeProductVisibilityObserver implements ObserverInterface | ||
{ | ||
/** | ||
* @var AdaptUrlRewritesToVisibilityAttribute | ||
*/ | ||
private $adaptUrlRewritesToVisibility; | ||
|
||
/** | ||
* @param AdaptUrlRewritesToVisibilityAttribute $adaptUrlRewritesToVisibility | ||
*/ | ||
public function __construct(AdaptUrlRewritesToVisibilityAttribute $adaptUrlRewritesToVisibility) | ||
{ | ||
$this->adaptUrlRewritesToVisibility = $adaptUrlRewritesToVisibility; | ||
} | ||
|
||
/** | ||
* Generate urls for UrlRewrites and save it in storage | ||
* | ||
* @param Observer $observer | ||
* @return void | ||
* @throws UrlAlreadyExistsException | ||
*/ | ||
public function execute(Observer $observer) | ||
{ | ||
$event = $observer->getEvent(); | ||
$attrData = $event->getAttributesData(); | ||
$productIds = $event->getProductIds(); | ||
$visibility = $attrData[ProductInterface::VISIBILITY] ?? 0; | ||
|
||
if (!$visibility || !$productIds) { | ||
return; | ||
} | ||
|
||
$this->adaptUrlRewritesToVisibility->execute($productIds, (int)$visibility); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
* See COPYING.txt for license details. | ||
*/ | ||
|
||
// @deprecated | ||
// @codingStandardsIgnoreFile | ||
|
||
?> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.