lunarphp / lunar

An open-source package that brings the power of modern headless e-commerce functionality to Laravel.
https://lunarphp.io
MIT License
2.71k stars 357 forks source link

Error matching cart lines to order lines in MapDiscountBreakdown #1954

Closed maurice-ellis closed 2 months ago

maurice-ellis commented 2 months ago

Expected Behaviour:

To be able to create an order with no issue

Actual Behaviour:

When creating an order, I experience an "Undefined array key 2" error

Steps To Reproduce:

Add 2 of the same products without merging and create the order

It seems that the mapping to order lines can't handle when there is 2 of the same product variants in the cart

foreach ($order->lines as $orderLine) {
    $cartLine = $cart->lines->first(function ($cartLine) use ($orderLine) {
        return $cartLine->purchasable_type == $orderLine->purchasable_type &&
            $cartLine->purchasable_id == $orderLine->purchasable_id;
    });

    if ($cartLine) {
        $cartLinesMappedToOrderLines[$cartLine->id] = $orderLine;
    }
}

When it iterates through the 2nd line, the $cartline that is returned is the first one and then it overrides the same key in $cartLinesMappedToOrderLines .

This thing causes an issue when trying to reference the line later in the discountBreakdown and the key is undefined

'lines' => $discount->lines->map(function ($discountLine) use ($cartLinesMappedToOrderLines) {
    return (object) [
        'quantity' => $discountLine->quantity,
        'line' => $cartLinesMappedToOrderLines[$discountLine->line->id],
    ];
}),
maurice-ellis commented 2 months ago

Fixed with #1956