Set Custom CMS page as a home page programatically in Magento 2
Question
How to set a custom CMS page as my Magento home page programmatically.
Solution
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);
OTHER TIPS
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);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange