Skip to content

Commit

Permalink
Merge pull request #10 from Magenerds/fix-wrong-url-from-magento
Browse files Browse the repository at this point in the history
FIX: Get final url from category without redirect for breadxcrumb
  • Loading branch information
Mardl authored Jul 29, 2019
2 parents a142137 + e0f395f commit 4647b23
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions Block/SchemaOrg.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Magenerds\RichSnippet\Helper\Data;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Product;
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Data\Collection;
use Magento\Framework\DataObject;
Expand All @@ -28,6 +29,8 @@
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\Store;
use Magento\Theme\Block\Html\Header\Logo;
use Magento\UrlRewrite\Model\UrlFinderInterface;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;

/**
* Class SchemaOrg
Expand Down Expand Up @@ -92,6 +95,8 @@ class SchemaOrg extends Template // NOSONAR
* @var ResourceConnection
*/
protected $connection;
/** @var UrlFinderInterface */
private $urlFinder;

/**
* SchemaOrg constructor.
Expand All @@ -103,6 +108,7 @@ class SchemaOrg extends Template // NOSONAR
* @param Context $context
* @param EventManager $eventManager
* @param ResourceConnection $connection
* @param UrlFinderInterface $urlFinder
* @param array $data
*/
public function __construct(
Expand All @@ -113,6 +119,7 @@ public function __construct(
Context $context,
EventManager $eventManager,
ResourceConnection $connection,
UrlFinderInterface $urlFinder,
$data = []
) {
$this->coreRegistry = $registry;
Expand All @@ -122,6 +129,7 @@ public function __construct(
$this->eventManager = $eventManager;
$this->connection = $connection;
parent::__construct($context, $data);
$this->urlFinder = $urlFinder;
}

/**
Expand Down Expand Up @@ -453,7 +461,7 @@ protected function getCategorySchemaBreadcrumb($productPage)
$category = $this->getCategoryForBreadcrumb($productPage);
$crumbs = $this->getBreadcrumbPath($category);
if (isset($crumbs['category' . $this->getCategory()->getId()])) {
$crumbs['category' . $this->getCategory()->getId()]['link'] = $this->getCategory()->getUrl();
$crumbs['category' . $this->getCategory()->getId()]['link'] = $this->getFinalUrlFromCategory($this->getCategory());
}
if (isset($crumbs['product'])) {
unset($crumbs['product']);
Expand All @@ -474,6 +482,35 @@ protected function getCategorySchemaBreadcrumb($productPage)
return $schema;
}

/**
* Get category url
*
* @param Category $category
* @return string
*/
public function getFinalUrlFromCategory(\Magento\Catalog\Model\Category $category)
{
if ($category->hasData('request_path') && $category->getRequestPath() != '') {
$category->setData('url', $category->getUrlInstance()->getDirectUrl($category->getRequestPath()));
return $category->getData('url');
}

$rewrite = $this->urlFinder->findOneByData([
UrlRewrite::ENTITY_ID => $category->getId(),
UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE,
UrlRewrite::STORE_ID => $category->getStoreId(),
UrlRewrite::REDIRECT_TYPE => 0, // Final URL, no redirect. TD-CHANGE from origin
]);
if ($rewrite) {
$category->setData('url', $category->getUrlInstance()->getDirectUrl($rewrite->getRequestPath()));
return $category->getData('url');
}

$category->setData('url', $category->getCategoryIdUrl());
return $category->getData('url');
}


/**
* Get product schema
*
Expand Down Expand Up @@ -633,7 +670,7 @@ public function getBreadcrumbPath(Category $category)
if (isset($categories[$categoryId]) && $categories[$categoryId]->getName()) {
$path['category' . $categoryId] = [
'label' => $categories[$categoryId]->getName(),
'link' => $categories[$categoryId]->getUrl()
'link' => $this->getFinalUrlFromCategory($categories[$categoryId])
];
}
}
Expand Down

0 comments on commit 4647b23

Please sign in to comment.