How to Add a Cookie Banner to Magento
In this article
Adding a cookie banner to your Magento store is essential for GDPR and CCPA compliance, as it informs visitors about the cookies your site uses and obtains consent where required. This guide walks you through the process using Magento's built-in Cookie Restriction Mode, which is available in both Magento 2 and Magento 1 (with slight variations). No coding is required, though you may optionally customize the banner text. You'll need admin access to your Magento backend and the ability to edit CMS blocks.
What You'll Need
- Administrator access to your Magento admin panel (backend).
- FTP or file manager access if you plan to customize the banner template (optional).
- Basic knowledge of Magento's admin navigation.
Step-by-Step
- Log in to your Magento admin panel. Navigate to your store's backend URL (e.g., yourdomain.com/admin) and enter your credentials.
- Enable Cookie Restriction Mode. In the admin sidebar, go to Stores → Configuration → General → Web. Under the Default Cookie Settings section, find the Cookie Restriction Mode option and set it to Yes. This activates the cookie consent feature.
- Save the configuration. Click the Save Config button at the top right. Magento will now display a default cookie notice on your storefront.
- Create a CMS block for the cookie notice content. Go to Content → Elements → Blocks. Click Add New Block. Give it an identifier like
cookie-notice, set the store view to All Store Views, and in the content area, write your cookie policy message. For example: "We use cookies to improve your experience. By continuing to browse, you agree to our use of cookies." You can include a link to your privacy policy. Click Save Block. - Assign the block to the cookie notice. In the admin sidebar, go to Content → Design → Configuration. Find your store's row and click Edit. Under Other Settings, expand the HTML Head section. In the Cookie Restriction Notice Block field, select the block you just created (e.g.,
cookie-notice). Save the configuration. - Clear the cache. Go to System → Cache Management and click Flush Magento Cache (or the corresponding button for your version). This ensures the changes take effect.
- Test the banner. Visit your storefront in a private/incognito browser window. You should see the cookie notice at the bottom of the page. Click the Allow button (or similar) to verify it disappears and sets a cookie.
- Optional: Customize the banner design. If you want to change the look (e.g., colors, position), you can override the template file. In Magento 2, copy
vendor/magento/module-cookie/view/frontend/templates/html/notices.phtmltoapp/design/frontend/YourVendor/YourTheme/Magento_Cookie/templates/html/notices.phtmland edit it. In Magento 1, the file is atapp/design/frontend/base/default/template/page/html/cookie_notice.phtml. You can then add CSS classes or inline styles.
Common Mistakes
- Forgetting to clear the cache: Changes to blocks or configuration may not appear until the Magento cache is flushed.
- Not creating a block before assigning it: If you try to select a block that doesn't exist, the dropdown will be empty or you'll get an error.
- Using the wrong store view: If you assign the block to a specific store view instead of "All Store Views", it may not appear on all your websites.
- Overriding the template incorrectly: In Magento 2, placing the template in the wrong folder (e.g., directly under
app/design/frontendwithout the vendor/theme structure) will cause it to be ignored. - Not testing in incognito mode: If you test while logged in as admin or with existing cookies, the banner may not show.
Tips
- Use a clear and concise message: Keep your cookie notice short and include a link to your full privacy policy.
- Make sure the banner is mobile-friendly: Test on a phone to ensure the banner doesn't cover important content.
- Consider GDPR requirements: For European visitors, you may need to obtain explicit consent before setting non-essential cookies. Magento's built-in mode only shows a notice; it doesn't block cookies. For full compliance, consider a third-party extension that supports opt-in consent.
- Update your privacy policy: Ensure your privacy policy page lists all cookies you use, including analytics and marketing cookies.
- Use a translation if your store is multilingual: You can create separate blocks for different store views and assign them accordingly.
Troubleshooting
- Banner doesn't appear: First, confirm that Cookie Restriction Mode is set to Yes and the cache is cleared. Also check that you are viewing the site in a browser with no existing Magento cookies (use incognito). If it still doesn't show, verify that the CMS block is assigned correctly in the design configuration.
- Banner appears but clicking "Allow" doesn't hide it: This can happen if the JavaScript that handles the click is not loading. In Magento 2, ensure that the
Magento_Cookiemodule is enabled. You can check by going to Stores → Configuration → Advanced → Advanced and ensuring the module output is not disabled. - Custom block content isn't showing: Double-check that the block identifier matches exactly what you entered in the design configuration. Also make sure the block is enabled and assigned to the correct store view.
- Error after enabling Cookie Restriction Mode: Some themes may have conflicts. Try switching to a default theme (e.g., Luma) temporarily to see if the issue persists. If it's a theme issue, contact your theme developer.
- Banner appears on every page even after clicking Allow: This suggests the cookie is not being set. Check that your site's base URL is correct and that cookies are not being blocked by browser settings. Also ensure that the cookie path is set to '/' in the configuration.
Frequently Asked Questions
Does Magento's built-in cookie banner comply with GDPR?
Magento's Cookie Restriction Mode shows a notice and requires a click to allow cookies, but it does not block cookies before consent. For strict GDPR compliance, you may need a third-party extension that prevents non-essential cookies from being set until consent is given.
Can I add a cookie banner without editing code?
Yes, the built-in feature requires no coding. You only need to enable it and optionally create a CMS block for the message.
How do I change the text of the cookie banner?
Create a CMS block with your desired text and assign it to the Cookie Restriction Notice Block field in the design configuration.
Will the cookie banner appear on all pages?
Yes, the banner appears on all pages of your storefront until the visitor clicks the allow button.
Can I have different cookie messages for different languages?
Yes, create separate CMS blocks for each store view and assign the appropriate block to each view in the design configuration.
What if I already have a third-party cookie banner extension?
You can disable Magento's built-in mode and rely on the extension. Just set Cookie Restriction Mode to No and follow the extension's instructions.