• Resolved Jared_Wills

    (@jared_wills)


    Hello!

    I saw this thread but didn’t want to add to it…

    I’m looking to sort the order items on the packing list by a custom field. The custom field was added to the General Product Tab like so:

    // Display Custom Product Fields For Aisle
    add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
    function woo_add_custom_general_fields() {
    
      	global $woocommerce, $post;
    
      	echo '<div class="options_group">';
    
      	// Custom fields will be created here...
      	// Select
    	woocommerce_wp_select(
    	array(
    		'id'      => '_select',
    		'label'   => __( 'Choose the aisle this product in on', 'woocommerce' ),
    		'options' => array(
    			'1'   => __( 'Aisle 1', 'woocommerce' ),
    			'2'   => __( 'Aisle 2', 'woocommerce' ),
    			'3' => __( 'Aisle 3', 'woocommerce' ),
    			'4' => __( 'Aisle 4', 'woocommerce' ),
    			'5' => __( 'Aisle 5', 'woocommerce' ),
    			'6' => __( 'Aisle 6', 'woocommerce' ),
    			'7' => __( 'Aisle 7', 'woocommerce' ),
    			'8' => __( 'Aisle 8', 'woocommerce' ),
    			'9' => __( 'Aisle 9', 'woocommerce' ),
    			'10' => __( 'Aisle 10', 'woocommerce' ),
    			'11' => __( 'Aisle 11', 'woocommerce' ),
    			'12' => __( 'Aisle 12', 'woocommerce' ),
    			'13' => __( 'Aisle 13', 'woocommerce' ),
    			'14' => __( 'Aisle 14', 'woocommerce' ),
    			'15' => __( 'Aisle 15', 'woocommerce' ),
    			'16' => __( 'Aisle 16', 'woocommerce' ),
    			)
    		)
    	);
    
      	echo '</div>';
    
    }

    The custom field is
    _select

    This custom field is saved and I’ve been able to get it to show up in Email Orders and the PDF template.

    However, this did not work.
    <?php $wpo_wcpdf->custom_field('_select', 'Custom field:'); ?>
    (Not sure why?)

    I had to use this code inside the Packing Slip template instead:

    <?php
    $product_id = $item['product_id'];
    echo 'Located on Aisle ';
    echo get_post_meta( $product_id, '_select', true );
    ?>

    On this thread I saw where you provided some code for sorting by SKU

    add_filter( 'wpo_wcpdf_order_items_data', 'wpo_wcpdf_sort_items_by_sku', 10, 2 );
    function wpo_wcpdf_sort_items_by_sku ( $items, $order ) {
    	usort($items, 'wpo_wcpdf_sort_by_sku');
    	return $items;
    }
    
    function wpo_wcpdf_sort_by_sku($a, $b) {
    	if ($a['sku']==$b['sku']) return 0;
    	return ($a['sku']<$b['sku'])?-1:1;
    }

    This does work perfectly! It even works for quantity if you swap it out…

    I thought maybe it would work for the custom field _select, but it does not.

    Do you know if the code you originally provided can work with a field like I described?

    Any help would be greatly appreciated it, even if you can point me in the right direction.

    https://wordpress.org/plugins/woocommerce-pdf-invoices-packing-slips/

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

    (@pomegranate)

    Hello Jared,
    First of all, the code

    <?php $wpo_wcpdf->custom_field('_select', 'Custom field:'); ?>

    Doesn’t work because this function is targeting custom fields in the order (what you want is stored in the product).

    To be able to sort by the custom field, you first need to make it part of the $items array.

    I think the following code should do that (untested):

    add_filter( 'wpo_wcpdf_order_items_data', 'wpo_wcpdf_sort_items_by_aisle', 10, 2 );
    function wpo_wcpdf_sort_items_by_aisle ( $items, $order ) {
    	foreach ($items as $item_id => $item) {
    		$items['aisle'] = get_post_meta( $item['product_id'], '_select', true );
    	}
    	usort($items, 'wpo_wcpdf_sort_by_aisle');
    	return $items;
    }
    
    function wpo_wcpdf_sort_by_aisle($a, $b) {
    	if ($a['aisle']==$b['aisle']) return 0;
    	return ($a['aisle']<$b['aisle'])?-1:1;
    }

    A positive side-effect of this is that you can now pull the aisle with $item[‘aisle’] in your template.

    Hope that helps!

    Ewout

    Thread Starter Jared_Wills

    (@jared_wills)

    Hey Ewout!

    Thanks for the reply and help! I added the above code to my functions.php and tried to add this to my Packing Slip template:

    <?php if( !empty( $item['aisle'] ) ) : ?><dt class="sku"><?php _e( 'Located on Aisle', 'wpo_wcpdf' ); ?></dt><dd class="sku"><?php echo $item['aisle']; ?></dd><?php endif; ?>

    When I view the Packing Slip, this is what I see:
    http://i.imgur.com/O9Uff8Q.jpg

    The bottom item “1 1” with the SKU and Weight is not an actual product. It’s being added on all Packing Slip PDFs and Invoice PDFs for all the orders I’ve tested. The “1 1” sometimes changes to “2 2”.

    Looks like <?php echo $item['aisle']; ?> is only showing up on the last item that’s being inserted.

    I still have this in the template, which is why it’s still showing on the rest of the items:

    <?php
    $product_id = $item['product_id'];
    echo 'Located on Aisle ';
    echo get_post_meta( $product_id, '_select', true );
    ?>

    Any thoughts on why the last item is being added?

    Plugin Contributor Ewout

    (@pomegranate)

    I see there was an error in the code, I think if you change this, it should be solved:

    $items['aisle'] = get_post_meta( $item['product_id'], '_select', true );

    to

    $items[$item_id]['aisle'] = get_post_meta( $item['product_id'], '_select', true );

    Thread Starter Jared_Wills

    (@jared_wills)

    Sorry for the delay in replying, Ewout. That worked perfectly!

    I can’t thank you enough for taking the time to help on this. Greatly appreciated!

    Plugin Contributor Ewout

    (@pomegranate)

    You’re welcome. Would you like to leave me a review? Thanks!

    Thread Starter Jared_Wills

    (@jared_wills)

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Sort Order Items By Custom Field’ is closed to new replies.