Adding Custom Script Just After Head Tag in Magento 2
If you’re looking to add a custom script right after the opening <head> tag in your Magento 2 store, you’ve landed on the right page. I’ll guide you through this process step by step.
Understanding the Head Section
Before we dive into the code, let’s briefly understand the <head> section of an HTML document. The <head> tag contains meta-information about the HTML document and is placed between the opening html tag and the opening body tag.
Your Current Approach
You’ve already tried adding a script in your default_head_blocks.xml layout file, but you’re seeing the script being added before the closing </head> tag. This is because the referenceContainer with name “head.additional” is placed before the closing </head> tag.
<referenceContainer name="head.additional">
<block class="Custom\Module\Block\Success" template="Custom_Module::success/head.phtml"/>
</referenceContainer>
The Solution: Custom Template and Block
To achieve your goal, you’ll need to create a custom template that contains the script, and then reference this template in a block that is inserted into the head section.
Step 1: Create the Custom Template
Create a new file success/head.phtml in your module’s view folder (usually found at app/code/Custom/Module/view/frontend/templates/success).
<script>
console.log("I'm loaded!");
</script>
This template will contain the script you want to add.
Step 2: Create the Custom Block
Create a new file Onepage/Success.php in your module’s block folder (usually found at app/code/Custom/Module/Block/Onepage).
namespace Custom\Module\Block\Onepage;
use Magento\Framework\View\Element\Template;
class Success extends \Magento\Checkout\Block\Onepage\Success {
public function getOrder()
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$helper = $objectManager->get('Custom\Module\Helper\Data');
$lastOrderId = $this->getOrderId();
if (empty($lastOrderId))
{
return null;
}
$orderData = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($this->getOrderId());
return $orderData;
}
public function getTemplate()
{
if ($this->helper->getConfigValue('custom_general/general/active') == 1) {
$template = 'Custom_Module::success/head.phtml';
} else {
$template = 'Magento_Checkout::success.phtml';
}
return $template;
}
}
Step 3: Update the DI File
In your module’s di.xml file, update the preference for the success block to point to your custom block.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../vendor/magento/framework/ObjectManager/etc/config.xsd">
<preference for="Magento\Checkout\Block\Onepage\Success" type="Custom\Module\Block\Onepage\Success"/>
</config>
Step 4: Update the Layout XML
In your module’s default.xml layout file, add a new reference to the head section.
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="require.js">
<action method="setTemplate">
<argument name="template" xsi:type="string">Custom_Module::success/head.phtml</argument>
</action>
</referenceBlock>
</body>
</page>
Conclusion
Following these steps, you should now have your custom script added right after the opening <head> tag in your Magento 2 store. Remember to clear your cache and re-compile the static files for the changes to take effect.
This approach demonstrates a clean way of extending Magento’s core functionality using custom blocks and templates. If you need more complex logic, consider creating a new module or extending an existing one.
Don’t forget to check out other articles on Laravel Company for more in-depth guides on Magento development and best practices.
Happy coding!