簡體   English   中英

是否可以啟用產品關聯的自動加載?

[英]Is it possible to enable the automatic loading of an association for products?

我正在尋找一種在加載產品(產品/類別頁面)時自動從產品加載所有流的方法,但似乎找不到添加它的方法。 我可以將它添加為擴展屬性,但我更願意使用$product->getStreams()函數。

我嘗試了以下方法,但不知道如何通過此方法添加關聯:

public static function getSubscribedEvents(): array
{
    return [
        ProductEvents::PRODUCT_LOADED_EVENT => 'onLoad'
    ];
}

public function onLoad(EntityLoadedEvent $event): void
{
    $product = $event->getEntities();
}

您可以在調用裝飾存儲庫的search之前裝飾product.repository並更改條件以添加streams的關聯。 您可以在此處找到一個示例,其中media.repository曾經被修飾以在獲取數據之前更改搜索條件。

服務定義:

<service id="MyPlugin\Core\Content\Product\ProductRepositoryDecorator" decorates="product.repository">
       <argument type="service" id="MyPlugin\Core\Content\Product\ProductRepositoryDecorator.inner"/>
</service>

在裝飾器中:

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="MyExample\Core\Content\Product\SalesChannel\SalesChannelProductRepository"
                 decorates="sales_channel.product.repository"
                 public="false">
            <argument type="service" id="my_example.sales_channel.product.repository.inner"/>           
        </service>

感謝那些對這個問題做出反應的人,我已經得到了下面的裝飾。 我仍然遇到一些與在動態產品組規則中使用屬性作為過濾器相關的問題。 我在這里為此創建了一個新線程: Getting streams from product returns zero results with some rules

裝飾類:

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);
    }
}

服務.xml:

<service id="Vendor\Base\Core\Content\Product\ProductRepositoryDecorator" decorates="sales_channel.product.repository">
        <argument type="service" id="Vendor\Base\Core\Content\Product\ProductRepositoryDecorator.inner"/>
</service>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM