Sorting order items by SKU in Woocommerce

后端 未结 2 772
旧时难觅i
旧时难觅i 2020-12-12 03:02

I am trying to order the products by sku within an order in an email in Woocommerce.

I have not had any luck with the following code

2条回答
  •  孤街浪徒
    2020-12-12 03:41

    In addition to @LoicTheAztec I've added the following check in the code:

    if ($product instanceof WC_Product) {
                        $item_skus[$product->get_sku()] = $items_id;
                    }
    

    This prevents an error on the front-end customer orders page.

    add_filter( 'woocommerce_order_get_items', 'filter_order_get_items_by_sku', 10, 3 );
    function filter_order_get_items_by_sku( $items, $order, $types ) {
        if( count($items) > 1 ) {
            $item_skus = $sorted_items = array();
    
            // Loop through order line items
            foreach( $items as $items_id => $item ){
                // Check items type: for versions before Woocommerce 3.3
                if( $item->is_type('line_item') ){
                    $product = $item->get_product(); // Get the product Object
                    
                    if ($product instanceof WC_Product) {
                        $item_skus[$product->get_sku()] = $items_id;
                    }
                }
            }
    
            // Only for line items when our sku array is not empty
            if( ! empty($item_skus) ) {
                // Sorting in ASC order based on SKUs;
                ksort($item_skus); // or use krsort() for DESC order
    
                // Loop through sorted $item_skus array
                foreach( $item_skus as $sku => $item_id ){
                    // Set items in the correct order
                    $sorted_items[$item_id] = $items[$item_id];
                }
                $items = $sorted_items;
            }
        }
        return $items;
    }
    

提交回复
热议问题