• 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/
    [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.
    • This topic was modified 7 years, 9 months ago by inibori.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi @inibori,

    You can try just using do_shortcode() function, 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.

    • This reply was modified 7 years, 9 months ago by Algoritmika.
    Thread Starter inibori

    (@inibori)

    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.
    • This reply was modified 7 years, 9 months ago by inibori.

    Hi @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.

    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.