Easy way to Test Emails in Magento2

An easy way to test emails in Magento 2 is just add the following lines echo $content; exit(); into the vendor/magento/framework/Mail/Template/TransportBuilder.php file. Navigate to the file above and scroll to the line 380. It should be $content = $template->processTemplate(); inside prepareMessage() method. And add the lines echo $content; exit(); after line 380. The code should look like this: protected function prepareMessage() { $template = $this->getTemplate(); $content = $template->processTemplate(); echo $content; exit(); switch ($template->getType()) { ....

January 22, 2024 · 1 min

Why no Orders in My Account in Magento 2 - You have placed no orders

Orders are missing on Order History page in My Account on Front-end The Orders are missing and the error You have placed no orders is presented in My Account on Order History page. But! The customer has orders and they are presented in the admin panel. Usualy it happens because of the Order Status has Visible on Storefront = No. How to display missing orders on Order History page in My Account on Front-end In the admin panel navigate to the Sales > Orders and open Order that 100% customer did and it doesn’t displays on Order History page....

September 4, 2023 · 2 min

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: List of All Available Condition Types Used in Search Criteria

Introduction In the Magento 2 when we build a Search Criteria we use filters. And each filter takes parameter condition type. This parameter is necessary to say Magento how we want to filter a field: should a field be greater or less than passed value to the filter, for example. In my previous article I explained in simple terms how we can use Search Criteria. In this article I prepared the table of all available condition types in the Magento 2 with filters and conditions and without it....

June 4, 2023 · 1 min

Magento 2: How to Get Category List. Use Search Criteria.

Introduction Sometime we need to load Category List while developing something for Magento 2. And there are a few ways to do it. But in this small chapter we will use the right way. Solution The right way to load Category List includes two things: use CategoryListInterface to get the list of categories use Search Criteria to build a query Here is the basic code to load Category List. This code will load All Categories, becuase we are using empty Search Criteria....

June 4, 2023 · 4 min

Magento 2: How to Call Cms Block in PHTML Template Correctly

Introduction I’m sure you know how to do it. Just paste the following code into the necessary *.phtml template and that’s it: <?php echo $this->getLayout() ->createBlock(\Magento\Cms\Block\Block::class) ->setIdentifier('block_identifier') ->toHtml(); ?> But! Did you know that the \Magento\Cms\Block\Block is depricated since magento 2.4.2 ? So, the solution above is work for Magento 2.4.1 and less. And for Magento 2.4.2 and higher the solution below. Solution If you updated to the newest Magento version (2....

June 3, 2023 · 1 min

How to Configure Magento 2 for the Comfortable Development

Solution There will be no introduction here. Straight to the point. For a comfortable development for the Magento 2 I use the following setting: Enable developer mode. In this mode: you can see errors; the static content like css and javascript files will be linked to the pub folder and you no need to do setup:static-content:deploy each time after an changes. To do it, please run the command: bin/magento deploy:mode:set developer Disable the following cache types: layout block_html full_page Disabling of these caches will slow down the page speed loading (a little bit), but will saves your time and nerves....

May 30, 2023 · 1 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