magento 2

Understanding $block->getChildHtml()

As I am learning magento, I had this issue from quite a while. Sometimes I was able to use $block->getChildHtml and sometimes not. So I asked help from my colleague Lee and he helped me to understand the whole concept of $block->getChildHtml();
Supposably I want to display this block in one of my view which block of class ‘Title’

echo $block->getChildHtml('sudeep_block');

So in layout file I have to define this as follows

<!-- page title -->
<block class="Vendor\Product\Block\Html\Title" name="page.main.title"
    <block class="Vendor\Product\Block\Html\Title" name="" as="sudeep_block"


If I don’t put this within <block class=”Vendor\Product\Block\Html\Title” ┬áit will not appear correctly. This is because, we are calling child HTML of this block
Thats the reply from my colleague Lee
“the problem with putting things in the container is that its just a container and things are ordered within that container based on the before and after elements that are added in the xml – it just prints all the blocks out that are set.
I’ve moved it into the title block – this sets it as a child, so you can either call ->getChildHtml() – that will pull all child elements in that block
or ->getChildHtml(‘name-of-block’) – and that will call the specific block that you want
if its available obviously!”

Magento 2 add static block in Layout XML

Here is a quick way on how to add static cms block in the layout

Display CMS Static Block In Phtml File:

Here the code to show CMS Static Block in any template (phtml) file in Magento 2,


Display CMS Static Block In CMS Content:

Here the code to show CMS Static Block in any other CMS static block/page in Magento 2,

{{block class="Magento\\Cms\\Block\\Block" block_id="block_identifier"}}

Display CMS Static Block In XML:
Here the code to show CMS Static Block in any layout (XML) file in Magento 2,


<referenceContainer name="content"> 
<block class="Magento\Cms\Block\Block" name="block_identifier"> 
<argument name="block_id" xsi:type="string">block_identifier</argument> 



Thanks to info at

Magento 2 admin catalog product edit form – categories order

I have recently been doing some developments in Magento 2 and have found out some inconsistency in the product edit form in the admin section.

The desired sort order of the category should be the default position one. However in the product edit admin catalog product edit form, the sort order is not defined therefore, it appears in the way it is returned from the database. This is not very user-friendly especially if you have about 4000 categories to deal with.

Magento 2 admin product edit form category sort order - backend Magento 2 natural - original  category sort order - frontend
admin catalog product edit form – categories order categories order on frontend

Thus to obtain same layout, we have made changed in following pre. You can also create pre pool and add the pre there, but this is a quick fix

My Magento version is community 2.2.6



Go to line 375 and add the following code


Make sure to add this line before

foreach ($collection as $category) {.

So the final code should look like as follows


        foreach ($collection as $category) {
            foreach ([$category->getId(), $category->getParentId()] as $categoryId) {
                if (!isset($categoryById[$categoryId])) {
                    $categoryById[$categoryId] = ['value' => $categoryId];

            $categoryById[$category->getId()]['is_active'] = $category->getIsActive();
            $categoryById[$category->getId()]['label'] = $category->getName();
            $categoryById[$category->getParentId()]['optgroup'][] = &$categoryById[$category->getId()];

            self::CATEGORY_TREE_ID . '_' . $filter,

        return $categoryById[CategoryModel::TREE_ROOT_ID]['optgroup'];

Thus my final version looks as following

Magento 2 quick check list before going live

Hello all

I have been working on Magento2 from quiet a while and getting my hands on its development & deployment. I know it always happens when you develop or customise something and keep on refreshing the page, but you does not get the desired results.

Therefore I have created a quick checklist to make sure all boxes are ticked before you go live. I have also included the commands so that its handy to run them in order

Production mode is enabled

php bin/magento deploy:mode:set production

Cache is first flushed and then enabled

php bin/magento cache:clean
php bin/magento cache:enable

Static contents have been deployed

 php bin/magento setup:static-content:deploy

var folder persmissions

chmod -R 777 var

pub folder persmissions

chmod -R 777 pub

Static contents have been deployed

 php bin/magento setup:static-content:deploy

Catalog has been reindexed

 php bin/magento indexer:reindex