英文:
Shopware 6 get products from product stream with currency and storefront language
问题
我有以下变量:
$secretApiKey = $a->get('secretApiKey');
$publicApiKey = $a->get('publicApiKey');
$productStreamId = $a->get('productStreamId');
$salesChannelId = $a->get('salesChannelId');
$ruleId = $a->get('ruleId');
$currencyId = $a->get('currencyId');
如何通过给定的 storefront 的货币和默认语言从产品流中获取产品?
我看到了这个链接:https://github.com/shopware/core/blob/trunk/Content/Product/SalesChannel/CrossSelling/ProductCrossSellingRoute.php
但是我没有上下文,因为该函数可以通过命令和计划任务执行。
英文:
i have the following variables:
$secretApiKey = $a->get('secretApiKey');
$publicApiKey = $a->get('publicApiKey');
$productStreamId = $a->get('productStreamId');
$salesChannelId = $a->get('salesChannelId');
$ruleId = $a->get('ruleId');
$currencyId = $a->get('currencyId');
how can i get the products from the product stream with the currency and the default language of the storefront given?
I have seen the following: https://github.com/shopware/core/blob/trunk/Content/Product/SalesChannel/CrossSelling/ProductCrossSellingRoute.php
But i dont have a context, cause the function can be executed by an command and scheduler.
答案1
得分: 1
如果你拥有所有必要的参数,你可以自己构建上下文。
<service id="SwagBasicExample\Command\ExampleCommand">
<argument type="service" id="product.repository"/>
<argument type="service" id="Shopware\Core\Content\ProductStream\Service\ProductStreamBuilder"/>
<tag name="console.command"/>
</service>
$context = new Context(
new SystemSource(),
[$ruleId],
$currencyId,
[$languageId, Defaults::LANGUAGE_SYSTEM]
);
$filters = $this->productStreamBuilder->buildFilters(
$productStreamId,
$context
);
$criteria = new Criteria();
$criteria->addFilter(...$filters);
$products = $this->productRepository->search($criteria, $context);
或者,你可以注入AbstractSalesChannelContextFactory
来构建整个SalesChannelContext
,从而只使用销售渠道的ID来构建Context
。
<argument type="service" id="Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory"/>
// 你可以选择覆盖销售渠道的选项
// 例如,如果一个销售渠道有多个分配的语言
// 并且你想使用除默认语言之外的其他语言
$options = [
SalesChannelContextService::LANGUAGE_ID => $languageId,
];
$salesChannelContext = $this->salesChannelContextFactory->create(
Uuid::randomHex(),
$salesChannelId,
$options
);
$context = $salesChannelContext->getContext();
英文:
You can simply construct the context yourself if you have all the necessary arguments.
<service id="SwagBasicExample\Command\ExampleCommand">
<argument type="service" id="product.repository"/>
<argument type="service" id="Shopware\Core\Content\ProductStream\Service\ProductStreamBuilder"/>
<tag name="console.command"/>
</service>
$context = new Context(
new SystemSource(),
[$ruleId],
$currencyId,
[$languageId, Defaults::LANGUAGE_SYSTEM]
);
$filters = $this->productStreamBuilder->buildFilters(
$productStreamId,
$context
);
$criteria = new Criteria();
$criteria->addFilter(...$filters);
$products = $this->productRepository->search($criteria, $context);
Alternatively you can inject AbstractSalesChannelContextFactory
to build the entire SalesChannelContext
and thus also the Context
from only the id of the sales channel.
<argument type="service" id="Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory"/>
// You can optionally override the options of the sales channel
// e.g. if a sales channel has multiple languages assigned
// and you want to use a language other than the default
$options = [
SalesChannelContextService::LANGUAGE_ID => $languageId,
];
$salesChannelContext = $this->salesChannelContextFactory->create(
Uuid::randomHex(),
$salesChannelId,
$options
);
$context = $salesChannelContext->getContext();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论