如何使用服务将产品 ParentId 添加到 LineItems

How to add Product ParentId into LineItems using a Service

我正在尝试为我的网站构建此页面:

我需要将 Products ParentId 添加到 LineItems 中并使用 twig 来分隔具有相同父 ID 的产品, 我这样做是因为具有变体的 LineItems 没有任何共同点。看图:

从那以后我为制作此页面所做的工作: 1.i 已创建服务,我正在尝试将产品 ParentId 添加到 LineItems 2. 使用 $lineitems->setPayloadValue(„myVar“, $ParentId);将 ParentId 添加到 LineItems

这是我的代码:

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);
  }

 }

我也曾尝试使用 Criteria & ProductEntity 获取 LineItems Parentid,但不幸的是它没有用。

有没有人知道如何做到这一点? 谢谢

我也试过这个:

use Shopware\Core\Checkout\Cart\LineItem\CartDataCollection;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\Checkout\Cart\CartProcessorInterface;

class AddDataToPage implements CartProcessorInterface
{
public function process(CartDataCollection $data, Cart $original, Cart $toCalculate, SalesChannelContext $context, CartBehavior $behavior):void
{
    foreach ($lineItems as $lineItem) {
        /** @var SalesChannelProductEntity $product */
        $product = $data->get('product' . $lineItem->getReferencedId()); 
        $lineItem->setPayloadValue('myVar', $product->getParentId());   
    }

}
}

在我的 Services.xml 中:

<service id="***\Service\AddDataToPage" public="true">
        <tag name="shopware.cart.processor" priority="5100"/>
</service>

我认为最好的选择是为此使用 Shopware Cart Processor。在 process 方法中,您可以按项目循环并将任何数据添加到 payload

foreach ($lineItems as $lineItem) {
        /** @var SalesChannelProductEntity $product */
        $product = $data->get('product-' . $lineItem->getReferencedId()); 
        $lineItem->setPayloadValue('myVar', $product->getParentId());   
    }

我尝试了下面的代码,它运行良好:

    /**
    * @var EntityRepositoryInterface
    */
    private $productRepository;


    public function __construct(EntityRepositoryInterface $productRepository)
    {
    $this->productRepository = $productRepository;
    }

    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();
    $badlineitem = $event->getLineItem();
    $lineitems = $event->getLineItem();



    foreach ($mylineitems as $actualones) {
        
        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('id', $actualones->getId()));
        $criteria->setLimit(2);
        $products = $this->productRepository->search($criteria, $event->getContext()); 
        
        foreach ($products as $prost) {
            if($prost->getParentId() !== null) {
                $lineitems->setPayloadValue("parentId", $prost->getParentId());
            }          
        }
    }