shopware - Shopware 6 - 从产品存储库中获取与特定销售渠道相关联的产品
问题描述
情况
我正在构建一个 CLI 命令来获取与给定销售渠道相关联的所有产品。例如,我想拥有 B2B 渠道的所有产品。
目前的方法
通过阅读文档,我的理解是我需要执行以下操作:
- 使用
Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory
- 传递
context
到产品存储库
这是一些伪代码。我使用,RepositoryIterator
但基本上它只是包装任何EntityRepositoryInterface
. 在这种情况下,产品存储库。
$options = [
SalesChannelContextService::LANGUAGE_ID => MY_STORE::B2B
];
$salesChannelContext = $this->salesChannelContextFactory->create('', $this->salesChannelId, $options);
return new RepositoryIterator($productRepository, $this->salesChannelContext->getContext(), $criteria);
如果我遍历存储库,我现在得到的仍然是所有实体的列表,而不是特定于所提供销售渠道的实体。
那么我错过了什么?
解决方案
我现在想通了。似乎这SalesChannelContext
还不够。您还需要添加过滤器。这是管理区域中的预期行为。
所以这将是标准:
...
$criteria = new Criteria();
$criteria->addFilter(new EqualsAnyFilter('visibilities.salesChannel.id', [$this->salesChannelId]));
...
return new RepositoryIterator($this->productRepository, $this->salesChannelContext->getContext(), $criteria);
它仍然感觉不直观,因为SalesChannelContext
我的责任并不清楚。
推荐阅读
- python - 我如何递归这个函数?
- angularjs - Angular 表单数组以及如何显示错误消息
- python - cv2.rectangle() 是否有一个名为“rec”的参数?
- android - 我想在Android上自动点击权限窗口
- python - IF 语句未在 bash 脚本中执行 - 将变量与 Int 进行比较
- r - 如何在多图中绘制多层次变量
- ios - 如何添加一个已经有父控制器的视图控制器作为另一个视图控制器的子控制器
- pdf-generation - 在客户端生成 PDF
- go - 带上下文取消的异步/等待模式
- javascript - 将 jest 测试套件生成为单独的文件而不是单个文件