1

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 -->

</config>

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.

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

1 Answer 1

2

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

$layout->getUpdate()->addHandle('store_'.$Storecode);

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

app/code/{vendorname}/{Modulename}/etc/events.xml

code:

<?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" />
    </event>

</config>

Define observer which create store_{YourStore_code} pattern handle

app/code/{vendorname}/{Modulename}/Observer/addStoreLayout.php

code:

<?php
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();
        $layout->getUpdate()->addHandle('store_'.$this->storeManager->getStore()->getCode());
    }
}

At header.phtml

add this code , and you will see you handler

echo "<pre>";
var_dump($block->getLayout()->getUpdate()->getHandles());
3
  • 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.
    Commented 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.
    Commented Oct 19, 2017 at 20:01
  • Removed all items, and re-added them in the order I wanted. Thanks again.
    – Mac A.
    Commented Oct 20, 2017 at 15:56

Your Answer

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

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