How can we show in admin line items with parentId somehow in relation with parent products?

Hello everybody!

We’ve implemented a plugin that allows you to add same product to basket and add children(as individual products) to those ‚main‘ products. Everything looks ok in storefront, in order_line_item childrens are saved as separate line items which gets an ‚parentId‘. Our problem is that in admin area and also on documents generation, line items are shown totally random and we do not know which products are ‚seen‘ as childrens and which are main.

Does anyone knows how we can achieve a proper view based on the main/child relation based on parentId? Somehow a grouping by parentId…

Best regards,

Sorin

Hello everyone !

For order documents I’ve found a solution and I’ll post it here maybe will help someone, but for admin order detail view I’m still struggle to get it work right.

Here’s the solution for order documents:

In services.xml:

In DocumentSubscriber.php

 'onDocumentTemplateRendererParameterEvent'
        ];
    }

    /**
     * Handle document generation grouping parent/child line items
     *
     * @param DocumentTemplateRendererParameterEvent $event
     */
    public function onDocumentTemplateRendererParameterEvent(DocumentTemplateRendererParameterEvent $event): void
    {
        //get event parameters
        $parameters = $event->getParameters();

        //get order
        $order = array_key_exists('order', $parameters) ? $parameters['order'] : null;

        if(!is_null($order)) {
            $aLineItems = [];
            $root = new OrderLineItemCollection();

            $orderLineItems = $order->getLineItems()->getElements();
            foreach ($order->getLineItems()->getElements() as $lineItemId => $lineItemEntity){
                if(array_key_exists($lineItemEntity->getParentId(), $orderLineItems)) {
                    $aLineItems[$lineItemEntity->getParentId()][] = $lineItemEntity->getId();
                } else {
                    $aLineItems[$lineItemEntity->getId()][] = $lineItemEntity->getId();
                }

            }

            foreach($aLineItems as $lineItemId => $childIds){
                foreach($childIds as $childId){
                    $root->add($orderLineItems[$childId]);
                }
            }

            $order->setLineItems($root);

        }

    }


}

Best regards,

Sorin

Hello everyone!

I’m trying to get this view work also in admin vue js based on the logic for order documents but I’m getting stuck to vue js code.

I’ve tried to change the order line items in '…\shopware\administration\Resources\app\administration\src\module\sw-order\component\sw-order-line-items-grid\index.js

in 

data() {
        return {
            isLoading: false,
            selectedItems: {},
            newOrderedLineItems: [[]]
        };
    },
computed: {
        
        orderLineItems() {
            this.order.lineItems.forEach((orderLineItem) => {
                if(this.inArray(orderLineItem.parentId, this.order.lineItems.getIds())) {
                    this.newOrderedLineItems[orderLineItem.parentId] = [];
                    this.newOrderedLineItems[orderLineItem.parentId].push(orderLineItem.id);
                } else {
                    this.newOrderedLineItems[orderLineItem.id] = [];
                    this.newOrderedLineItems[orderLineItem.id].push(orderLineItem.id);
                }
            });
            console.log(this.newOrderedLineItems);

            return this.order.lineItems;
        },

The multidimensional array gets populated but only with the last element for childs.

I do not have too much experience with vue js and any help will be apreaciatted.

Best regards,

Sorin

Hi Sorin,

I need to group children Products too.
Can you please share your code.
Services.xml and subscriber or …

Thanks

Thanks for the information. I’ll be sure to keep an eye on this thread. looking for the same issue.

MyAARPMedicare