Skip to content

feat: Export category URL #101

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

Open
wants to merge 4 commits into
base: beta
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
41 changes: 40 additions & 1 deletion Model/Write/Categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

namespace Tweakwise\Magento2TweakwiseExport\Model\Write;

use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
use Magento\Framework\UrlInterface;
use Magento\UrlRewrite\Model\UrlFinderInterface;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use Tweakwise\Magento2TweakwiseExport\Model\Config;
use Tweakwise\Magento2TweakwiseExport\Model\Helper;
use Tweakwise\Magento2TweakwiseExport\Model\Logger;
Expand Down Expand Up @@ -53,13 +57,17 @@ class Categories implements WriterInterface
* @param Config $config
* @param Helper $helper
* @param Logger $log
* @param UrlFinderInterface $urlFinder
* @param UrlInterface $url
*/
public function __construct(
Iterator $iterator,
StoreManager $storeManager,
Config $config,
Helper $helper,
Logger $log
Logger $log,
private readonly UrlFinderInterface $urlFinder,
private readonly UrlInterface $url
) {
$this->iterator = $iterator;
$this->storeManager = $storeManager;
Expand Down Expand Up @@ -153,6 +161,11 @@ public function exportStore(Writer $writer, XMLWriter $xml, Store $store, array
continue;
}

$categoryUrl = $this->getCategoryUrl((int)$data['entity_id'], $store);
if ($categoryUrl) {
$data['url'] = $categoryUrl;
}

// Set category as exported
$exportedCategories[$data['entity_id']] = true;
$this->writeCategory($xml, $storeId, $data);
Expand Down Expand Up @@ -181,6 +194,10 @@ protected function writeCategory(XMLWriter $xml, int $storeId, array $data): voi
$xml->writeElement('rank', $data['position']);
$xml->writeElement('name', $data['name']);

if (isset($data['url'])) {
$xml->writeElement('url', $data['url']);
}

if (isset($data['parent_id']) && $data['parent_id']) {
$xml->startElement('parents');

Expand All @@ -199,4 +216,26 @@ protected function writeCategory(XMLWriter $xml, int $storeId, array $data): voi

$xml->endElement(); // </category>
}

/**
* @param int $categoryId
* @param Store $store
* @return string|null
*/
private function getCategoryUrl(int $categoryId, Store $store): ?string
{
$rewrite = $this->urlFinder->findOneByData(
[
UrlRewrite::ENTITY_ID => $categoryId,
UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE,
UrlRewrite::STORE_ID => $store->getId(),
UrlRewrite::REDIRECT_TYPE => 0
]
);
if ($rewrite) {
return $this->url->getDirectUrl($rewrite->getRequestPath());
}

return null;
}
}