2

In index.php, there is a call to instantiate the Magento\Framework\App\Bootstrap object with $_SERVER as the initial parameters.

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

We had a hack in the index.php (based on this gist) so that we could use the same URL for multiple stores and show different content based on the cookie settings.

$params = $_SERVER;
if (isset($_COOKIE[\Magento\Store\Model\StoreCookieManager::COOKIE_NAME])) {
    $storeCode = $_COOKIE[\Magento\Store\Model\StoreCookieManager::COOKIE_NAME];
}
if (isset($_GET[\Magento\Store\Api\StoreResolverInterface::PARAM_NAME])) {
    $storeCode = $_GET[\Magento\Store\Api\StoreResolverInterface::PARAM_NAME];
}
if (isset($storeCode)) {
    $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = $storeCode;
    $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'store';
}
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);

The problem is that we're now shifting to the Magento Commerce Cloud servers and the index.php is regenerated on build, so we can't rely on a change to the index.php anymore.

I've tried creating a module which adds a DI preference for my own version of the Bootstrap class (which contains the above code in the create method) instead of the Magento version, but this doesn't appear to be working - I suspect because it might be too early in the process for interceptors to be in play.

Does anyone have any insight into how to overcome this issue?

1

I was able to accomplish something similar (although, not on Magento Commerce Cloud) by creating a plugin for the method:

Magento\Store\Model\StoreResolver::getCurrentStoreId()

In my case, if no store is set (new visitor), I am choosing the store to show based on custom logic with $this->getStore().

public function aroundGetCurrentStoreId(\Magento\Store\Model\StoreResolver $subject, callable $proceed)
{
    $storeCode = $this->_request->getParam(
        $subject::PARAM_NAME,
        $this->_storeCookieManager->getStoreCodeFromCookie()
    );

    // If store code is not set
    if (!$storeCode) {
        // Try to load store
        if ($id = $this->getStore()->getId()) {
            return $id;
        }
    }

    return $proceed();
}

I did run into issues with a Segmentation fault PHP error if I tried to set/delete the cookie with the Magento\Store\Model\StoreCookieManager model within the plugin.

I'm not entirely sure how Magento Commerce Cloud is setup, but I hope this can help. Please let me know if you would like any more details, and please update the thread if you figure anything else out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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