phpwoocommerceordershighperformance

Different displayed message if it's customer first order in WooCommerce


I've got some code working which shows a message at the top of the packing slips if we have a new customer.

However, it's showing the message for all orders, so I've figured my code isn't getting the right info from WooCommerce to confirm if the customer has previously placed an order.

We're using HPOS too.

function action_wpo_wcpdf_after_document_label( $wpo_wcpdf_export_template_type, $order ) { 

  if ('packing-slip' != $wpo_wcpdf_export_template_type) { return; }

  $billing_email = $order->get_billing_email();

  if (!$billing_email) {

    return;
  }

  $customer_orders = get_posts( array(
    'numberposts' => -1,
    'exclude'     => array($order->get_id()),
    'meta_key'    => '_billing_email',
    'meta_value'  => $billing_email,
    'post_type'   => wc_get_order_types(),
    'post_status' => array_keys( wc_get_order_statuses() ),
  ) );
      
  if (empty($customer_orders)) { print "\n" . '<p class="form-field form-field-wide" style="font-weight: bold;margin-top: -60px;border: 1px solid;width: 90px;padding: 3px 10px 5px 10px;line-height: 1;text-align: center;">New Customer</p>' . "\n"; return; }

  else { print "\n" . '<p class="form-field form-field-wide" style="font-weight: bold;margin-top: -60px;border: 1px solid;width: 90px;padding: 3px 10px 5px 10px;line-height: 1;text-align: center;">Existing Customer</p>' . "\n"; return; }

  return;
} 
         
add_action( 'wpo_wcpdf_after_document_label', 'action_wpo_wcpdf_after_document_label', 10, 2 ); 

Solution

  • Reminder: With High-Performance Order Storage enabled, you need to replace all WordPress Post and Postmeta functions with WooCommerce CRUD methods (or functions).

    So you should replace WP get_posts() with compatible wc_get_orders() function, like:

    function action_wpo_wcpdf_after_document_label( $document_type, $order ) { 
        if ( 'packing-slip' !== $document_type ) return; // Target only Packing slip
        
        // Get customer ID or billing email
        $customer = $order->get_user_id() ?: $order->get_billing_email();
      
        if ( ! $customer ) return; // Exit if no customer ID or billing email
    
        // Get the previous customer payed order ID (array)
        $previous_order_id = wc_get_orders( array(
            'type'        => 'shop_order',
            'status'      => wc_get_is_paid_statuses(),
            'customer'    => $customer,
            'exclude'     => array( $order->get_id() ),
            'limit'       => 1, 
            'return'      => 'ids'
        ) );
            
        printf( "\n".'<p class="form-field form-field-wide" style="font-weight:bold;margin-top:-60px;border:1px solid;width:90px;padding:3px 10px 5px 10px;line-height:1;text-align:center;">%s</p>'."\n",
            $previous_order_id ? esc_html__('Existing Customer') : esc_html__('New Customer') 
        );
    }
    
    add_action( 'wpo_wcpdf_after_document_label', 'action_wpo_wcpdf_after_document_label', 10, 2 ); 
    

    It should work now.

    References: