Hello Magento Friends,
In today’s blog, we will learn about How to Update CMS Static Page Programmatically in Magento 2.
CMS pages help to distribute your content and increase the visibility of products. CMS pages allow promoting your products in various ways to shoppers. You can Create CMS Static Page Programmatically in Magento 2.
Once you have created the CMS static page, you can update the CMS static page easily. This can be done programmatically in Magento 2. Let’s find out more
Step 1: Create a file in your Magento root directory at the below path
magento_root_directory\update_call.php
Now add the code as follows
<?php use Magento\Framework\App\Bootstrap; require './app/bootstrap.php'; $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $resource = $objectManager->get('Magento\Framework\App\ResourceConnection'); $connection = $resource->getConnection(); $state = $objectManager->get('Magento\Framework\App\State'); $state->setAreaCode('frontend'); try { $pageFactory = $objectManager->create('Magento\Cms\Model\PageFactory'); $pageedit = $pageFactory->create()->load( 'YOUR_CMS_PAGE_IDENTIFIER', 'identifier' ); $content = 'Test Content Set'; $title = 'Home Page Title'; $pagelayout = '2columns-left'; if ($pageedit->getId()) { $pageedit->setTitle($title); $pageedit->setPageLayout($pagelayout); $pageedit->setIsActive(1); $pageedit->setContent($content); $pageedit->setSortOrder(22); $pageedit->save(); } echo "CMS page updated successfully"; }catch (\Exception $e){ echo "****** Exception Throw ************* \n"; echo $e->getMessage() . " \n"; }
Output:
You can see the updated content on the CMS static page
Therefore, you can easily Update CMS Static Page Programmatically in Magento 2. You can also Translate CMS Pages in Magento 2. If you face any errors while updating the CMS static page using the above method, share them with me without any hesitation. Stay with us to learn more about Magento 2.
Happy Coding!
In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…
October was an exciting month for MageComp! From significant updates across our Magento 2 extension…
In modern web development, seamless navigation and state management are crucial for delivering a smooth…
Magento Open Source 2.4.8 beta version released on October 8, 2024. The latest release of…
Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…
As the world of eCommerce continues to thrive, Shopify has become one of the most…