The title kind of says it all, just to be a bit more specific:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <!-- **** if store == my-special-case-store  -->
    <preference for="Magento\Catalog\Block\Product\View\Options" type="MyPackage\MyModule\Block\Options"/>
    <!-- endif -->


also if I wanted to do the same with "view/frontend/layout/default.xml" how do I accomplish this?

I'm just looking for directions here.

  • ` is code working for di.xml`
    – Amit Bera
    Oct 12, 2017 at 19:03

1 Answer 1


I am not sure di.xml. store specifictarget.

But i am 100% sure that you cannot make default.xml store specific.

If you want store layout files and where you want to write store specific layout code and want to override other existing layout like default.xml.

At magento2, layout file name is equal to hanlde name

Then you need to create custom extension on this extension we will create**store specific handler like** store_default,store_us etc,

where pattern is store_{YourStore_code}.

On observer on eventlayout_load_before

  • then add your event at observer
  • using below code


So, you create layout files like

  • store_default.xml
  • store_us.xml
At app/design/frontend/{Themevendorname}/{Themename}/layout/{Vendorname}_{Modulename}/layout

This module should consist of below files:

  • app/code/app/code/{vendorname}/{Modulename}/registration.php
  • app/code/{vendorname}/{Modulename}/composer.json
  • app/code/{vendorname}/{Modulename}/etc/module.xml

Define event at



<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="layout_load_before">
        <observer name="add_Store_layout" instance="{vendorname}\{Modulename}\Observer\addStoreLayout" />


Define observer which create store_{YourStore_code} pattern handle



namespace {vendorname}\{Modulename}\Observer;
use Magento\Framework\Event\ObserverInterface;

class addStoreLayout implements ObserverInterface
    protected $storeManager;
     * addStoreLayout constructor.
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager
        $this->storeManager = $storeManager;

    public function execute(\Magento\Framework\Event\Observer $observer)
        //['full_action_name' => $this->request->getFullActionName(), 'layout' => $this->layout]
        $layout = $observer->getLayout();

At header.phtml

add this code , and you will see you handler

echo "<pre>";
  • So I did finally implement this, and worked super! and oh 1 thing please edit your code: "etc/events.xml" to "etc/frontend/events.xml"
    – Mac A.
    Oct 19, 2017 at 17:21
  • Found an issue, it won't give me the option to insert handle at index, it only adds handle at the end of process queue aka: $layout->getUpdate()->handles private array object. "vendor\magento\framework\View\Model\Layout\Merge.php" lines 73,234
    – Mac A.
    Oct 19, 2017 at 20:01
  • Removed all items, and re-added them in the order I wanted. Thanks again.
    – Mac A.
    Oct 20, 2017 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.