Magento 2 Google Site Map Exclusion

Sitemaps are critical to search engine optimization. Search engines may judge your web store by your sitemaps. Sometimes you may want to exclude some of the links from your sitemaps for SEO purposes but still, want to keep them in the database. With the help of Magento 2 Google Site Map Exclusion, you can easily achieve this.

1. DESCRIPTION

Our Magento 2 Google Site Map Exclusion extension will provide you the ability to exclude a specific or certain category, product and static page links from the Google site map generated by Magento.

By using this extension you can dynamically exclude Products, Categories, and Pages that you do not want to be indexed by search engines but are still required in your store.

This extension helps you gain in-depth control over your Magento 2 sitemap by allowing you to control visibility in sitemap for each Product, Category and CMS page.

2. KEY FEATURES OF MAGENTO 2 GOOGLE SITE MAP EXCLUSION EXTENSION

  • It allows you to exclude specific or certain Category /Product/CMS page links from sitemap.xml of your site.
  • It allows you to exclude duplicate and unwanted URLs from the sitemap.xml
  • There is an attribute "Exclude from Sitemap" added to each of the category/Product/CMS pages which can be set YES/NO to include/exclude a certain category, product and static page links from Google site map.
  • It a great admin tool
  • It fully supports multistore
  • User-friendly interface
  • Easy to install and manage

Today I will show you how to use command shortcuts in Magento 2. It will help you save time when working.

3. MODULE SETUP

Step 1:

First, we need to create a new module. This requires namespace and module folders with registration.php and etc/module.xml inside the module folder. For this example, I am going to use Magepow for namespace and Sitemapexclusion for the module name. If you are unfamiliar with development preparations and the creation of custom modules in Magento 2.

Step 1.1:

To register the module we add file registration as follows

app/code/Magepow/Sitemapexclusion/registration.php
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/ \Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Magepow_Sitemapexclusion',
__DIR__
);

Step 1.2:

To create the module we need to add

app/code/Magepow/Sitemapexclusion/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magepow_Sitemapexclusion" setup_version="1.1.1">
<sequence>
<module name="Magento_Backend"/>
<module name="Magento_Sitemap"/>
</sequence>
</module>
</config>

Step 2:

Add data to add an attribute

Custom code

Step 2.1:

First, we will create a Database: We will use a simple database

app/code/Magepow/Sitemapexclusion/Setup/InstallSchema.php
<?php
namespace Magepow\Sitemapexclusion\Setup; use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface; /**
* Class InstallSchema
* @package Magepow\Sitemapexclusion\Setup
*/
class InstallSchema implements InstallSchemaInterface
{
/**
* @param SchemaSetupInterface $setup
* @param ModuleContextInterface $context
*/
public function install(
SchemaSetupInterface $setup,
ModuleContextInterface $context
) {
$setup->startSetup(); $setup->getConnection()->addColumn($setup->getTable('cms_page'), 'sitemap_exclude', [
'type' => Table::TYPE_INTEGER,
'nullable' => true,
'comment' => 'Exclude sitemap',
]); $setup->endSetup();
}
}

Step 2.2:

Next, create a file InstallData.php to add attribute stiemap_exclusion

app/code/Magepow/Sitemapexclusion/Setup/InstallData.php
<?php 
namespace Magepow\Sitemapexclusion\Setup;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Setup\CategorySetupFactory;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface; /**
* Class InstallData
* @package Magepow\Sitemapexclusion\Setup
*/
class InstallData implements InstallDataInterface
{
/**
* @var CategorySetupFactory
*/
protected $categorySetupFactory; /**
* @var EavSetupFactory
*/
protected $eavSetupFactory; /**
* InstallData constructor.
*
* @param EavSetupFactory $eavSetupFactory
* @param CategorySetupFactory $categorySetupFactory
*/
public function __construct(
EavSetupFactory $eavSetupFactory,
CategorySetupFactory $categorySetupFactory
) {
$this->categorySetupFactory = $categorySetupFactory;
$this->eavSetupFactory = $eavSetupFactory;
} /**
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); /**
* Product attribute
*/
$eavSetup->removeAttribute(Product::ENTITY, 'sitemap_exclude');
$eavSetup->addAttribute(Product::ENTITY, 'sitemap_exclude', [
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Exclude From Sitemap',
'note' => 'Added by Magepow Sitemap',
'input' => 'boolean',
'class' => '',
'source' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class,
'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'unique' => false,
'group' => 'Product Details',
'sort_order' => 100,
'apply_to' => '',
]); /**
* Category attribute
*/
$categorySetup = $this->categorySetupFactory->create(['setup' => $setup]); $categorySetup->removeAttribute(Category::ENTITY, 'sitemap_exclude');
$categorySetup->addAttribute(Category::ENTITY, 'sitemap_exclude',
[
'type' => 'int',
'label' => 'Exclude From Sitemap',
'input' => 'boolean',
'sort_order' => 100,
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
'global' => ScopedAttributeInterface::SCOPE_STORE,
'visible' => true,
'required' => true,
'user_defined' => false,
'default' => null,
'group' => 'General Information',
'backend' => ''
]); $setup->endSetup();
}
}

Step 3: Create a file di.xml

Create a file di.xml to override the file needs edit.

app/code/Magepow/Sitemapexclude/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Sitemap\Model\ItemProvider\Category" type="Magepow\Sitemapexclusion\Model\ItemProvider\Category"/>
<preference for="Magento\Sitemap\Model\ItemProvider\Product" type="Magepow\Sitemapexclusion\Model\ItemProvider\Product"/>
<preference for="Magento\Sitemap\Model\ItemProvider\CmsPage" type="Magepow\Sitemapexclusion\Model\ItemProvider\CmsPage"/>
</config>

Step 4: Create a Model

Next is the content we need to override, in order to remove the content we need to delete we create the necessary files.

To override the Magento \ Sitemap \ Model \ ItemProvider \ Product file we create the following file.

Step 4.1: Product.php

Magepow\Sitemapexclusion\Model\ItemProvider\Product

With content

<?php
namespace Magepow\Sitemapexclusion\Model\ItemProvider;> use Magento\Sitemap\Model\ResourceModel\Catalog\ProductFactory;
use Magento\Sitemap\Model\SitemapItemInterfaceFactory; class Product extends \Magento\Sitemap\Model\ItemProvider\Product
{

/**
* {@inheritdoc}
*/
public function getItems($storeId)
{
$items = parent::getItems($storeId);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
//remove product from sitmap
$productId = $productCollection->addAttributeToSelect('sitemap_exclude')
->addAttributeToFilter('sitemap_exclude', '1');
foreach ($productId as $product){

unset($items[$product->getId()]);
}

return $items;
}
}

To override the Magento \ Sitemap \ Model \ ItemProvider \ Category file we create the following file.

Step 4.2: Category.php

Magepow\Sitemapexclusion\Model\ItemProvider\Category

With content

<?php
namespace Magepow\Sitemapexclusion\Model\ItemProvider; use Magento\Sitemap\Model\ResourceModel\Catalog\CategoryFactory;
use Magento\Sitemap\Model\SitemapItemInterfaceFactory; class Category extends \Magento\Sitemap\Model\ItemProvider\Category
{
/**
* {@inheritdoc}
*/
public function getItems($storeId)
{
$items = parent::getItems($storeId);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$CategoryFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$categories = $CategoryFactory->create()->addAttributeToSelect('sitemap_exclude')
->addAttributeToFilter('sitemap_exclude', '1');

foreach ($categories as $category){
var_dump($category->getId());
unset($items[$category->getId()]);
}
return $items;
}
}

To override the Magento \ Sitemap \ Model \ ItemProvider \ CmsPage file we create the following file.

Step 4.3: CmsPage.php

Magepow\Sitemapexclusion\Model\ItemProvider\CmsPage

With content

<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/ namespace Magepow\Sitemapexclusion\Model\ItemProvider; use Magento\Sitemap\Model\ResourceModel\Cms\PageFactory;
use Magento\Sitemap\Model\SitemapItemInterfaceFactory; class CmsPage extends \Magento\Sitemap\Model\ItemProvider\CmsPage
{
/**
* {@inheritdoc}
*/
public function getItems($storeId)
{
$items = parent::getItems($storeId);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$collection = $objectManager->get('\Magento\Cms\Model\ResourceModel\Page\CollectionFactory');
// add Filter if you want
$cmsPage = $collection->create()->addFieldToFilter('sitemap_exclude','1');
foreach ($cmsPage as $page){
unset($items[$page->getId()]);
}
return $items;
}
}

Step 5: Create view attributes

Next, we build ui_component in the admin HTML view to show custom buttons that toggle the category and cms page properties.

I create more files category_form.xml and cms_page_form.xml

Step 5.1: Create category_form.xml

Magepow\Sitemapexclusion\view\adminhtml\ui_component\category_form.xml

 

<?xml version="1.0" ?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="general">
<field name="sitemap_exclude" sortOrder="100" formElement="checkbox">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="sortOrder" xsi:type="number">100</item>
<item name="dataType" xsi:type="string">boolean</item>
<item name="formElement" xsi:type="string">checkbox</item>
<item name="label" xsi:type="string" translate="true">Sitemap From Exclude</item>
<item name="prefer" xsi:type="string">toggle</item>
<item name="valueMap" xsi:type="array">
<item name="true" xsi:type="string">1</item>
<item name="false" xsi:type="string">0</item>
</item>
<item name="default" xsi:type="number">0</item>
</item>
</argument>
</field>
</fieldset>
</form>

Step 5.2: Create category_form.xml

Magepow\Sitemapexclusion\view\adminhtml\ui_component\cms_page_form.xml

 

<?xml version="1.0" ?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="general">
<field name="sitemap_exclude" sortOrder="10" formElement="checkbox">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">page</item>
<item name="default" xsi:type="number">1</item>
</item>
</argument>
<settings>
<dataType>boolean</dataType>
<label translate="true">Sitemap From Exclude</label>
<dataScope>sitemap_exclude</dataScope>
</settings>
<formElements>
<checkbox>
<settings>
<valueMap>
<map name="false" xsi:type="number">0</map>
<map name="true" xsi:type="number">1</map>
</valueMap>
<prefer>toggle</prefer>
</settings>
</checkbox>
</formElements>
</field>
</fieldset>
</form>

 

Source available on Github

Run the “setup:upgrade” command

Running this command makes your new module active, notifying Magento of its presence.

php bin/magento setup:upgrade

Next to running this command deploy:

php bin/magento setup:static-content:deploy -f

Next to running this command delete cache:

php bin/magento cache:flsuh

4. CHECK THE RESULT 

Check Product Details

Check Categories

Check Cms Page

When enabled sitemap exclusion property URLs, category or cms_page will be hidden until we turn it off.

Take for example the photo below.

Hope this article will help you in some way, You can see useful articles in the next articles.

Anything you need support from Magento 2 feels free to contact us at Alothemes and

Phone: (+84)865633728

Email: support@alothemes.com

Skype: vuhoi292