To insert a custom script immediately after the opening <head> tag in Magento, you can utilize layout XML files. This method allows you to manage your scripts efficiently without directly modifying core files.

Step-by-Step Guide

  1. Create a Custom Block: First, you need to create a block that will hold your custom script. Here’s an example of how to define a block in Custom/Module/Block/Onepage/Success.php:

    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();
            $lastOrderId = $this->getOrderId();
            if (empty($lastOrderId)) {
                return null;
            }
            return $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($lastOrderId);
        }
    }
  2. Create a Helper Class: You may also want to create a helper class to manage configurations. Here’s a simple example for Custom/Module/Helper/Data.php:

    namespace Custom\Module\Helper;
    class Data extends \Magento\Framework\App\Helper\AbstractHelper {
        protected $_request;
    
        public function __construct(\Magento\Framework\App\Helper\Context $context, \Magento\Framework\App\Request\Http $request) {
            $this->_request = $request;
            parent::__construct($context);
        }
    
        public function getConfigValue($value = '') {
            return $this->scopeConfig->getValue($value, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
        }
    }
  3. Define Dependency Injection: In your etc/di.xml, set up the preference for 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>
  4. Modify Layout XML: In your layout file located at Custom/Module/view/frontend/layout/default.xml, add the following code to reference your custom block:

    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <head>
            <referenceContainer name="head.additional">
                <block class="Custom\Module\Block\Onepage\Success" template="Custom_Module::success/head.phtml"/>
            </referenceContainer>
        </head>
    </page>
  5. Create the Template File: Finally, create the template file Custom/Module/view/frontend/templates/success/head.phtml where you will place your custom script:

    <script>
        console.log("I'm loaded!");
    </script>

Conclusion

By following these steps, you can successfully add custom scripts right after the opening <head> tag in your Magento store. This approach keeps your modifications organized and maintainable.