最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Different displayed message if it's customer first order in WooCommerce - Stack Overflow

programmeradmin3浏览0评论

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 ); 

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 ); 
Share Improve this question edited Jan 30 at 21:44 LoicTheAztec 254k24 gold badges397 silver badges443 bronze badges asked Jan 29 at 10:54 adem007adem007 674 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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:

  • wc_get_orders and WC_Order_Query WooCommerce documentation,
  • WooCommerce High-Performance Order Storage extension recipe book.
  • WooCommerce PDF Invoices & Packing Slips > PDF template action hooks
发布评论

评论列表(0)

  1. 暂无评论