Skip to content

Instantly share code, notes, and snippets.

@Donderda
Last active July 29, 2020 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Donderda/29378e2168156028c2883154e8b689a4 to your computer and use it in GitHub Desktop.
Save Donderda/29378e2168156028c2883154e8b689a4 to your computer and use it in GitHub Desktop.
Load Parent Product on ProductPage in Shopware 6
<?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>
<?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);
}
}
}
@leptoquark1
Copy link

There is another way without adding a extra query and without using an extension:

    public static function getSubscribedEvents(): array
    {
        return [
            ProductLoaderCriteriaEvent::class => "onProductLoad"
        ];
    }

    public function onProductLoad(ProductLoaderCriteriaEvent $event)
    {
        $event->getCriteria()->addAssociation('parent');
    }

(untestet)

@Donderda
Copy link
Author

    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 :(

@samsauter
Copy link

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