How To

How to Update CMS Static Page Programmatically in Magento 2

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

Steps to Update CMS Static Page Programmatically in Magento 2:

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

Conclusion:

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!

Click to rate this post!
[Total: 3 Average: 4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

1 day ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

4 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

7 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

7 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

1 week ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

1 week ago