Last active
July 29, 2020 13:20
-
-
Save Donderda/29378e2168156028c2883154e8b689a4 to your computer and use it in GitHub Desktop.
Load Parent Product on ProductPage in Shopware 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" ?> | |
<container xmlns="http://symfony.com/schema/dic/services" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | |
<services> | |
<service id="MY_Shortdesc\Subscriber\ProductPageSubscriber"> | |
<argument type="service" id="product.repository"/> | |
<tag name="kernel.event_subscriber"/> | |
</service> | |
</services> | |
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
namespace MY_Shortdesc\Subscriber; | |
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection; | |
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface; | |
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; | |
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent; | |
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
class ProductPageSubscriber implements EventSubscriberInterface | |
{ | |
/** | |
* @var EntityRepositoryInterface | |
*/ | |
private $productRepository; | |
public function __construct(EntityRepositoryInterface $productRepository) | |
{ | |
$this->productRepository = $productRepository; | |
} | |
public static function getSubscribedEvents(): array | |
{ | |
return [ | |
ProductPageLoadedEvent::class => "onProductsLoaded" | |
]; | |
} | |
public function onProductsLoaded(ProductPageLoadedEvent $event) | |
{ | |
if (!$event->getPage()->getExtension("parentProduct")) { | |
/** @var EntityCollection $parent */ | |
$parent = $this->productRepository->search( | |
new Criteria([ | |
$event->getPage()->getProduct()->getParentId() | |
]), $event->getContext() | |
)->first(); | |
$event->getPage()->addExtension("parentProduct", $parent); | |
} | |
} | |
} |
public function onProductLoad(ProductLoaderCriteriaEvent $event) { $event->getCriteria()->addAssociation('parent'); }
This gives me this error:
The given alias '`product.parent.translation`' is not unique in FROM and JOIN clause table. The currently registered aliases are: `product`, `product.parent`, `product.translation`, `product.tax`, `product.manufacturer`, `product.cover`, `product.unit`, `product.deliveryTime`, `product.parent.translation`, `product.parent.parent`.
I tried both $event->getCriteria()->addAssociation('product.parent.parent');
and $event->getCriteria()->addAssociation('product.parent');
. I both cases the product's parent keeps null :(
I tried both
$event->getCriteria()->addAssociation('product.parent.parent');
and$event->getCriteria()->addAssociation('product.parent');
. I both cases the product's parent keeps null :(
Same for me.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is another way without adding a extra query and without using an extension:
(untestet)