I’m trying to build this page for my Website:
I need to add Products ParentId into LineItems and use twig to seperate the products with same parent id,
I’m doing that because LineItems with variation does’nt have anything in common. see the Image:
What i have done to make this page since:
1.i’ve make a Service and i’m trying to add products ParentId to LineItems
2. use $lineitems->setPayloadValue(„myVar“, $ParentId); to add ParentId into LineItems
Here is my Code:
use HashContext;
use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\Context;
class AddDataToPage implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [BeforeLineItemAddedEvent::class => 'onLineItemAdded'];
}
/**
* @param onLineItemAdded $event
* @throws \Shopware\Core\Checkout\Cart\Exception\InvalidPayloadException
*/
public function onLineItemAdded(BeforeLineItemAddedEvent $event)
{
$mylineitems = $event->getCart()->getLineItems();
##
#
##// Get parent entities
#$criteria = new Criteria($entities);
# $parents = $this->productRepository->search($criteria, $ProductEntity);
# echo "<pre>";
# var_dump($parents);
#echo "</pre>";
#// Assign parents to variants
# foreach ($entities as $entity) {
# if ($entity->getParentId() && $parents->has($entity->getParentId())) {
# $entity->setParent($parents->get($entity->getParentId()));
# }
#}
##
##
##
$lineitems = $event->getLineItem();
$lineitems->setPayloadValue("myVar", $ParentId);
}
}
I have also tried to get LineItems Parentid using Criteria & ProductEntity but unfortunately it didn’t worked.
Does anyone has an idea how to do that?
Please share some code.
THANKS