Skip to content

Instantly share code, notes, and snippets.

@vijayrami
Last active February 5, 2023 10:12
Show Gist options
  • Save vijayrami/36e5f4d3afc16f8641377c7308d76621 to your computer and use it in GitHub Desktop.
Save vijayrami/36e5f4d3afc16f8641377c7308d76621 to your computer and use it in GitHub Desktop.
Magento2 Code Samples

Add image path in CSS

background: url('@{baseDir}images/icon_sprite.png');

Add Static Block

In .phtml tempalte file:

<?php echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_identifier')->toHtml();?>

In CMS content:

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

In layout .xml file:

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

Add two block is same referance container

<referenceBlock name="product.info.details">
        <block class="Magento\Framework\View\Element\Template" template="Magento_Theme::catalog/product/tabs/tab1.phtml" name="extra-tab-1" group="detailed_info">
            <arguments>
                <argument name="title" translate="true" xsi:type="string">Extra Tab 1</argument>

            </arguments>
        </block>

        <block class="Magento\Framework\View\Element\Template" template="Magento_Theme::catalog/product/tabs/tab2.phtml" name="extra-tab-2" group="detailed_info">
            <arguments>
                <argument name="title" translate="true" xsi:type="string">Extra Tab 2</argument>

            </arguments>
        </block>

</referenceBlock>

Get a list of events generated when you perform any action in Magento 2

create ‘di.xml file inside our extension folder at below path using following code.

app\code\Magelearn\Mymodule\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <type name="Magento\Framework\Event\Manager">
       <plugin name="list_all_dispatched_event" type="Magelearn\Mymodule\Plugin\ListDispatchedEvents" sortOrder="10" disabled="false"/>
   </type>
</config>

Now, we need to create on more file ‘ListDispatchedEvents.php‘ inside our extension folder.

app\code\Magelearn\Mymodule\Plugin\ListDispatchedEvents.php

<?php
namespace Magelearn\Mymodule\Plugin;

class ListDispatchedEvents
{
    public function beforeDispatch($subject, $eventName, array $data = [])
    {
	$writer = new \Zend_Log_Writer_Stream(BP . '/var/log/event.log');
       $logger = new \Zend_Log();
	$logger->addWriter($writer);
	$logger->info($eventName);
    }
}

Add Custom Attribute Label and Value in Product list/grid info (category product list) page.

Edit below file

app/design/frontend/CUSTOM/THEME/Magento_Catalog/templates/product/list.phtml

Get Attribute custom code

$attribute = $_product->getResource()->getAttribute('attribute_code');

Get Value

$attributeValue = $attribute->getFrontend()->getValue($_product);

Get Lable

$attributeLabel = $attribute->getStoreLabel();

Magento2 Developer Toolbar

https://www.mgt-commerce.com/documentation/magento2-mgt-developer-toolbar

https://github.com/vpietri/magento2-developer-quickdevbar

Magento2 Add Zend Log

$writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom.log');
$logger = new \Zend_Log();
$logger->addWriter($writer);
$logger->info("<pre>".print_r($paymentQuote->getData())."</pre>");

Add custom CSS in Magento2

Add below code in Magento_Theme/layout/default_head_blocks.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
    <css src="css/font-css.min.css" />
    </head>
</page>

Download font and css library

place the css files in <custom-theme-directory>/web/css
 
place the font files in <custom-theme-directory>/web/fonts

Add custom CSS in footer in Magento2

Add below code in: app/design/frontend/<module>/<theme>/Magento_Theme/layout/default.xml

<referenceContainer name="before.body.end">
	    <block class="Magento\Framework\View\Element\Template" template="Magento_Theme::custom_css.phtml" name="before_custom_css"/>
</referenceContainer>

in app/design/frontend/<module>/<theme>/Magento_Theme/templates/custom_css.phtml

<link  rel="stylesheet" type="text/css"  media="all" href="<?php echo $block->getViewFileUrl('Magento_Theme::css/custom.css')?>" />

Add custom.css file in app/design/frontend/<module>/<theme>/Magento_Theme/web/css

Display log for object in Magento2

$this->_logger->debug($order->convertToJson());

$this->_logger->debug('<pre>'.print_r($order_billing_data,1).'</pre>');

List of all dispatched events

https://cyrillschumacher.com/magento-2.4-list-of-all-dispatched-events/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment