• Resolved nickmaxedon

    (@nickmaxedon)


    Is there a way to sort the products on the packingslip “by category” instead of “the order of the customer putting in the cart”?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor kluver

    (@kluver)

    Hi @nickmaxedon,

    To achieve this you can add the following code snippet:

    // Sort items alphabetically by category
    add_filter( 'wpo_wcpdf_order_items_data', 'wpo_wcpdf_sort_items_by_category', 10, 3 );
    function wpo_wcpdf_sort_items_by_category ( $items, $order, $document_type ) {
    	if( $document_type == 'packing-slip' ) {
    		usort($items, 'wpo_wcpdf_sort_by_category');
    	}
    	return $items;
    }
    
    function wpo_wcpdf_sort_by_category($a, $b) {
    	$categories_a = strip_tags( wc_get_product_category_list( $a['product_id'] ) );
    	$categories_b = strip_tags( wc_get_product_category_list( $b['product_id'] ) );
    	if ( $categories_a == $categories_b ) return 0;
    	return ( $categories_a < $categories_b ) ? -1 : 1;
    }

    This code snippet should be placed in the functions.php of your child theme. If you haven’t worked with code snippets or functions.php before please read this: How to use filters

    Thread Starter nickmaxedon

    (@nickmaxedon)

    Thanks for the quick reply! I tried your code, but I than I got a white page. I upgraded the php version from 7.1 to 7.2 and now the website seem to work, but the order is still in the order of placing it in the cart.

    Thread Starter nickmaxedon

    (@nickmaxedon)

    Or do I need the premiumversion of the plugin in order for this to work?

    Plugin Contributor kluver

    (@kluver)

    Hi @nickmaxedon,

    That is strange. No you do not need the premium extension for this to work. On my local test environment this works without a problem. Are you sure the filter is executed?

    If you’ve added this to your child theme are you sure you also activated this child theme?

    Thread Starter nickmaxedon

    (@nickmaxedon)

    Hey there,

    I needed to make another order in order for this to work. Its seem to work perfect thanks! Do you have the code for doing the same thing but on the invoice. Do you also have the code to display the products on alphabetical order on both invoice as packingslip?

    Kind regards,

    Nick

    Plugin Contributor Ewout

    (@pomegranate)

    Hello Nick,
    To apply the sorting to all documents you can simply remove the packing-slip conditional from the snippet, changing:

    
    	if( $document_type == 'packing-slip' ) {
    		usort($items, 'wpo_wcpdf_sort_by_category');
    	}
    

    to just:

    
    	usort($items, 'wpo_wcpdf_sort_by_category');
    

    The entire resulting snippet being:

    
    // Sort items alphabetically by category
    add_filter( 'wpo_wcpdf_order_items_data', 'wpo_wcpdf_sort_items_by_category', 10, 3 );
    function wpo_wcpdf_sort_items_by_category ( $items, $order, $document_type ) {
    	usort($items, 'wpo_wcpdf_sort_by_category');
    	return $items;
    }
    
    function wpo_wcpdf_sort_by_category($a, $b) {
    	$categories_a = strip_tags( wc_get_product_category_list( $a['product_id'] ) );
    	$categories_b = strip_tags( wc_get_product_category_list( $b['product_id'] ) );
    	if ( $categories_a == $categories_b ) return 0;
    	return ( $categories_a < $categories_b ) ? -1 : 1;
    }
    
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Sort the products on the packingslip by category’ is closed to new replies.