Skip to content

Commit

Permalink
Merge pull request #12 from Magenerds/fix-breadcrumb
Browse files Browse the repository at this point in the history
Fix breadcrumb and Default config
  • Loading branch information
Mardl authored Jun 29, 2020
2 parents e5e16da + e1d83fb commit 03082f3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 34 deletions.
55 changes: 28 additions & 27 deletions Block/SchemaOrg.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ protected function getCategory()
protected function getCategoryRating()
{
// set default return value
$defaultReturn = [0, 0];
$defaultReturn = false;

// get products
if (!($productIds = $this->getCategory()->getProductCollection()->getAllIds())) {
Expand Down Expand Up @@ -387,6 +387,10 @@ public function getOrganizationSchema()
*/
public function getSchema()
{
// add ratings
if (!$this->helper->getSchemaEnable()) {
return '';
}
if ($this->getPage() === 'catalog_category_view') {
return $this->getCategorySchema();
} else {
Expand All @@ -402,6 +406,9 @@ public function getSchema()
*/
public function getSchemaBreadcrumb()
{
if (!$this->helper->getSchemaEnableBreadcrumb()) {
return '';
}
if ($this->getPage() === 'catalog_category_view') {
return $this->getCategorySchemaBreadcrumb(false);
} else {
Expand Down Expand Up @@ -456,30 +463,27 @@ protected function getCategorySchemaBreadcrumb($productPage)
'itemListElement' => [],
];

// add ratings
if ($this->helper->getSchemaEnableCategoryRatings()) {
$category = $this->getCategoryForBreadcrumb($productPage);
if (!$category) {
return [];
}
$crumbs = $this->getBreadcrumbPath($category);
if (isset($crumbs['category' . $this->getCategory()->getId()])) {
$crumbs['category' . $this->getCategory()->getId()]['link'] = $this->getFinalUrlFromCategory($this->getCategory());
}
if (isset($crumbs['product'])) {
unset($crumbs['product']);
}
$schemaCrumb = [];
foreach ($crumbs as $crumb) {
$schemaCrumb[] = [
'@type' => 'ListItem',
'position' => (count($schemaCrumb) + 1),
'name' => $crumb['label'],
'item' => $crumb['link'],
];
}
$schema['itemListElement'] = $schemaCrumb;
$category = $this->getCategoryForBreadcrumb($productPage);
if (!$category) {
return [];
}
$crumbs = $this->getBreadcrumbPath($category);
if (isset($crumbs['category' . $this->getCategory()->getId()])) {
$crumbs['category' . $this->getCategory()->getId()]['link'] = $this->getFinalUrlFromCategory($this->getCategory());
}
if (isset($crumbs['product'])) {
unset($crumbs['product']);
}
$schemaCrumb = [];
foreach ($crumbs as $crumb) {
$schemaCrumb[] = [
'@type' => 'ListItem',
'position' => (count($schemaCrumb) + 1),
'name' => $crumb['label'],
'item' => $crumb['link'],
];
}
$schema['itemListElement'] = $schemaCrumb;

// return schema
return $schema;
Expand Down Expand Up @@ -642,9 +646,6 @@ protected function getAttribute($code)
*/
protected function _toHtml()
{
if (!$this->helper->getSchemaEnable()) {
return '';
}
if (!$this->getTemplate()) {
$this->setTemplate('Magenerds_RichSnippet::head/schema.phtml');
}
Expand Down
20 changes: 15 additions & 5 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ public function getSchemaEnableCategoryRatings($storeId = 0)
return (bool)$this->getConfig($storeId, 'enable_category_ratings', 'schema');
}

/**
* Check if schema aggregated ratings are enabled
*
* @param int $storeId
* @return bool
*/
public function getSchemaEnableBreadcrumb($storeId = 0)
{
return (bool)$this->getConfig($storeId, 'enable_breadcrumb', 'schema');
}

/**
* Returns system configuration
*
Expand All @@ -145,10 +156,9 @@ public function getSchemaEnableCategoryRatings($storeId = 0)
*/
protected function getConfig($storeId, $name, $second = 'product_properties')
{
if ($storeId) {
return $this->scopeConfig->getValue('richsnippet/' . $second . '/' . $name, ScopeInterface::SCOPE_STORE, $storeId);
} else {
return $this->scopeConfig->getValue('richsnippet/' . $second . '/' . $name);
}
return $this->scopeConfig->getValue(
'richsnippet/' . $second . '/' . $name, ScopeInterface::SCOPE_STORE,
!empty($storeId) ? $storeId : null
);
}
}
6 changes: 5 additions & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,17 @@
<group id="schema" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Schema.org</label>
<field id="enable" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Enable</label>
<label>Enable Catalog/Product schema</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="enable_category_ratings" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Enable Aggregated Category Ratings</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="enable_breadcrumb" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Enable Catalog/Product breadcrumb</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
</section>
</system>
Expand Down
3 changes: 2 additions & 1 deletion etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</general>
<product_properties>
<color>color</color>
<description>description</description>
<description>1</description>
<sku>sku</sku>
<product_id>sku</product_id>
<brand>manufacturer</brand>
Expand All @@ -40,6 +40,7 @@
<schema>
<enable>1</enable>
<enable_category_ratings>1</enable_category_ratings>
<enable_breadcrumb>1</enable_breadcrumb>
</schema>
</richsnippet>
</default>
Expand Down

0 comments on commit 03082f3

Please sign in to comment.