I need to send unique id along with form data so its possible to retrieve it and save it under this ID in CRM and other systems - once the form is submitted I retrieve the data and use curl to send it to CRM
add_action( 'wpcf7_mail_sent', 'on_cf7_submit' );
function on_cf7_submit( $contact_form ){
$submission = WPCF7_Submission::get_instance();
$id = $submission->get_posted_data('lead-id'); <---- here is the problem
$fullName = $submission->get_posted_data('your-name');
$email = $submission->get_posted_data('your-email');
$phone = $submission->get_posted_data('your-phone');
........
Tried this snippet, but wasn't even able to create the hidden input in the html
add_action( 'wpcf7_init', 'init_form' );
function init_form( $tag ) {
$id = uniqid(rand());
return "<input type='hidden' name='lead-id' value='$id' />";
}
Related
I'm trying to add custom meta to my new order emails. At this moment, custom meta shows up when I use the "Resend new order notification", but it doesn't when a purchase is made through the website.
I suspect this has to do with my code not being able to check the Order Details before sending the email notification. Here's the code I came up with so far..
// Ref: https://www.businessbloomer.com/woocommerce-add-extra-content-order-email/
function custom_order_type_number_email( $order, $sent_to_admin, $plain_text, $email ) {
// Get custom Order Type and Order Number meta keys
$custom_order_type = get_post_meta( $order->get_id(), 'custom_order_type', true );
$custom_order_number = get_post_meta( $order->get_id(), 'custom_order_number', true );
// Add for New Order email notification only
if ( $email->id == 'new_order' ) {
if ( !empty( $custom_order_type ) ) {
echo '<p><b>Order type :</b> ' . $custom_order_type . '</p>';
} else {
echo '<p><b>Order type :</b> Website</p>';
}
if ( !empty( $custom_order_number ) ) {
echo '<p><b>Order no :</b> ' . $custom_order_number . '</p>';
} else {
echo '<p><b>Order no :</b> New</p>';
}
}
}
add_action( 'woocommerce_email_after_order_table', 'custom_order_type_number_email', 20, 4 );
What I'm trying to do ideally is..
When the customer purchases from the website, this new order will be without the custom meta. So the custom meta in the email will show Order type: Website / Order number: New.
And if the admin manually adds a new order with defined custom meta, this new order email notification would get and show the saved custom meta later.
I hope this made sense. (>_<) And thanks in advance for the help.
I want to calculate the inventory down to zero via storing the input of a number field.
So far i have the following:
add_action( 'wpcf7_before_send_mail',
function( $contact_form, $abort, $submission ) {
$post_id = get_the_id();
$voorraad = '';
if ($post_id === 5080){
$voorraad = get_post_meta(5080,'voorraad',true);
$newvoorraad = implode( ', ', (array) $submission->get_posted_data( 'number-435' ));
$stock = $voorraad - $newvoorraad;
update_post_meta($post_id, 'voorraad', $stock);
}
},10,3
);
However, this does not work.
The meta field voorraad exists and has value 450. However, the field is not updated after input. I cannot store the submitted data in the first place, even if i use an empty meta field with a different name and store a set string like 'example', only for testing purposes.
How can i store a user input in a meta field upon submission?
Any ideas?
The get_the_id() function won't retrieve the postID from the form. You have to get that from the $submission->get_meta('container_post_id)
add_action( 'wpcf7_before_send_mail',
function( $contact_form, $abort, $submission ) {
$post_id = $submission->get_meta('container_post_id');
$voorraad = '';
if ($post_id === 5080){
$voorraad = get_post_meta(5080,'voorraad',true);
$newvoorraad = implode( ', ', (array) $submission->get_posted_data( 'number-435' ));
$stock = $voorraad - $newvoorraad;
update_post_meta($post_id, 'voorraad', $stock);
}
},10,3
);
Method #2
Another Method would be to use the contact form ID as the trigger, and just place your post ID in the form.
add_action('wpcf7_before_send_mail',
function ($contact_form) {
if ($contact_form->id() === 123) { // Your Contact Form ID
$submission = WPCF7_Submission::get_instance();
$voorraad = '';
$voorraad = get_post_meta(5080, 'voorraad', true);
$newvoorraad = implode(', ', (array)$submission->get_posted_data('number-435'));
$stock = $voorraad - $newvoorraad;
update_post_meta(5080, 'voorraad', $stock);
}
}
);
I have a user registration form from woocommerce where users can register to create a account. Right now the username generated is first name. Is it possible to use the billing_company field as the username upon a woocommerce registration instead of the first name?
You should use a custom function hooked into woocommerce_new_customer_data filter hook. This code will combine first name and last name as a username. Better than using the billing company
add_filter( 'woocommerce_new_customer_data', 'custom_new_customer_data', 10, 1 );
function custom_new_customer_data( $cust_customer_data ){
// get the first and last billing names
if(isset($_POST['billing_first_name'])) $first_name = $_POST['billing_first_name'];
if(isset($_POST['billing_last_name'])) $last_name = $_POST['billing_last_name'];
// the customer billing complete name
if( ! empty($first_name) || ! empty($last_name) ) {
$user_name = $first_name . ' ' . $last_name;
}
// Replacing 'user_login' in the user data array, before data is inserted
if( ! empty($user_name ) ) {
$cust_customer_data['user_login'] = sanitize_user( str_replace( ' ', '_', $user_name ) );
}
return $cust_customer_data;
}
I've created a meta box. The code is:
// Create your custom meta box
add_action( 'add_meta_boxes', 'hotel_amenities' );
// Add a custom meta box to a post
function hotel_amenities( $post ) {
add_meta_box(
'Meta Box Amenities', // ID, should be a string
'Amenities', // Meta Box Title
'amenities_content', // Your call back function, this is where your form field will go
'post', // The post type you want this to show up on, can be post, page, or custom post type
'normal', // The placement of your meta box, can be normal or side
'high' // The priority in which this will be displayed
);
}
// Content for the custom meta box
function amenities_content( $post ) {
echo '<label>Bed room</label>';
echo '<input type="text" name="amenity_bed_room" value="" />';
}
// Save your meta box content
add_action( 'save_post', 'save_amenities' );
// save newsletter content
function save_amenities(){
global $post;
// Get our form field
if( $_POST ) :
$amenities_meta = esc_attr( $_POST['amenity_bed_room'] );
// Update post meta
update_post_meta($post->ID, '_amenities_custom_meta', $amenities_meta);
endif;
}
It shows a meta box on admin post page with a text field. but it gets blank if I save or update the post after I put some thing on the text field.
Seems function save_amenities() is not working. What I am doing wrong in this code?
Also for getting that value I use the function below. Is that correct?
//get amenities meta box values
function get_amenities_meta_box() {
global $post;
$meta_values = get_post_meta($post->ID, '_amenities_custom_meta', true);
}
There are a few things going wrong there. The final value that you want to see will be displayed by the value attribute in the amenities_content function. Right now it is just displaying an empty string (""). Try putting any value in that attribute and you should see it show up in the meta box (value="this is a test").
The save_amenities function should take $post_id as a parameter. You'll need that to update the post meta-data and give a real value for the amenities_content function to echo back to the admin screen.
The amenities_content function should really have a nonce field that should then be verified by the save_amenities function. And user input should be sanitized before it is saved (I'm doing it both when I save it and when I display it. I'm not sure if that's necessary.)
try this out for the amenities_content function:
function amenities_content( $post ) {
// This is the value that was saved in the save_amenities function
$bed_room = get_post_meta( $post->ID, '_amenity_bed_room', true );
wp_nonce_field( 'save_amenity', 'amenity_nonce' );
echo '<label>Bed room</label>';
echo '<input type="text" name="amenity_bed_room"
value="' . sanitize_text_field( $bed_room ) . '" />';
}
and this for the save_amenities function:
function save_amenities( $post_id ) {
// Check if nonce is set
if ( ! isset( $_POST['amenity_nonce'] ) ) {
return $post_id;
}
if ( ! wp_verify_nonce( $_POST['amenity_nonce'], 'save_amenity' ) ) {
return $post_id;
}
// Check that the logged in user has permission to edit this post
if ( ! current_user_can( 'edit_post' ) ) {
return $post_id;
}
$bed_room = sanitize_text_field( $_POST['amenity_bed_room'] );
update_post_meta( $post_id, '_amenity_bed_room', $bed_room );
}
I'm using User Front End Pro to provide a front end registration form. One line of that is for an address, and I want to use that to display a map of all users with Geo Mashup.
I have a custom field that saves to user meta called geocode_address, I need to then geocode it and output it to two separate fields Latitude and Longitude. I found this for geocoding custom post data, and have tried to amend it for user meta, but it is not working, and causes the registration to hang. What have I done wrong?
function geocode_address($user_id)
{
$custom_fields = get_user_meta();
if(isset($custom_fields["geocode_address"]) && !empty($custom_fields["geocode_address"][0]))
{
$resp = wp_remote_get( "http://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($custom_fields["geocode_address"][0])."&sensor=false" );
if ( 200 == $resp['response']['code'] ) {
$body = $resp['body'];
$data = json_decode($body);
if($data->status=="OK"){
$latitude = $data->results[0]->geometry->location->lat;
$longitude = $data->results[0]->geometry->location->lng;
update_user_meta($user_id, "latitude", $latitude);
update_user_meta($user_id, "longitude", $longitude);
}
}
}
}
add_action('update_user_meta', 'geocode_address');
As a side note, Geomashup has a function to geocode a custom field, but when I enter "geocode address" it doesn't seem to work...
The action update_user_meta takes more arguments and the first one is not the User ID:
add_action('update_user_meta', 'geocode_address', 10, 4 );
function geocode_address( $meta_id, $user_id, $meta_key, $meta_value )
{
if( 'geocode_address' == $meta_key )
{
var_dump( $meta_value );
die();
}
}
Then, you're using get_user_meta() wrong, we need to pass at least the User ID: get_user_meta($user_id, $key = '', $single = false);. But I suspect it's not needed in your case.
As for the Geocode API, it returned a correct response when I tested with a random spanish address:
$address = "gran via, madrid";
$resp = wp_remote_get( "http://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($address)."&sensor=false" );