0

I am creating an array for an order to then pass this data to stripe, the array has multiple forEach clauses.

The issue I am facing is, if the order item(s) does not have menu_options it is not included in the array.

What am I doing wrong to ensure that each $orderItem is in the array wether it does or does not have an $itemOption / menu_options.

@foreach ($order->getOrderMenusWithOptions() as $orderItem)

<div class="cart-items pt-2">
    <ul>
        @foreach ($order->getOrderMenusWithOptions() as $orderItem)
            <li>
                <span class="price pull-right">{{ currency_format($orderItem->subtotal) }}</span>
                <span class="name fw-bold">
                    @if ($orderItem->quantity > 1)
                        <span class="quantity">
                            {{ $orderItem->quantity }} @lang('igniter.cart::default.text_times')
                        </span>
                    @endif
                    {{ $orderItem->name }}
                </span>
                @php $itemOptionGroup = $orderItem->menu_options->groupBy('order_option_category') @endphp
                @if ($itemOptionGroup->isNotEmpty())
                    <ul class="list-unstyled small">
                        @foreach ($itemOptionGroup as $itemOptionGroupName => $itemOptions)
                            <li>
                                <u class="text-muted">{{ $itemOptionGroupName }}:</u>
                                <ul class="list-unstyled">
                                    @foreach ($itemOptions as $itemOption)
                                        <li>
                                            @if ($itemOption->quantity > 1)
                                                {{ $itemOption->quantity }} @lang('igniter.cart::default.text_times')
                                            @endif
                                            {{ $itemOption->order_option_name }}&nbsp;
                                            @if ($itemOption->order_option_price > 0)
                                                ({{ currency_format($itemOption->quantity * $itemOption->order_option_price) }})
                                            @endif
                                        </li>
                                    @endforeach
                                </ul>
                            </li>
                        @endforeach
                    </ul>
                @endif
                @if (!empty($orderItem->comment))
                    <p class="comment text-muted small">
                        {!! $orderItem->comment !!}
                    </p>
                @endif
            </li>
        @endforeach
    </ul>
</div>
private function convertCartToOrderLines($order)
{
    $lines = [];
    foreach ($order->getOrderMenusWithOptions() as $orderItem) {
        foreach ($orderItem->menu_options as $itemOptionGroup ) {
            $itemOptionGroup = $orderItem->menu_options->groupBy('order_option_category');
            foreach ($itemOptionGroup as $itemOptionGroupName => $itemOptions) {
                foreach ($itemOptions as $itemOption) {
                    $test1 = "Qty: x".$orderItem->quantity." | ".$itemOption->order_option_name;              
                    array_push($lines, [
                        'price_data' => [
                            'currency' => currency()->getUserCurrency(),
                            'unit_amount_decimal' => number_format($orderItem->subtotal, 2, '.', '') * 100,
                            'product_data' => [
                                'name' => $orderItem->name,
                                'description' => $test1,
                            ],
                        ],
                        'quantity' => 1,
                    ]);
                }
            }
        }
    }

    return $lines;
}

the following code works and shows all cart/orderItems with and without options, but I am still needing to add/include the itemOption->order_option_name

    private function convertCartToOrderLines(object $order): array
{
    $currency = currency()->getUserCurrency();
    $lines = [];
    foreach ($order->getOrderMenusWithOptions() as $orderItem) {
        $subtotal = number_format($orderItem->subtotal, 2, '.', '') * 100;

            $lines[] = [
                'price_data' => [
                    'currency' => $currency,
                    'unit_amount_decimal' => $subtotal,
                    'product_data' => [
                        'name' => $orderItem->name,
                        'description' => sprintf(
                                "Qty: x%d",
                                $orderItem->quantity,
                        )
                        ],
                ],
                'quantity' => 1,
            ];
        
 
    }

    return $lines;
}
3
  • What is $order->getOrderMenusWithOptions()? Does it return what you expect it to return?
    – IGP
    Commented Jul 10 at 0:13
  • What data should be be pushed into $lines when $orderItem has no $itemOptions? Are you expecting an empty subarray for 'product_data'? Should quantity be set to 0 in this case? Why does currency()->getUserCurrency() need to be called in the 4th level of nested loops? Commented Jul 10 at 0:29
  • @mickmackusa product_data should have orderItem in the array, some orderItem have itemOptions so I expect all orderItems and the description to show itemOptions when it has one don't worry about quantity Commented Jul 10 at 11:10

0

Browse other questions tagged or ask your own question.