我有一个Shopware 6插件,它当前使用布尔配置reviewSkipModeration值,可以自动发布通过该插件提交的任何 comments :

<input-field type="bool">
  <name>reviewSkipModeration</name>
  <label>Accept reviews automatically</label>
  <defaultValue>true</defaultValue>
</input-field>

我想将其迁移到具有以下选项的多选配置:

  • 0颗星
  • 1颗星
  • 2颗星
  • 3颗星
  • 4颗星
  • 5星

在我的UPDATE方法中,如何正确地迁移数据,以便:

  • true maps to 0颗星
  • false maps to 无
  • 并自动发布高于某个星级阈值的 comments ,例如3星.但我需要这样做,而不 destruct 现有的功能,为用户谁已经安装了插件.

并删除旧的布尔配置值?

我的插件更新函数如下所示:

    public function update(UpdateContext $context): void {
        parent::update($context);
    }

但我不确定如何处理映射和删除.如有任何帮助,我们不胜感激!

推荐答案

配置存储在数据库中,可以通过直接MySQL查询进行读/写,也可以使用SystemConfigService.

public function update(UpdateContext $context): void
{
    parent::update($context);

    // Get config service from the container
    $configService = $this->container->get('Shopware\Core\System\SystemConfig\SystemConfigService');
    
    // Get the current value of the configuration
    $currentValue = $configService->getBool('YourPlugin.config.reviewSkipModeration');
    
    // Specify the new value
    $value = 'None'
    
    if($currentValue === true) {
        $value = '0 Stars'
    }
    
    // Set the specified value as default
    $configService->set('YourPlugin.config.reviewSkipModeration', $value);
}

下面是一个基本的示例,但请记住,您可能需要处理所有销售渠道配置的更新和默认配置(SalesChannelID=空)

$configService->getBool('YourPlugin.config.reviewSkipModeration', $salesChannelId);
$configService->set('YourPlugin.config.reviewSkipModeration', $value, $salesChannelId);

Php相关问答推荐

根据特定运输类别增加WooCommerce购物车总重量

自定义WooCommerce帐户下载列表

无法在WooCommerce中发送客户笔记邮箱通知

在WooCommerce产品变体SKU字段旁边添加自定义输入字段

为什么正则表达式与得到的文本块之前得到的也行?

Laravel:测试命令时无法获取工作通知::AssertSentTo

使注册字段UserMeta值对于WooCommerce中的每个用户是唯一的

如何在php网站代码中清理浏览器缓存

Laravel区分大小写搜索

如何配置我的.env文件以在laravel的一个项目中连接两个不同的数据库?

将购物车按钮重定向到Checkout-Wooccommerce

未定义的类型';PDF';在Laravel 8

用户注册表单中的 HTTP 错误 405

Google Drive API:为什么使用服务帐户在下载文件时会将我带到 Google Drive 登录窗口?

无法在 Laravel 中实例化抽象类 ProductAbstract

Xdebug 不会在断点处停止

同一页面上多个组件的 Livewire 分页问题

PHP 中的正则表达式,具有字符范围,但不包括特定字符

如何在 Process facade 中转义特殊字符?

使用 Eloquent 更新现有的外键值