我正在寻找一种在加载产品(产品/类别页面)时自动加载产品所有流的方法,但似乎无法找到添加此的方法。我可以将其作为扩展属性添加,但我更愿意使用$product->getStreams()
函数。
我尝试了以下方法,但我不知道如何通过这种方法添加关联:
public static function getSubscribedEvents(): array
{
return [
ProductEvents::PRODUCT_LOADED_EVENT => 'onLoad'
];
}
public function onLoad(EntityLoadedEvent $event): void
{
$product = $event->getEntities();
}
您可以修饰product.repository
,并在调用修饰过的存储库的search
之前更改标准以添加streams
的关联。您可以在这里找到一个示例,其中media.repository
用于在获取数据之前修饰以更改搜索条件。
服务定义:
<service id="MyPluginCoreContentProductProductRepositoryDecorator" decorates="product.repository">
<argument type="service" id="MyPluginCoreContentProductProductRepositoryDecorator.inner"/>
</service>
In the Decorator:
public function search(Criteria $criteria, Context $context): EntitySearchResult
{
$criteria->addAssociation('streams');
return $this->decorated->search($criteria, $context);
}
创建您自己的产品存储库
final class SalesChannelProductRepository implements SalesChannelRepositoryInterface
{
/**
* @var SalesChannelRepositoryInterface
*/
private $repository;
public function __construct(
SalesChannelRepositoryInterface $repository
) {
$this->repository = $repository;
}
public function search(Criteria $criteria, SalesChannelContext $salesChannelContext): EntitySearchResult
{
$this->processCriteria($criteria);
return $this->repository->search($criteria, $salesChannelContext);
}
private function processCriteria(Criteria $criteria): void
{
$criteria->addAssociation('streams');
}
}
定义服务装饰默认产品存储库
<service id="my_example.sales_channel.product.repository"
class="MyExampleCoreContentProductSalesChannelSalesChannelProductRepository"
decorates="sales_channel.product.repository"
public="false">
<argument type="service" id="my_example.sales_channel.product.repository.inner"/>
</service>
感谢回答这个问题的人,我得到了下面的装饰。我仍然遇到一些与在Dynamic Product组规则中使用属性作为过滤器相关的问题。我在这里创建了一个新的线程:从产品获取流返回零结果与一些规则
装饰类:
class ProductRepositoryDecorator implements SalesChannelRepositoryInterface
{
private SalesChannelRepositoryInterface $decorated;
public function __construct(
SalesChannelRepositoryInterface $decorated
) {
$this->decorated = $decorated;
}
public function search(Criteria $criteria, SalesChannelContext $salesChannelContext): EntitySearchResult
{
$this->processCriteria($criteria);
return $this->decorated->search($criteria, $salesChannelContext);
}
private function processCriteria(Criteria $criteria): void
{
$criteria->addAssociation('streams');
}
public function aggregate(Criteria $criteria, SalesChannelContext $salesChannelContext): AggregationResultCollection
{
return $this->decorated->aggregate($criteria, $salesChannelContext);
}
public function searchIds(Criteria $criteria, SalesChannelContext $salesChannelContext): IdSearchResult
{
return $this->decorated->searchIds($criteria, $salesChannelContext);
}
}
: services .
<service id="VendorBaseCoreContentProductProductRepositoryDecorator" decorates="sales_channel.product.repository">
<argument type="service" id="VendorBaseCoreContentProductProductRepositoryDecorator.inner"/>
</service>