在 Shopware 中加载产品的所有变体 (PHP)
Load all variants of a product in Shopware (PHP)
我正在尝试加载产品的所有变体。
在使用 google 时,我发现了这个代码片段:
class ProductViewSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $productRepository;
public function __construct(EntityRepositoryInterface $productRepository)
{
$this->productRepository = $productRepository;
}
public function getVariantsOfProduct(SalesChannelProductEntity $product, Context $context): EntitySearchResult
{
$parentProduct = $product->getParent();
if ($parentProduct != null) {
// We need the parent product to find all variants
$product = $parentProduct;
}
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('product.parentId',
$product->getUniqueIdentifier()));
return $this->productRepository->search($criteria, $context);
}
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => ['addVariants'],
];
}
public function addVariants(ProductPageLoadedEvent $pEvent)
{
$products = $this->getVariantsOfProduct($product = $pEvent->getPage()->getProduct(), $pEvent->getContext());
$matchingVariants = $products->getElements();
}
}
但是使用代码片段只返回一个空列表,尽管该产品肯定有变体。我该如何修复此代码?
我认为下面一行是错误的:
$criteria->addFilter(new EqualsFilter('product.parentId', $product->getUniqueIdentifier()));
应该是:
$criteria->addFilter(new EqualsFilter('parentId', $product->getId()));
因为您已经使用 productRepository 进行搜索,所以 属性 只是 parentId
.
它应该像这样工作。
<?php
namespace TestPlugin\Subscriber;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductPageLoadedSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $productRepository;
public function __construct(EntityRepositoryInterface $productRepository)
{
$this->productRepository = $productRepository;
}
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => 'addVariants'
];
}
public function addVariants(ProductPageLoadedEvent $pEvent)
{
$product = $pEvent->getPage()->getProduct();
if (empty($productParentId = $product->getParentId())) { // Main product or has no variants
return;
}
$products = $this->getVariantsOfProduct($productParentId, $pEvent->getContext());
$matchingVariants = $products->getElements();
var_dump($matchingVariants);
}
public function getVariantsOfProduct(string $productParentId, Context $context): EntitySearchResult
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('parentId', $productParentId));
return $this->productRepository->search($criteria, $context);
}
}
据我所知,Shopware 总是加载一个变体,因此检查 ParentId 很重要,它提供了主要产品 ID,您可以使用此 ID 找出所有变体。
我正在尝试加载产品的所有变体。
在使用 google 时,我发现了这个代码片段:
class ProductViewSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $productRepository;
public function __construct(EntityRepositoryInterface $productRepository)
{
$this->productRepository = $productRepository;
}
public function getVariantsOfProduct(SalesChannelProductEntity $product, Context $context): EntitySearchResult
{
$parentProduct = $product->getParent();
if ($parentProduct != null) {
// We need the parent product to find all variants
$product = $parentProduct;
}
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('product.parentId',
$product->getUniqueIdentifier()));
return $this->productRepository->search($criteria, $context);
}
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => ['addVariants'],
];
}
public function addVariants(ProductPageLoadedEvent $pEvent)
{
$products = $this->getVariantsOfProduct($product = $pEvent->getPage()->getProduct(), $pEvent->getContext());
$matchingVariants = $products->getElements();
}
}
但是使用代码片段只返回一个空列表,尽管该产品肯定有变体。我该如何修复此代码?
我认为下面一行是错误的:
$criteria->addFilter(new EqualsFilter('product.parentId', $product->getUniqueIdentifier()));
应该是:
$criteria->addFilter(new EqualsFilter('parentId', $product->getId()));
因为您已经使用 productRepository 进行搜索,所以 属性 只是 parentId
.
它应该像这样工作。
<?php
namespace TestPlugin\Subscriber;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductPageLoadedSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $productRepository;
public function __construct(EntityRepositoryInterface $productRepository)
{
$this->productRepository = $productRepository;
}
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => 'addVariants'
];
}
public function addVariants(ProductPageLoadedEvent $pEvent)
{
$product = $pEvent->getPage()->getProduct();
if (empty($productParentId = $product->getParentId())) { // Main product or has no variants
return;
}
$products = $this->getVariantsOfProduct($productParentId, $pEvent->getContext());
$matchingVariants = $products->getElements();
var_dump($matchingVariants);
}
public function getVariantsOfProduct(string $productParentId, Context $context): EntitySearchResult
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('parentId', $productParentId));
return $this->productRepository->search($criteria, $context);
}
}
据我所知,Shopware 总是加载一个变体,因此检查 ParentId 很重要,它提供了主要产品 ID,您可以使用此 ID 找出所有变体。