1

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

4
  • 1
    why you need to add it pragmatically as there is an options in admin available to set CMS page as a home page. Aug 8 '18 at 5:17
  • please review this magento.stackexchange.com/questions/169372/…
    – Shorabh
    Aug 8 '18 at 5:49
  • @ShorabhKumarGupta,please check the question before commenting any reference url
    – Amit Bera
    Aug 8 '18 at 5: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. Aug 8 '18 at 5:56
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);
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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.