Magento 2: How to Create Cms Block Programmatically Using DataPatch

Introduction In the [How to Create Cms Page Programmatically Using DataPatch] chapter I explained why we should use DataPatch in the Magento 2. To create CMS-Block we laso will use DataPatch. It will save our time in the future. Solution <?php namespace Vendor\ModuleName\Setup\Patch\Data; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Framework\Setup\Patch\PatchRevertableInterface; use Magento\Cms\Model\BlockFactory; class CreateCmsBlock implements DataPatchInterface, PatchRevertableInterface { /** * @var BlockFactory */ protected BlockFactory $blockFactory; /** * @param BlockFactory $blockFactory */ public function __construct( BlockFactory $blockFactory ) { $this->blockFactory = $blockFactory; } /** * {@inheritdoc} */ public function apply() { $blockData = [ 'title' => 'Test CMS Block', 'identifier' => 'test_cms_block_identifier', 'content' => 'Yau!...

June 13, 2023 · 2 min

Magento 2: How to Create Cms Page Programmatically Using DataPatch

Introduction In this chapter we will create CMS-Page programmatically in the Magento 2. But first a few words about why DataPatch. If you are developing something for your Magento 2 client which has staging and production envirenments, the best way to do changes for admin part (like changing settings, updating attributes or modifying CMS-Blocks and CMS-pages) is to create DataPatch. DataPatch allows you to avoid remember what and where in the admin panel you did changes....

June 7, 2023 · 2 min

Magento 2: How to Update Stores Configuration Settings Programmatically

Introduction A few moments earlier I posted two articles: Magento 2: How to Change Category Attribute Value Programmatically Magento 2: How to Update Product Attribute Settings Programmatically where described why we should use Data Patch to update something in the admin panel. In this article I will show the method of how to update 'Stores > Configuration' settings in the Magento 2. Solution <?php declare(strict_types=1); namespace Vendor\ModuleName\Setup\Patch\Data; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Framework\App\Config\Storage\WriterInterface; class UpdateLayeredNavigationSettings implements DataPatchInterface { /** @var ModuleDataSetupInterface */ private ModuleDataSetupInterface $moduleDataSetup; /** * @var WriterInterface */ private WriterInterface $configWriter; /** * Constructor * * @param ModuleDataSetupInterface $moduleDataSetup * @apram WriterInterface $configWriter */ public function __construct( ModuleDataSetupInterface $moduleDataSetup, WriterInterface $configWriter ) { $this->moduleDataSetup = $moduleDataSetup; $this->configWriter = $configWriter; } /** * {@inheritdoc} */ public function apply() { $this->moduleDataSetup->startSetup(); $this->configWriter->save('catalog/layered_navigation/display_category', '0'); $this->configWriter->save('catalog/layered_navigation/display_product_count', '0'); $this->moduleDataSetup->endSetup(); } /** * {@inheritdoc} */ public static function getDependencies() { return []; } /** * {@inheritdoc} */ public function getAliases() { return []; } } Explanation If read my previous articles you remember that we used something like EavSetup model for the product attribute and Category Repository for the category attribute....

May 13, 2023 · 2 min

Magento 2: How to Update Product Attribute Settings Programmatically

Introduction Imagine similar situation as I described in the previous article Magento 2: How to Change Category Attribute Value Programmatically But now we need to change product attribute setting. And, as in previuos example, we can do it via admin panel. Buuut… would be better… Solution To use Data Patch. <?php declare(strict_types=1); namespace Vendor\ModuleName\Setup\Patch\Data; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Eav\Setup\EavSetup; use Magento\Eav\Setup\EavSetupFactory; use Magento\Catalog\Model\Product; class UpdateColorAttribute implements DataPatchInterface { /** @var ModuleDataSetupInterface */ private ModuleDataSetupInterface $moduleDataSetup; /** * @var EavSetupFactory */ private EavSetupFactory $eavSetupFactory; /** * Constructor * * @param ModuleDataSetupInterface $moduleDataSetup * @param EavSetupFactory $eavSetupFactory */ public function __construct( ModuleDataSetupInterface $moduleDataSetup, EavSetupFactory $eavSetupFactory ) { $this->moduleDataSetup = $moduleDataSetup; $this->eavSetupFactory = $eavSetupFactory; } /** * {@inheritdoc} */ public function apply() { /** @var EavSetup $eavSetup */ $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]); $eavSetup->updateAttribute(Product::ENTITY, 'color', ['used_in_product_listing' => 1]); } /** * {@inheritdoc} */ public static function getDependencies() { return []; } /** * {@inheritdoc} */ public function getAliases() { return []; } } Explanation We use updateAttribute method of the EavSetup model to update color product attribute....

May 13, 2023 · 2 min

Magento 2: How to Change Category Attribute Value Programmatically

Introduction Imagine we are working on the client’s project. It serves on staging and production servers, of course. But we are working locally. And we need to change a category attribute value. It’s easy! Just navigate to the admin panel and select the necessary value. But this is a bad practice. Because now we should remember about this attribute and after ticket will be deployed to staging we should change attribute value here also....

May 13, 2023 · 2 min