category wise sitemap generate in magento2

Generate category wise sitemap in Magento Admin allows you to apply an impressive document file that contains all of the web site’s URL and possibly direct people to any page on Magento stores quickly. Moreover, for the visitors, the sitemap is the same as a traditional geographical map during the adventure encompassing the real world because the Magento sitemap will help them visit pages easily they are looking for instead of losing or wasting much time going around a website.
In addition to the convenience of direction, Magento 2 sitemap is also an easy way to point what pages are ready for crawling to any search engines, which is very useful for your more salutary SEO. Thus, configuring the sitemap in your store is very significant.

     Magento 2 Sitemap: 4 steps to set-up your category wise sitemap :

  • Step 1: Use the SitemapGenerator.php file. 
  • Step 2: Created a sitemap.php a custom script.
  • Step 3: Made folder as sitemaps in the root directory.
  • Step 4: Run the script file by siteurl/sitemap.php.

(1) Use SitemapGenerator.php file:

  • First of all create SitemapGenerator.php file, which you can download ‘SitemapGenerator.php’.
  • Thereafter place that files in the root directory.

(2) Created a sitemap.php a custom script:

  • Next, You have to create a file name as “sitemap.php” a custom script into Root Directory.
  • After Place below file sitemap.php in the root directory.
<?php
set_time_limit(18000);
use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';
require_once 'SitemapGenerator.php';
$params = $_SERVER; 
$bootstrap = Bootstrap::create(BP, $params); 
$obj = $bootstrap->getObjectManager(); 
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$productCollection = $obj->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$categoryCollection = $obj->get('\Magento\Catalog\Model\CategoryFactory');
$sitemap_path = getcwd() .'/sitemaps/';
// Get First Level of Childerns
$categoryIds = Array('3');
foreach ($categoryIds as $categoryId) {

    $_category = $categoryCollection->create();
    $category = $_category->load($categoryId);
    $categories = $category->getCollection()
            ->addIsActiveFilter()
            ->addOrderField('name')
            ->addIdFilter($category->getChildren());
    foreach ($categories as $category) {

        $name = $category->getName();
        $name =  preg_replace('/[^a-zA-Z0-9\s]/', '', strtolower($name));
        $file = $sitemap_path.$name.'.xml';
        if(file_exists($file))
        {
            @unlink($file);
        }
        $categoryProducts = $productCollection->create();
        $categoryProducts->addAttributeToSelect('*');
        $categoryProducts->addCategoriesFilter(['in' => [$category->getId()] ]);
        $categoryProducts->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
        $categoryProducts->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
        if(count($categoryProducts))
        {
            $generator = new \Icamys\SitemapGenerator\SitemapGenerator('');
            $generator->createGZipFile = false;
            $generator->maxURLsPerSitemap = 50000;
            $generator->sitemapFileName = $file;
            foreach ($categoryProducts as $product) {
               $generator->addUrl($product->getProductUrl(), new DateTime(), 'weekly', '0.5');
            }

            $generator->createSitemap();
            $generator->writeSitemap();
        }
    } 
}
?>

 

 

  • Once you create sitemap.php file into root directory which will generate a sitemap for the first level of child categories.

(3) Made folder as sitemaps in the root directory:

  • Made one folder name “sitemaps” in the root directory. which will be empty not consist of any single file.

(4) Run the script file by siteurl/sitemap.php :

  • Run the script file by siteurl/sitemap.php after run script user will get category-specific xml files into the empty sitemaps folder.

 

Leave a Reply