1

How to set a custom CMS page as my Magento home page programmatically.

Aniket Prajapati
  • 342
  • 3
  • 17
  • 1
    why you need to add it pragmatically as there is an options in admin available to set CMS page as a home page. – Aasim Goriya Aug 08 '18 at 05:17
  • please review this https://magento.stackexchange.com/questions/169372/creating-a-custom-homepage-template-in-magento2-1-5 – Shorabh Aug 08 '18 at 05:49
  • @ShorabhKumarGupta,please check the question before commenting any reference url – Amit Bera Aug 08 '18 at 05:54
  • @AasimGoriya I am working on custom module, where i need to upgrade my default page to my custom page. I know i can set it from admin. But i want to do it by programmatically in my custom module. – Aniket Prajapati Aug 08 '18 at 05:56

2 Answers2

3

You can set default value with following code.

 public function __construct(\Magento\Config\Model\ResourceModel\Config $_resourceConfig)
    {
        $this->_resourceConfig = $_resourceConfig;
    }

        $this->_resourceConfig->saveConfig('web/default/cms_home_page', 'youridentifier', 'default', 0);
PROGOSTECH
  • 754
  • 4
  • 10
0

By using Magento\Framework\App\Config\Storage\WriterInterface , you can set a store config value

use Magento\Framework\App\Config\ScopeConfigInterface;

    /**
     *  @var \Magento\Framework\App\Config\Storage\WriterInterface
     */
    protected $configWriter;

    /**
     *
     * @param \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
     */
    public function __construct(
        ....
        \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
        .....
    )
    {
        $this->configWriter = $configWriter;
    }

    $this->configWriter->save('web/default/cms_home_page',  $CMSPAGEIdentifier, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0);
Qaisar Satti
  • 32,469
  • 18
  • 85
  • 137
Amit Bera
  • 77,456
  • 20
  • 123
  • 237