WooCommerce Checkout add-on plugin: Get the a custom field value

594 views Asked by At

With WooCommerce I use the plugin WooCommerce Checkout add-on that add extra field to the checkout page.

I add a field named 'livraison' and it's saved id DB, but I can't get it with the code :

get_post_meta( $order->id, 'livraison', true );

I don't understand why is false...

What I am doing wrong?

Thank you

1

There are 1 answers

0
LoicTheAztec On BEST ANSWER

To solve your issue:

1) Checking in your database within wp_postmeta table.
You should make a search sticking this SQL search in SQL query window:

SELECT * FROM `wp_postmeta` WHERE `meta_key` LIKE '%livraison%'

Because is possible that the meta_key slug of this custom field is for example '_livraison'.

If you get nothing, you should check with another search for a defined order ID (for example below we search for order ID 1216):

SELECT * FROM `wp_postmeta` WHERE `post_id` = 1216

Then you will get all the meta data for this order and you will search visually for this custom field.


2) Making sure that you get an Order ID using **$order->id with get_post_meta() function.**

Depending where you are using get_post_meta() you need to be sure that $order->id isn't null.

You should try to output before in your code $order->id value for example with:

echo 'Current Order ID is ' . $order->id;

If you don't get any order Id, here is the problem. In some WooCommerce hooks you can use for example $order_id instead, depending on the arguments of the hook used. Same thing for some templates.

It could be useful, to get better helped, you provide the code or some extra information concerning where you are trying to use get_post_meta( $order->id, 'livraison', true );