skip to Main Content

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.

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

2

Answers


    1. If you add only one variable to the array, it would be more correct to use

    $array[] =

    instead of

    array_push();

    E.g.

    $array[] = $var

    1. Since menu_options is used only to get post descriptions, you can output this part to the post_description variable

    2. If the post does not have menu_options, the description of the post will be an empty line, you need to consider whether this is a required field

    This should work

    private function convertCartToOrderLines($order)
    {
        $lines = [];
        foreach ($order->getOrderMenusWithOptions() as $orderItem)
        {
            $order_description = '';
            $order_name = $orderItem->name;
            $unit_without_decimal = number_format($orderItem->subtotal, 2, '.', '') * 100;
            
            foreach ($orderItem->menu_options as $itemOptionGroup ) {
                $itemOptionGroup = $orderItem->menu_options->groupBy('order_option_category');
                foreach ($itemOptionGroup as $itemOptionGroupName => $itemOptions) {
                    foreach ($itemOptions as $itemOption) {
                        $order_description = "Qty: x".$orderItem->quantity." | ".$itemOption->order_option_name;
                    }
                }
            }
            
            $new_order = [
                'price_data' => [
                    'currency' => currency()->getUserCurrency(),
                    'unit_amount_decimal' => $unit_without_decimal,
                    'product_data' => [
                        'name' => $order_name,
                        'description' => $order_description,
                    ],
                ],
                'quantity' => 1,
            ];
    
            $lines[] = $new_order;
        }
        return $lines;
    }
    
    Login or Signup to reply.
  1. You’ll need to intercept your empty order items and structure the entry as you see fit for the result array. You could write continue at the end of the if block, but the foreach() on the menu_options won’t be entered anyhow.

    I’ve also move unchanging variable declarations up and out of loops to remove unnecessary iterative work for PHP ($currency and $subtotal).

    This script should successfully populate the result array with empty order items and individual order option items.

    private function convertCartToOrderLines(object $order): array
    {
        $currency = currency()->getUserCurrency();
        $lines = [];
        foreach ($order->getOrderMenusWithOptions() as $orderItem) {
            $subtotal = number_format($orderItem->subtotal, 2, '.', '') * 100;
            if (!$orderItem->menu_options) {
                $lines[] = [
                    'price_data' => [
                        'currency' => $currency,
                        'unit_amount_decimal' => $subtitle,
                        'product_data' => [],
                    ],
                    'quantity' => 0,
                ];
            }
            foreach ($orderItem->menu_options as $itemOptionGroup ) {
                $itemOptionGroup = $orderItem->menu_options->groupBy('order_option_category');
                foreach ($itemOptionGroup as $itemOptions) {
                    foreach ($itemOptions as $itemOption) {
                        $lines[] = [
                            'price_data' => [
                                'currency' => $currency,
                                'unit_amount_decimal' => $subtotal,
                                'product_data' => [
                                    'name' => $orderItem->name,
                                    'description' => sprintf(
                                        "Qty: x%d | %s",
                                        $orderItem->quantity,
                                        $itemOption->order_option_name
                                    )
                                ],
                            ],
                            'quantity' => 1,
                        ];
                    }
                }
            }
        }
    
        return $lines;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search