Skip to content
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

fix virtual category url rewrite #565

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 122 additions & 26 deletions Job/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
use Akeneo\Pim\ApiClient\Pagination\PageInterface;
use Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface;
use Magento\Catalog\Model\Category as CategoryModel;
use Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider;
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
use Magento\CatalogUrlRewrite\Model\UrlRewriteBunchReplacer;
use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Select;
Expand Down Expand Up @@ -111,26 +113,47 @@ class Category extends Import
* @var IndexerFactory $indexFactory
*/
protected $indexFactory;
/**
* This variable contains a ChildrenCategoriesProvider
*
* @var ChildrenCategoriesProvider $childrenCategoriesProvider
*/
protected $childrenCategoriesProvider;
/**
* This variable contains a CategoryUrlRewriteGenerator
*
* @var CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator
*/
protected $categoryUrlRewriteGenerator;
/**
* This variable contains a UrlRewriteBunchReplacer
*
* @var UrlRewriteBunchReplacer $urlRewriteBunchReplacer
*/
protected UrlRewriteBunchReplacer $urlRewriteBunchReplacer;

/**
* Category constructor
*
* @param CategoryLogger $logger
* @param CategoryHandler $handler
* @param OutputHelper $outputHelper
* @param ManagerInterface $eventManager
* @param Authenticator $authenticator
* @param TypeListInterface $cacheTypeList
* @param Entities $entitiesHelper
* @param StoreHelper $storeHelper
* @param ConfigHelper $configHelper
* @param CategoryModel $categoryModel
* @param CategoryUrlPathGenerator $categoryUrlPathGenerator
* @param CategoryFilters $categoryFilters
* @param Edition $editionSource
* @param Entities $entities
* @param IndexerFactory $indexFactory
* @param array $data
* @param CategoryLogger $logger
* @param CategoryHandler $handler
* @param OutputHelper $outputHelper
* @param ManagerInterface $eventManager
* @param Authenticator $authenticator
* @param TypeListInterface $cacheTypeList
* @param Entities $entitiesHelper
* @param StoreHelper $storeHelper
* @param ConfigHelper $configHelper
* @param CategoryModel $categoryModel
* @param CategoryUrlPathGenerator $categoryUrlPathGenerator
* @param CategoryFilters $categoryFilters
* @param Edition $editionSource
* @param Entities $entities
* @param IndexerFactory $indexFactory
* @param ChildrenCategoriesProvider $childrenCategoriesProvider
* @param CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator
* @param UrlRewriteBunchReplacer $urlRewriteBunchReplacer
* @param array $data
*/
public function __construct(
CategoryLogger $logger,
Expand All @@ -148,20 +171,26 @@ public function __construct(
Edition $editionSource,
Entities $entities,
IndexerFactory $indexFactory,
ChildrenCategoriesProvider $childrenCategoriesProvider,
CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator,
UrlRewriteBunchReplacer $urlRewriteBunchReplacer,
array $data = []
) {
parent::__construct($outputHelper, $eventManager, $authenticator, $entitiesHelper, $configHelper, $data);

$this->storeHelper = $storeHelper;
$this->logger = $logger;
$this->handler = $handler;
$this->cacheTypeList = $cacheTypeList;
$this->categoryModel = $categoryModel;
$this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
$this->categoryFilters = $categoryFilters;
$this->editionSource = $editionSource;
$this->entities = $entities;
$this->indexFactory = $indexFactory;
$this->storeHelper = $storeHelper;
$this->logger = $logger;
$this->handler = $handler;
$this->cacheTypeList = $cacheTypeList;
$this->categoryModel = $categoryModel;
$this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
$this->categoryFilters = $categoryFilters;
$this->editionSource = $editionSource;
$this->entities = $entities;
$this->indexFactory = $indexFactory;
$this->childrenCategoriesProvider = $childrenCategoriesProvider;
$this->categoryUrlRewriteGenerator = $categoryUrlRewriteGenerator;
$this->urlRewriteBunchReplacer = $urlRewriteBunchReplacer;
}

/**
Expand Down Expand Up @@ -819,6 +848,7 @@ public function setUrlRewrite()
'store_id' => new Expr($store['store_id']),
'parent_id' => 'parent_id',
'level' => 'level',
'name' => 'labels-' . $local,
]
);

Expand Down Expand Up @@ -886,11 +916,19 @@ public function setUrlRewrite()

if ($rewriteId) {
try {
$oldUrlRewrite = $this->getOldUrlRewrite($rewriteId);

$connection->update(
$this->entitiesHelper->getTable('url_rewrite'),
['request_path' => $requestPath],
['url_rewrite_id = ?' => $rewriteId]
);

if ($oldUrlRewrite['request_path'] === $requestPath) {
continue;
}

$this->updateUrlRewrite($category);
} catch (\Exception $e) {
$this->jobExecutor->setAdditionalMessage(
__(
Expand Down Expand Up @@ -1084,4 +1122,62 @@ public function getLogger()
{
return $this->logger;
}

/**
* Get old reference from url rewrite
*
* @param $rewriteId
* @return array|mixed
*/
public function getOldUrlRewrite($rewriteId)
{
/** @var AdapterInterface $connection */
$connection = $this->entitiesHelper->getConnection();
/** @var string $tableName */
$select = $connection->select()->from(
$this->entitiesHelper->getTable('url_rewrite'),
[
'request_path' => 'request_path',
'entity_id' => 'entity_id',
]
)->where('url_rewrite_id = ?', $rewriteId);

return $connection->fetchRow($select);
}

/**
* Update Url Path for Category
*
* @param CategoryModel $category
* @param CategoryModel|null $parentCategory
* @return void
*/
private function updateUrlPathForCategory(CategoryModel $category, CategoryModel $parentCategory = null): void
{
$category->unsUrlPath();
$category->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category, $parentCategory));
$category->getResource()->saveAttribute($category, 'url_path');
}

/**
* Updates all url rewrites under the current category
*
* @param CategoryModel $category
* @return void
*/
private function updateUrlRewrite(CategoryModel $category): void
{
$category->setPath($category->getUrlPath());
$children = $this->childrenCategoriesProvider->getChildren($category, true);
foreach ($children as $child) {
$child->setStoreId($category->getStoreId());
if ($child->getParentId() === $category->getId()) {
$this->updateUrlPathForCategory($child, $category);
} else {
$this->updateUrlPathForCategory($child);
}
}
$categoryUrlRewriteResult = $this->categoryUrlRewriteGenerator->generate($category);
$this->urlRewriteBunchReplacer->doBunchReplace($categoryUrlRewriteResult);
}
}