{"id":306648,"date":"2021-09-22T19:18:23","date_gmt":"2021-09-22T19:18:23","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=306648"},"modified":"2024-06-17T13:37:17","modified_gmt":"2024-06-17T13:37:17","slug":"reindexing-for-one-product-in-magento-2","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/","title":{"rendered":"Reindexing for one product in Magento 2"},"content":{"rendered":"\n<p>Hello Friends!<br>In this blog, we will learn how we can do reindexing for one product programmatically.<br><\/p>\n\n\n\n<p>In Magento 2, when we create a new product or update an existing product, sometimes its inventory and prices don&#8217;t update on frontend. And we don&#8217;t want to execute the reindexing command from CLI after each change.<\/p>\n\n\n\n<p>In that case, we can create an observer on &#8216;<strong>controller_action_catalog_product_save_entity_after<\/strong>&#8216; event, which is dispatched in <strong>\/Magento\/Catalog\/Controller\/Adminhtml\/Product\/Save <\/strong>Controller class in Magento 2.<\/p>\n\n\n\n<p>And if we are creating or updating a product programmatically then we can add the following code statements in our code.<\/p>\n\n\n\n<p>Here, I have done this in AdminProductSaveAfterObserver.php file inside app\/code\/Vendor\/CustomModule\/Observer\/ directory.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\/**\n * Vendor Desc.\n * php version 7.3.*\n *\n * @category  Vendor\n * @package   Vendor_CustomModule\n * @author    Vendor\n * @copyright Copyright (c) Vendor\n * @license   https:\/\/eaxmple.com\/license.html\n *\/\nnamespace Vendor\\CustomModule\\Observer;\n\nuse Magento\\Framework\\Event\\ObserverInterface;\n\nclass AdminProductSaveAfterObserver implements ObserverInterface\n{\n    \/**\n     * @var \\Magento\\Framework\\Indexer\\IndexerRegistry\n     *\/\n    protected $indexerRegistry;\n\n    \/**\n     * Object intialization\n     *\n     * @param \\Magento\\Framework\\Indexer\\IndexerRegistry $indexerRegistry\n     *\/\n    public function __construct(\n        \\Magento\\Framework\\Indexer\\IndexerRegistry $indexerRegistry\n    ) {\n        $this-&gt;indexerRegistry = $indexerRegistry;\n    }\n\n    \/**\n     * Product save after observer\n     *\n     * @param \\Magento\\Framework\\Event\\Observer $observer\n     *\/\n\n    public function execute(\\Magento\\Framework\\Event\\Observer $observer)\n    {\n        try {\n            $product = $observer-&gt;getProduct();\n            if (!empty($product)) {\n                $productIds = &#091;$product-&gt;getId()]; \/\/here you can one or more product Ids in array\n                \/\/list of indexers\n                $indexList = &#091;\n                    &#039;catalog_category_product&#039;,\n                    &#039;catalog_product_category&#039;,\n                    &#039;catalog_product_attribute&#039;,\n                    &#039;cataloginventory_stock&#039;,\n                    &#039;inventory&#039;,\n                    &#039;catalogsearch_fulltext&#039;,\n                    &#039;catalog_product_price&#039;,\n                    &#039;catalogrule_product&#039;,\n                    &#039;catalogrule_rule&#039;\n                ];\n \n                foreach ($indexList as $index) {\n                    $categoryIndexer = $this-&gt;indexerRegistry-&gt;get($index);\n                \n                    \/\/check is indexer is scheduled\n                    if (!$categoryIndexer-&gt;isScheduled()) {\n                        $categoryIndexer-&gt;reindexList($productIds);\n                    }\n                }\n            }\n        } catch (\\Exception $e) {\n            echo $e-&gt;getMessage();\n        }\n    }\n}<\/pre>\n\n\n\n<p>Hope this will be helpful. Thanks \ud83d\ude42<br><br><strong>Previous Blog:<\/strong> <a href=\"https:\/\/webkul.com\/blog\/display-dependent-fields-on-customer-account-edit-page\/\" target=\"_blank\" rel=\"noreferrer noopener\">Display dependent fields on Customer Account Edit Page<\/a><br><br><strong>Next Blog:<\/strong> <a href=\"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">Detect your device is Mobile or Desktop in Magento 2<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello Friends!In this blog, we will learn how we can do reindexing for one product programmatically. In Magento 2, when we create a new product or update an existing product, sometimes its inventory and prices don&#8217;t update on frontend. And we don&#8217;t want to execute the reindexing command from CLI after each change. In that <a href=\"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":249,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9121],"tags":[12091,12090,12092,3848],"class_list":["post-306648","post","type-post","status-publish","format-standard","hentry","category-magento-2","tag-indexer","tag-reindexing","tag-reindexing-for-one-product","tag-reindexing-programmatically-in-magento-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Reindexing for one product in Magento 2 - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Reindexing for one product in Magento 2 - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Hello Friends!In this blog, we will learn how we can do reindexing for one product programmatically. In Magento 2, when we create a new product or update an existing product, sometimes its inventory and prices don&#8217;t update on frontend. And we don&#8217;t want to execute the reindexing command from CLI after each change. In that [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-22T19:18:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-17T13:37:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Khushboo Sahu\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Khushboo Sahu\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/\"},\"author\":{\"name\":\"Khushboo Sahu\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca\"},\"headline\":\"Reindexing for one product in Magento 2\",\"datePublished\":\"2021-09-22T19:18:23+00:00\",\"dateModified\":\"2024-06-17T13:37:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/\"},\"wordCount\":154,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"indexer\",\"reindexing\",\"reindexing for one product\",\"reindexing programmatically in magento 2\"],\"articleSection\":[\"Magento 2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/\",\"url\":\"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/\",\"name\":\"Reindexing for one product in Magento 2 - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2021-09-22T19:18:23+00:00\",\"dateModified\":\"2024-06-17T13:37:17+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Reindexing for one product in Magento 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca\",\"name\":\"Khushboo Sahu\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"caption\":\"Khushboo Sahu\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/khushboo-sahu062\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Reindexing for one product in Magento 2 - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/","og_locale":"en_US","og_type":"article","og_title":"Reindexing for one product in Magento 2 - Webkul Blog","og_description":"Hello Friends!In this blog, we will learn how we can do reindexing for one product programmatically. In Magento 2, when we create a new product or update an existing product, sometimes its inventory and prices don&#8217;t update on frontend. And we don&#8217;t want to execute the reindexing command from CLI after each change. In that [...]","og_url":"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2021-09-22T19:18:23+00:00","article_modified_time":"2024-06-17T13:37:17+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Khushboo Sahu","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Khushboo Sahu","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/"},"author":{"name":"Khushboo Sahu","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca"},"headline":"Reindexing for one product in Magento 2","datePublished":"2021-09-22T19:18:23+00:00","dateModified":"2024-06-17T13:37:17+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/"},"wordCount":154,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["indexer","reindexing","reindexing for one product","reindexing programmatically in magento 2"],"articleSection":["Magento 2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/","url":"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/","name":"Reindexing for one product in Magento 2 - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2021-09-22T19:18:23+00:00","dateModified":"2024-06-17T13:37:17+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Reindexing for one product in Magento 2"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca","name":"Khushboo Sahu","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","caption":"Khushboo Sahu"},"url":"https:\/\/webkul.com\/blog\/author\/khushboo-sahu062\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/306648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/249"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=306648"}],"version-history":[{"count":4,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/306648\/revisions"}],"predecessor-version":[{"id":448347,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/306648\/revisions\/448347"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=306648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=306648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=306648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}