Skip to content

Sale_lib::sort_and_filter_cart() crashes when cart has no printable items #4685

Closed

Description

@objecttothis

Bug

Sale_lib::sort_and_filter_cart() (app/Libraries/Sale_lib.php:190) crashes with a TypeError when the cart, after filtering to items with print_option == PRINT_YES, ends up empty.

TypeError: array_multisort(): Argument #1 ($array) must be an array or a sort flag
app/Libraries/Sale_lib.php:214

Root cause

When line_sequence config is '0' (Entry sequence):

if ($this->config['line_sequence'] == '0') {
    $sort = [];
    foreach ($filtered_cart as $k => $v) {
        $sort['line'][$k] = $v['line'];
    }
    array_multisort($sort['line'], SORT_ASC, $filtered_cart);
}

If $filtered_cart is empty (e.g. every cart line has print_option != PRINT_YES), the foreach loop never runs, so $sort['line'] is never set — array_multisort() then receives null instead of an array, causing the crash. The same shape of bug likely applies to the other line_sequence branches in this function ($sort['stock_type'], etc.) for the same empty-$filtered_cart case.

Repro

Complete a sale (invoice or quote mode) where the entire cart has print_option values other than PRINT_YES (0, from app/Config/Constants.php:123). sort_and_filter_cart() is called from postComplete() in app/Controllers/Sales.php before rendering the invoice/quote/work-order view, so this can crash a real checkout.

Suggested fix

Guard the array_multisort() calls (or the whole if/elseif chain) for the case where $filtered_cart is empty, e.g. return early with return $filtered_cart; right after the filtering loop if it's empty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions