Thank you! Your feedback has been delivered
Thank you! Your feedback has been sent

Populate fields with values from Woocommerce in Contact Form 7

I'm having a problem with the Wordpress-plugin "Contact Form 7 Dynamic Text Extension"? I'm trying to take the fields containing user info from Woocommerce and populate the contact form. The fields I need for the form are the following:

$address = apply_filters( 'woocommerce_my_account_my_address_formatted_address', array(
'first_name' => get_user_meta( $customer_id, $name . '_first_name', true ),
'last_name' => get_user_meta( $customer_id, $name . '_last_name', true ),
'company' => get_user_meta( $customer_id, $name . '_company', true ),
'address_1' => get_user_meta( $customer_id, $name . '_address_1', true ),
'address_2' => get_user_meta( $customer_id, $name . '_address_2', true ),
'city' => get_user_meta( $customer_id, $name . '_city', true ),
'state' => get_user_meta( $customer_id, $name . '_state', true ),
'postcode' => get_user_meta( $customer_id, $name . '_postcode', true ),
'country' => get_user_meta( $customer_id, $name . '_country', true )
), $customer_id, $name );

How ever I can't figure out how to write that into a short code I can use in "Contact Form 7".

I'm looking for help to write the short codes needed to populate the field.

Thanks in advance.

User Gravatar

TroelsLL

Posted May 1 2014 11:58 UTC

$30


  • Assigned To tripflex
  • Solved
  • wordpress
  • 3864 Views

9 Replies


It would be something like the code below. You have to create a shortcode and then use it just like that dynamic text extension plugin works...

function CF7_WOO( $atts ){
    extract(shortcode_atts(array(
        'meta' => 0,
    ), $atts, 'CF7_WOO'));
    return get_user_meta( $customer_id, $name . $meta, true );
}
add_shortcode('CF7_WOO', 'CF7_WOO');

With the shortcode being similar to below depending on the meta field you want

CF7_WOO meta='_first_name'

What is you're reason for doing this? If you can give a little bit more explanation as to what you're trying to accomplish that would make it easier to help you out.

Reason I say that is the code you provided above is from the my-address.php file which is inside of a for loop, and the code I provided may not work depending on what you're trying to do, and where you're going to display the form at.

User Gravatar

tripflex

Posted May 1 2014 12:51 UTC

I've tried to add the shortcode both as a MU-plugin and add it to the php in the template used on the page containing the form, but it don't seems to work. Before using it in the form i've tried to add it to the page as a regular shortcode using:

[CF7_WOO meta='_first_name']

But nothing shows up. Do you have any suggestions why that might be?

User Gravatar

TroelsLL

Posted May 1 2014 13:38 UTC

You still didn't answer the question as to what your ultimate goal is with this ... I need to know so I can tell you the best way to do this

And nothing is showing up because the $customer_id and $name variables are not defined

User Gravatar

tripflex

Posted May 1 2014 13:44 UTC

Sorry, I didn't see the last part of you post.

I have a form which only logged in users can see and submit. The form has a number of fields for the user to fill in, but at the end of the form I want the fields to be populated with the information from the user profile. The form is added to the same page as the shipping and billing address from Woocommerce (the my-address.php)

I've used Woocommerce for the user administration (I'm not sure that's the cleverest way to do it, but since Woocommerce has the additional user fields I need, I decided to use that). It's those field I want to add to the bottom of the form.

Since I only need the my-address.php file used for this specific purpose, I've added the contact form to this file via the shortcode from Contact Form 7 <?php echo do_shortcode('[contact-form-7 id="254" title="contact"]'); ?>

I hope that's a better explanation.

User Gravatar

TroelsLL

Posted May 1 2014 13:57 UTC

I am assuming your $name variable above is something you could extract from user data (or from db).

You'll have to add this to your functions.php

function my_get_current_user_meta($atts){
    extract(shortcode_atts(array('key' => 'first_name'), $atts));

    //here you get your $name variable appropriately, assume it's in user meta table somewhere
    $name = get_user_meta($current_user->ID, '__the_name_key', true);

    global $current_user; get_currentuserinfo();

    $val = get_user_meta( $current_user->ID , $name . '_' . $key, true ); return $val;
}

add_shortcode('my_get_current_user_meta', 'my_get_current_user_meta');

Now that you have a shortcode to get current user meta prepended by $name_, you can add this in CF7 form field. Example to get the user's last name:

[dynamictext dynamicname "my_get_current_user_meta key='last_name'"]
User Gravatar

adrian7

Posted May 1 2014 13:58 UTC

I think there is still a problem with the $name variable, as I can't get the shortcode to show the result (neither in or outside the form). The first mention in the my-address.php of $name is this:

<?php foreach ( $get_addresses as $name => $title ) : ?>

Is the $name still not defined the right way?

Two general questions: (Newbie-question) Can I add to the function.php right away without risking that the file is overwritten in the next update. Usually it's advised that changes are only made to the child-theme, but that doesn't seems to apply to the function-php. Second: Is it ok to post an url to the site in question?

User Gravatar

TroelsLL

Posted May 1 2014 14:25 UTC

Hi TroelsLL,

We highly encourage you to post a link to the actual site. The whole purpose of CodersClan is to get you the exact code you need, and there's no better way to do it than seeing your program live.

User Gravatar

CodersClan

Posted May 1 2014 14:31 UTC

I've got it, just stick this in your functions file and it should work:

function CF7_WOO( $atts ){
    $customer_id = get_current_user_id();
    $get_addresses    = apply_filters( 'woocommerce_my_account_get_addresses', array(
        'billing' =>  __( 'Billing Address', 'woocommerce' )
    ), $customer_id );
    $name = key($get_addresses);
    extract(shortcode_atts(array(
        'meta' => '_first_name',
    ), $atts, 'CF7_WOO'));
    return get_user_meta( $customer_id, $saved_name . $meta, true );
}
add_shortcode('CF7_WOO', 'CF7_WOO');

but now that i look at it, looks like the filter is already querying the billing address. The reason is because there can be multiple addresses, so really the code could just be:

function CF7_WOO( $atts ){
    $customer_id = get_current_user_id();
    extract(shortcode_atts(array(
        'meta' => '_first_name',
    ), $atts, 'CF7_WOO'));
    return get_user_meta( $customer_id, 'billing' . $meta, true );
}
add_shortcode('CF7_WOO', 'CF7_WOO');

Will you have multiple addresses or only one address? Reason I ask is the meta values are stored like billing_first_name or billing2_first_name if there is a second billing address. So you want to make sure you're getting the meta for the right address.

User Gravatar

tripflex

Posted May 1 2014 14:38 UTC

Solution

This didn't solve your task? Get your own custom solution.

The last solution works perfect. Thank you so much both of you.

I'm able to decide whether I want the shipping or billing address, just by changing billing to shipping.

Right now I've added a short code for first name, last name, company and so forth and that works, but maybe I'll see if I can make the short code get each variable, so that I only have to change the key value in the form (to reduce the number of short codes.

Once again: Thank you!

User Gravatar

TroelsLL

Posted May 2 2014 8:53 UTC

Add a reply

By posting a reply on CodersClan you agree to our Terms & Conditions