Skip Navigation

[Resolved] Store custom field value in $variable and return it

This support ticket is created 7 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 1 reply, has 2 voices.

Last updated by Christian Cox 7 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#556850

Hey 🙂

So I am trying to basically store a custom fields value into a variable.. This is the scenario:

1. In the registration form, there is a "company name" custom field. This field also exists in the woocommerce account page/checkout.

2. I am using the following code to pre-populate woo fields with the values entered in the my-account page:

add_filter('woocommerce_checkout_get_value', function($input, $key ) {
	global $current_user;
	switch ($key) :
		case 'billing_first_name':
		case 'shipping_first_name':
			return $current_user->first_name;
		break;

               case 'billing_company':
			return $company;
		break;
	endswitch;
}, 10, 2);

It wont't take shortcodes for anything and this is what I have tried to store it in a variable (using the guidance of this thread https://toolset.com/forums/topic/combine-custom-fields-into-single-variable/):

add_filter('save_post', 'combine_my_fields');
function combine_my_fields($post_id) {
$company = get_post_meta($post_id, 'wpcf-firmenname', true);
update_post_meta($post_id, 'full-company', $company);
}

3. I am not sure even how to check if it is indeed saved as a variable but all I got so far was the company field saying "Array" on checkout so either the saving of the variable is wrong or the way I am trying to return it.

Any ideas would be highly appreciated 🙂

#556994

Okay so you're saying that the company field is a custom field on each User's profile, correct? And you would like to be able to access that information to populate a field in your woo_commerce_checkout_get_value function.

 return $company;

Unless you have defined $company as a global variable, this won't work. PHP isn't designed to automatically share variables across functions like this. I don't think you need a variable here, though, instead you can access the information directly using the types_render_usermeta function. If your user custom field has the slug "company" then you can try this code to display the logged-in user's company:

types_render_usermeta( "company", array( "user_current" => true ) );

We have more information and examples here:
https://toolset.com/documentation/customizing-sites-using-php/functions/#textfield
Click the orange +More button for more examples.

add_filter('save_post', 'combine_my_fields');
function combine_my_fields($post_id) {
$company = get_post_meta($post_id, 'wpcf-firmenname', true);
update_post_meta($post_id, 'full-company', $company);
}

I don't think this code is necessary. It looks like it's just taking the value of one custom field and saving it in another custom field. If the intention was just to save a variable, then this can be deleted altogether. If you need the company name stored in this other field for some reason, then you can keep it.


I am not sure even how to check if it is indeed saved as a variable but all I got so far was the company field saying "Array" on checkout so either the saving of the variable is wrong or the way I am trying to return it.

I don't think you need to save a variable as I've mentioned above, but in the future you can use PHP's error_log function to write out information while code is executing. Go in your wp-config.php file and look for define(‘WP_DEBUG’, false);. Change it to:

define('WP_DEBUG', true);

Then add these lines, just before it says 'stop editing here':

ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');

Then, you can use the error_log function to write to that error log file.

error_log($variable);
error_log(print_r($array, true));

After you're finished you can turn logging off by reverting the changes in your wp-config.php file.