3
votes



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,

1
In your second snippet, how do you get $order_id value?Krunal Prajapati
I didn't, I was guessing - wrongly. @Dmitry has solved it !yogi_bear

1 Answers

3
votes

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