I have a problem that I can't really solve. I'm trying to call up a complete article in the storefront and include it in the product block.
I include the block like this::
{% include '@Storefront/storefront/component/product/card/box.html.twig' with {'product': product} %}
Unfortunately, not all article information is received in this way (like image or the price), so the product block looks like this:
I call up the product via the product.repository
service which looks like this in detail:
namespace RcCarPartScout\Service;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Psr\Container\ContainerInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
class AddDataToPage implements EventSubscriberInterface
{
private SystemConfigService $systemConfigService;
private EntityRepository $productRepository;
public function __construct(
SystemConfigService $systemConfigService,
ContainerInterface $container,
EntityRepository $productRepository)
{
$this->systemConfigService = $systemConfigService;
$this->productRepository = $productRepository;
}
public static function getSubscribedEvents(): array
{
return[
StorefrontRenderEvent::class => 'onStorefrontRender'
];
}
/**
* @param StorefrontRenderEvent $event
*/
public function onStorefrontRender(StorefrontRenderEvent $event): void
{
$event->setParameter('product', $this->getProduct($event->getContext(), $event->getRequest()->get('productnumber')));
}
/**
* @param Context $context
* @param $id
*/
private function getProduct(Context $context, $id)
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('productNumber', $id));
$product = $this->productRepository->search($criteria, $context);
$product = $product->first();
return $product;
}```
There is a difference between products loaded for the admin api usage (over the "normal" repository) and products loaded for storefront representation (loaded over the sales_channel.product.repository
)
using the sales channel product repository will automatically add the cover media association as well as the prices and also calculate the cheapest prices etc.
So for your use case you should switch to using the sales_channel.product.repository
with the type \Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository
.