You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shopware 6 自定义规则更新进阶价格:KeyValuePair类型错误

Shopware 6 自定义进阶价格更新时的 TypeError 问题处理

我尝试通过编程方式在Shopware 6中使用自定义规则(进阶价格)更新产品价格,使用的数据数组如下:

array:2 [
  0 => array:3 [
    "productNumber" => "Test1"
    "prices" => array:4 [
      "ruleId" => "cfda4784f29b40428c881e3a1878ad69"
      "quantityStart" => 1
      "price" => array:1 [
        0 => array:4 [
          "currencyId" => "b7d2554b0ce847cd82f3ac9bd1c0dfca"
          "gross" => 2.0
          "net" => 1.0
          "linked" => true
        ]
      ]
    ]
    //This is the product ID
    "id" => "dc00dd078e4949bc9aadb14e62cf6924"
  ]
  1 => array:3 [
    "productNumber" => "Test2"
    "prices" => array:4 [
      "ruleId" => "cfda4784f29b40428c881e3a1878ad69"
      "quantityStart" => 1
      "price" => array:1 [
        0 => array:4 [
          "currencyId" => "b7d2554b0ce847cd82f3ac9bd1c0dfca"
          "gross" => 2.0
          "net" => 1.0
          "linked" => true
        ]
      ]
    ]
    //This is the product ID
    "id" => "2324be8b90a54524914b86bc8f0521e4"
  ]
]

运行自定义CLI命令时,触发如下错误:

[TypeError]
Shopware\Core\Framework\DataAbstractionLayer\Write\DataStack\KeyValuePair::__construct(): Argument #1 ($key) must be of type string, int given, called in /var/www/html/vendor/shopware/core/Framework/DataAbstractionLayer/Write/DataStack/DataStack.php on line 47

我尝试直接向product_price表插入数据而非更新产品,仍出现相同错误。


问题原因与解决方案

错误根源是**prices字段的结构不符合Shopware数据抽象层(DAL)的要求**:

  • Shopware中产品的进阶价格是一对多关联,prices字段需要接收一个包含多个价格对象的索引数组
  • 当前代码把单个价格对象直接赋值给prices,而非包裹在数组中,导致DAL处理时识别到int类型的键,触发类型错误

修正后的数组结构

array:2 [
  0 => array:3 [
    "productNumber" => "Test1"
    "prices" => [ // 改为数组包裹单个价格对象
      array:4 [
        "ruleId" => "cfda4784f29b40428c881e3a1878ad69"
        "quantityStart" => 1
        "price" => array:1 [
          0 => array:4 [
            "currencyId" => "b7d2554b0ce847cd82f3ac9bd1c0dfca"
            "gross" => 2.0
            "net" => 1.0
            "linked" => true
          ]
        ]
      ]
    ]
    "id" => "dc00dd078e4949bc9aadb14e62cf6924"
  ]
  1 => array:3 [
    "productNumber" => "Test2"
    "prices" => [ // 同样改为数组包裹
      array:4 [
        "ruleId" => "cfda4784f29b40428c881e3a1878ad69"
        "quantityStart" => 1
        "price" => array:1 [
          0 => array:4 [
            "currencyId" => "b7d2554b0ce847cd82f3ac9bd1c0dfca"
            "gross" => 2.0
            "net" => 1.0
            "linked" => true
          ]
        ]
      ]
    ]
    "id" => "2324be8b90a54524914b86bc8f0521e4"
  ]
]

补充说明

  • 优先通过ProductRepository的upsert方法处理价格更新,而非直接操作数据库表,这样能确保Shopware的业务逻辑、数据校验和事件系统正常运行
  • 如果需要添加多个进阶价格,只需在prices数组中追加更多价格对象即可

内容的提问来源于stack exchange,提问作者balrcoding

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 21:30:54