我有Shopware 5插件,它将一些js代码添加到商店的模板中。有main shop_id = 1
和subshop shop_id = 3
,当我配置插件时,在backend > configuration > plugin_manager > plugin > configuration
的每个商店都有单独的配置。
当我在数据库表s_core_config_values
中检查它时,我发现为每个shop_id
添加了两个不同的配置。
在主要的pugin文件中,我有一个订户方法,在该方法中,我正在获取插件配置,然后注册一些事件,例如
public static function getSubscribedEvents()
{
return [
'Enlight_Controller_Action_PostDispatchSecure_Frontend' => 'doSthMethod',
];
}
但是问题在于,总是有主要的插件配置(即使我在子商店中,也有shop_id = 1
的插件。在我的主插件文件中
class Shopware_Plugins_Backend_Pluginname_Bootstrap extends Shopware_Components_Plugin_Bootstrap
有$config = $this->Config();
总是返回主车间配置,那么有什么方法可以得到实际的子车间配置?
编辑:
我已经从第一个答案中尝试了解决方案,但是无论如何,无论我从哪个子商店提出请求,我都会在以下情况下返回我的主要商店:
$this->container->get('models')->getRepository(\Shopware\Models\Shop\Shop::class)->getActiveDefault();
返回主店后,我从以下位置获取默认商店配置:
$this->container->get('shopware.plugin.cached_config_reader')->getByPluginName('PluginName', $shop);
当我打电话时:
$this->container->get('models')->getRepository(\Shopware\Models\Shop\Shop::class)->getActiveShops();
然后,我会获得所有商店(包括子商店)的列表,但是我无法区分请求来自哪个子商店。
答案 0 :(得分:1)
您应该使用shopware.plugin.cached_config_reader
服务来阅读插件配置。您可以在documentation中找到一个很好的例子:
public function onPostDispatch(Enlight_Event_EventArgs $arguments)
{
$shop = false;
if ($this->container->initialized('shop')) {
$shop = $this->container->get('shop');
}
if (!$shop) {
$shop = $this->container->get('models')->getRepository(\Shopware\Models\Shop\Shop::class)->getActiveDefault();
}
$config = $this->container->get('shopware.plugin.cached_config_reader')->getByPluginName('PluginName', $shop);
[...]
}