4



I am trying to create some shortcodes related to woocommerce order data.

I have a custom page where a customer is redirected to on order completed. The guest checkout is disabled, so all customers that purchase will have an account. In the page I want to insert some data - via shortcode - from the order. Here’s an example:

“Hi [custom-woocommerce-name], thank you for your purchase. We have received your payment of [custom-woocommerce-total] via [custom-woocommerce-payment]. An email has been sent to [custom-woocommerce-email], blah blah blah. Your order #[custom-woocommerce-orderid], has been packed blah blah blah."

So what I am looking for is to access the following data:

$order->get_billing_first_name();
$order->get_total();
$order->get_payment_method();
$order->get_billing_email();
$order->get_id();


I have a working php snippet that creates a shortcode for the wordpress user name:

add_shortcode( ‘custom-wordpress-name' , ‘custom_user_name' );
function custom_user_name(){
    $user = wp_get_current_user();
    return $user->user_firstname;
}

Which I have tried to tweak, but my php understanding is very limited and it creates an error.

add_shortcode( ‘custom-woocommerce-name' , ‘custom_first_name' );
function custom_first_name(){
   $order = wc_get_order( $order_id );
   return $order->get_billing_first_name();
}

Where am I going wrong ?

Thanks,

yogi_bear
  • 73
  • 2
  • 7

1 Answers1

4

You can try this way:

add_shortcode( 'custom-woocommerce-name' , 'custom_first_name' );
function custom_first_name(){
    $customer_id = get_current_user_id();
    $order = wc_get_customer_last_order( $customer_id );
    return $order->get_billing_first_name();
}
Dmitry Leiko
  • 3,970
  • 3
  • 25
  • 42
  • 1
    Great ! Works like a charm, I've used the code to collect the other data as well. Just replacing `$order->get_billing_first_name();` in your code with any of the other “$order” objects. – yogi_bear Feb 04 '20 at 11:43