Title: Get Custom Input Field&#8217;s value via PHP
Last modified: August 29, 2018

---

# Get Custom Input Field’s value via PHP

 *  [inibori](https://wordpress.org/support/users/inibori/)
 * (@inibori)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/get-custom-input-fields-value-via-php/)
 * Hello,
 * I want to get whatever the customer typed into a custom input field via PHP.
 * The meta key for the value is “_wcj_product_input_fields_local_1″, but I cant
   get it via WC_Order…
 * Kind of like this shortcode does [https://booster.io/shortcodes/wcj_order_items_meta/](https://booster.io/shortcodes/wcj_order_items_meta/)
   [
   wcj_order_items_meta meta_key=”_wcj_product_input_fields_global_1”] but in PHP…
 * Does anyone know how to get it via PHP ?
 * Kind regards,
    Robin
    -  This topic was modified 7 years, 9 months ago by [inibori](https://wordpress.org/support/users/inibori/).
    -  This topic was modified 7 years, 9 months ago by [inibori](https://wordpress.org/support/users/inibori/).

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [Algoritmika](https://wordpress.org/support/users/algoritmika/)
 * (@algoritmika)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/get-custom-input-fields-value-via-php/#post-10640720)
 * Hi [@inibori](https://wordpress.org/support/users/inibori/),
 * You can try just using `do_shortcode()` [function](https://developer.wordpress.org/reference/functions/do_shortcode/),
   e.g. for current order:
 *     ```
       echo do_shortcode( '[wcj_order_items_meta meta_key="_wcj_product_input_fields_global_1"]' );
       ```
   
 * or by order ID:
 *     ```
       echo do_shortcode( '[wcj_order_items_meta order_id="123" meta_key="_wcj_product_input_fields_global_1"]' );
       ```
   
 * For “pure” PHP, please try something like this:
 *     ```
       $order = wc_get_order(); // or wc_get_order( $order_id );
       foreach ( $order->get_items() as $item_key => $item ) { 
           print_r( wc_get_order_item_meta( $item_key, '_wcj_product_input_fields_global_1' ) ); 
       }
       ```
   
 * Hope that helps.
 * P.S. If you like the plugin, please consider [leaving us a rating](https://wordpress.org/support/plugin/woocommerce-jetpack/reviews/?rate=5#new-post).
    -  This reply was modified 7 years, 9 months ago by [Algoritmika](https://wordpress.org/support/users/algoritmika/).
 *  Thread Starter [inibori](https://wordpress.org/support/users/inibori/)
 * (@inibori)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/get-custom-input-fields-value-via-php/#post-10640992)
 * This is my code:
 *     ```
       add_action( 'woocommerce_payment_complete', 'api_add_order');
           function api_add_order($order_id){
   
           	$order = new WC_Order($order_id);
   
           	$order_username = do_shortcode( '[wcj_order_items_meta order_id="' . $order_id . '" meta_key="_wcj_product_input_fields_global_1"]' );
   
   
               $fp = fopen('vardump.txt', 'w');
               fwrite($fp, serialize($order_username));
               fclose($fp);
   
   
           	$items = $order->get_items();
   
           	foreach ( $items as $item ) {	
   
           	$product_id = $item['product_id'];
                   $product = new WC_Product($item['product_id']);
   
                   if ( has_term( 'api', 'product_cat', $product_id ) ) {
   
                       $sku        = explode("-", $product->get_sku());
           	       	$service	= $sku[0];
                   	$username	= $order_username;
                       $quantity	= $sku[1];
   
                   }
               }
           }
       ```
   
 * I feel like I have tried everything but it always returns nothing, I can access
   everything else like the sku and first or last name… but i cant seem to get the
   order meta like the input fields input.
 * Is it possible that it gets added to the order meta after “woocommerce_payment_complete”?
    -  This reply was modified 7 years, 9 months ago by [inibori](https://wordpress.org/support/users/inibori/).
    -  This reply was modified 7 years, 9 months ago by [inibori](https://wordpress.org/support/users/inibori/).
 *  [Algoritmika](https://wordpress.org/support/users/algoritmika/)
 * (@algoritmika)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/get-custom-input-fields-value-via-php/#post-10653416)
 * Hi [@inibori](https://wordpress.org/support/users/inibori/),
 * Booster’s product input fields’ data is added to items meta on `woocommerce_add_order_item_meta`
   action if you have WooCommerce version below 3.0.0 and on `woocommerce_new_order_item`
   action in newer WooCommerce versions. Not really sure if `woocommerce_payment_complete`
   is fired before or after that…
 * Did you try my second suggestion:
 *     ```
       $order = wc_get_order(); // or wc_get_order( $order_id );
       foreach ( $order->get_items() as $item_key => $item ) { 
           print_r( wc_get_order_item_meta( $item_key, '_wcj_product_input_fields_global_1' ) ); 
       }
       ```
   
 * Also (not sure if it will help though), please try calling `$order->save();` 
   before trying to access items meta.
 * By the way – not sure if this is a typo – but product input fields’ data is stored
   as order **items** meta (i.e. not as order meta). And from your code, I can see
   that you are getting sku from the product itself (i.e. not from the order item).
 * Hope that helps.
 *  [nasty1333](https://wordpress.org/support/users/nasty1333/)
 * (@nasty1333)
 * [7 years, 6 months ago](https://wordpress.org/support/topic/get-custom-input-fields-value-via-php/#post-10892951)
 * Any solution for this, i have the same problem. The suggested solutions dont 
   work for me. I need to print the item meta field but always get empty value.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Get Custom Input Field’s value via PHP’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce-jetpack/assets/icon-256x256.png?rev=3351194)
 * [Booster for WooCommerce – PDF Invoices, Abandoned Cart, Variation Swatches & 100+ Tools](https://wordpress.org/plugins/woocommerce-jetpack/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce-jetpack/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce-jetpack/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce-jetpack/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce-jetpack/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce-jetpack/reviews/)

## Tags

 * [get](https://wordpress.org/support/topic-tag/get/)
 * [input](https://wordpress.org/support/topic-tag/input/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * 4 replies
 * 3 participants
 * Last reply from: [nasty1333](https://wordpress.org/support/users/nasty1333/)
 * Last activity: [7 years, 6 months ago](https://wordpress.org/support/topic/get-custom-input-fields-value-via-php/#post-10892951)
 * Status: not resolved