Skip to content

Commit

Permalink
Update: change system config canonical field
Browse files Browse the repository at this point in the history
  • Loading branch information
phutx committed Mar 19, 2024
1 parent a3ae9b9 commit 3cef0d8
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 21 deletions.
24 changes: 24 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 226,28 @@ public function getQtySale($product)
return 0;
}
}

/**
* @param $storeId
* @return bool
*/
public function canUseCanonicalForCategory($storeId)
{
$value = $this->getDuplicateConfig('canonical_tag', $storeId);
$value = $value ? explode(',', $value) : 0;

return in_array(1, is_array($value) ? $value : [$value]);
}

/**
* @param $storeId
* @return bool
*/
public function canUseCanonicalForProduct($storeId)
{
$value = $this->getDuplicateConfig('canonical_tag', $storeId);
$value = $value ? explode(',', $value) : 0;

return in_array(2, is_array($value) ? $value : [$value]);
}
}
43 changes: 43 additions & 0 deletions Model/Config/Source/CanonicalFor.php
Original file line number Diff line number Diff line change
@@ -0,0 1,43 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Seo
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/

namespace Mageplaza\Seo\Model\Config\Source;

use Magento\Framework\Option\ArrayInterface;

/**
* Class CanonicalFor
* @package Mageplaza\Seo\Model\Config\Source
*/
class CanonicalFor implements ArrayInterface
{
/**
* @return array
*/
public function toOptionArray()
{
return [
['label' => __('--Please select--'), 'value' => '0'],
['label' => __('Category'), 'value' => '1'],
['label' => __('Product'), 'value' => '2']
];
}
}
20 changes: 16 additions & 4 deletions Plugin/Helper/CanUseCanonicalTagForCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 21,7 @@

namespace Mageplaza\Seo\Plugin\Helper;

use Magento\Store\Model\StoreManagerInterface;
use Magento\Catalog\Helper\Category;
use Mageplaza\Seo\Helper\Data as HelperData;

Expand All @@ -30,6 31,13 @@
*/
class CanUseCanonicalTagForCategories
{
/**
* Store manager
*
* @var StoreManagerInterface
*/
protected $_storeManager;

/**
* @var HelperData
*/
Expand All @@ -38,11 46,15 @@ class CanUseCanonicalTagForCategories
/**
* CanUseCanonicalTagForCategories constructor.
*
* @param StoreManagerInterface $storeManager
* @param HelperData $helper
*/
public function __construct(HelperData $helper)
{
$this->_helper = $helper;
public function __construct(
StoreManagerInterface $storeManager,
HelperData $helper
) {
$this->_storeManager = $storeManager;
$this->_helper = $helper;
}

/**
Expand All @@ -54,7 66,7 @@ public function __construct(HelperData $helper)
public function afterCanUseCanonicalTag(Category $category, $result)
{
if ($this->_helper->isEnabled()) {
return $this->_helper->getDuplicateConfig('category_canonical_tag');
return $this->_helper->canUseCanonicalForCategory($this->_storeManager->getStore()->getId());
}

return $result;
Expand Down
22 changes: 17 additions & 5 deletions Plugin/Helper/CanUseCanonicalTagForProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 22,7 @@
namespace Mageplaza\Seo\Plugin\Helper;

use Magento\Catalog\Helper\Product;
use Magento\Store\Model\StoreManagerInterface;
use Mageplaza\Seo\Helper\Data as HelperData;

/**
Expand All @@ -30,19 31,30 @@
*/
class CanUseCanonicalTagForProducts
{
/**
* Store manager
*
* @var StoreManagerInterface
*/
protected $_storeManager;

/**
* @var HelperData
*/
protected $_helper;

/**
* CanUseCanonicalTagForProducts constructor.
* CanUseCanonicalTagForProduct constructor.
*
* @param StoreManagerInterface $storeManager
* @param HelperData $helper
*/
public function __construct(HelperData $helper)
{
$this->_helper = $helper;
public function __construct(
StoreManagerInterface $storeManager,
HelperData $helper
) {
$this->_storeManager = $storeManager;
$this->_helper = $helper;
}

/**
Expand All @@ -54,7 66,7 @@ public function __construct(HelperData $helper)
public function afterCanUseCanonicalTag(Product $product, $result)
{
if ($this->_helper->isEnabled()) {
return $this->_helper->getDuplicateConfig('product_canonical_tag');
return $this->_helper->canUseCanonicalForProduct($this->_storeManager->getStore()->getId());
}

return $result;
Expand Down
16 changes: 6 additions & 10 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 42,18 @@
</ul>
]]></comment>
</field>
<field id="enabled" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="enabled" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Enable</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
<group id="duplicate" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Duplicate Content</label>
<field id="category_canonical_tag" translate="label comment" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Use Canonical Link Meta Tag For Categories</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Avoid duplicate content.</comment>
</field>
<field id="product_canonical_tag" translate="label comment" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Use Canonical Link Meta Tag For Products</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Avoid duplicate content.</comment>
<field id="canonical_tag" translate="label" type="multiselect" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Use Canonical Link Meta Tag For</label>
<source_model>Mageplaza\Seo\Model\Config\Source\CanonicalFor</source_model>
<comment>Using canonical URLs will avoid duplicating content between product and category pages,
avoiding low quality ratings and reduced rankings on search engines.</comment>
</field>
</group>
<group id="richsnippets" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
Expand Down
3 changes: 1 addition & 2 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 44,7 @@
</seo>
<catalog>
<seo>
<category_canonical_tag>1</category_canonical_tag>
<product_canonical_tag>1</product_canonical_tag>

</seo>
</catalog>
</default>
Expand Down

0 comments on commit 3cef0d8

Please sign in to comment.