-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
20434 consider url rewrite when change product visibility attribute 2 3 #20774
Merged
magento-engcom-team
merged 11 commits into
magento:2.3-develop
from
VitaliyBoyko:20434-consider-url-rewrite-when-change-product-visibility-attribute-2-3
Mar 26, 2019
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
24382e3
ENGCOM-20434: Consider deleting/creating url rewrites on change the p…
4d18c9a
ENGCOM-20434: Added accidentally removed code
6a5776c
ENGCOM-20434: Removed url key validation for invisible products
f2088d9
ENGCOM-20434: Removed url key validation for invisible products (reve…
4edec38
ENGCOM-20434: Removed url key validation for invisible products (reve…
a8d1dc0
ENGCOM-20434: static fixes
5687704
Merge branch '2.3-develop' of github.com:magento/magento2 into 20434-…
c865bdc
ENGCOM-20434: refactoring, test coverage
1c608d7
ENGCOM-20434: refactoring, doc blocks adjustments
a80943f
ENGCOM-20434: fixed type inconsistency
a8d9be1
ENGCOM-4357: Static tests fix.
p-bystritsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
...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,111 @@ | ||
<?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\Catalog\Model\Product\Visibility; | ||
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; | ||
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; | ||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Event\ObserverInterface; | ||
|
||
/** | ||
* Class ProductProcessUrlRewriteSavingObserver | ||
*/ | ||
class ProcessUrlRewriteOnChangeProductVisibilityObserver implements ObserverInterface | ||
{ | ||
/** | ||
* @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; | ||
} | ||
|
||
/** | ||
* Generate urls for UrlRewrites and save it in storage | ||
* | ||
* @param Observer $observer | ||
* @return array | ||
* @throws UrlAlreadyExistsException | ||
*/ | ||
public function execute(Observer $observer) | ||
{ | ||
$event = $observer->getEvent(); | ||
$attrData = $event->getAttributesData(); | ||
$productIds = $event->getProductIds(); | ||
$storeId = $event->getStoreId(); | ||
$visibility = $attrData[ProductInterface::VISIBILITY] ?? null; | ||
|
||
if (!$visibility) { | ||
return [$attrData, $productIds, $storeId]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The interface method ( |
||
} | ||
|
||
$productCollection = $this->productCollectionFactory->create(); | ||
$productCollection->addAttributeToSelect(ProductInterface::VISIBILITY); | ||
$productCollection->addAttributeToSelect('url_key'); | ||
$productCollection->addFieldToFilter( | ||
'entity_id', | ||
['in' => array_unique($productIds)] | ||
); | ||
|
||
foreach ($productCollection 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); | ||
$this->urlPersist->replace($productUrlRewrite); | ||
} | ||
} | ||
|
||
return [$attrData, $productIds, $storeId]; | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@VitaliyBoyko I don't really get this logic. URL Rewrites for invisible products should still be created so that they can be made visible at any moment without conflict.
Please update description with current/changed behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @orlangur
I've updated the PR description. Hope it is more comprehensive now. You may also check bugs
#10725
#20434 for additional information.
Thank you!