I have an ACF repeater field field_5f71d4eaaf381 for email addresses and I want to output a comma separate list of the sub field values (measurement_extra_email_address) to create a $to for the WordPress mail function.
$values = $_POST['acf'];
$extra_recipients = $_POST['acf']['field_5f71d4eaaf381'];
if ( $extra_recipients ) {
$list = array();
foreach( $extra_recipients as $extra_recipient ) {
$list[] = $extra_recipient['measurement_extra_email_address'];
}
$to = implode(',', $list);
}
I thought above code should/would do it but no email is being send so I guess the code is incorrect..?
Just a note: When I remove above part of the code and just try $to = 'my#emailadress.com'; an e-mail is being send so the error should be in there...
Here's the code I was looking for:
$values = $_POST['acf'];
$measurement_mail_extra_recipients = $_POST['acf']['field_5f71d4eaaf381'];
if ( $measurement_mail_extra_recipients ) {
$list = array();
foreach( $measurement_mail_extra_recipients as $measurement_mail_extra_recipient ) {
$list[] = $measurement_mail_extra_recipient['field_5f71d55aaf382', $measurement_client];
}
$to = implode(',', $list);
}
Related
I am trying to get country name from country drop down, where I have used pipe symbol. like this in my form
[text* FullName id:fullname]
[select* country id:country include_blank "United States | a#gmail.com" "Canada | a#gmail.com" "Mexico | b#gmail.com" "United Kingdom | c#gmail.com"]
I have used different-2 email with country name because i need to sand email to company employee according to country name. In email template i have used [country] to get email and [_raw_country] to get country name that is working fine.
I am trying to get country name in function.php file to save it in database by this way.
function contactform7_before_send_mail( $form_to_DB ) {
//set your db details
$mydb = new wpdb('user','password','databasename','localhost');
$form_to_DB = WPCF7_Submission::get_instance();
if ( $form_to_DB )
$formData = $form_to_DB->get_posted_data();
$fullname = $formData['FullName'];
$country = $formData['country'];
$mydb->insert( 'mytable', array( 'fullname' =>$fullname,'country' =>$country), array( '%s' ) );
}
But it gives nothing. if i try to get like this
$country = $formData['country'][0];
it gives me only email id like a#gmail.com I have tryed $country = $formData['country'][1];
It give me nothing. I have also tryed $country = $formData['_raw_country']; But not get luck.
This is the reference document
https://contactform7.com/selectable-recipient-with-pipes/
you need something like this:
<?php
// Get the submitted data
$submission = WPCF7_Submission::get_instance();
if ( ! $submission
or ! $posted_data = $submission->get_posted_data() ) {
return;
}
// Get the contact form additional data
$contact_form = $submission->get_contact_form();
// get the tag used in the form
$mail_tags=$contact_form->scan_form_tags();
// search into tags for country
$key = array_search('country', array_column($mail_tags, 'name'));
// get the country pipes values
$pipes = new WPCF7_Pipes( $mail_tags[$key]->raw_values );
// get an array with the pipes
$pipes_array = $pipes->to_array();
// get the choosen value
$pipes_val = array_search( $posted_data['country'][0], array_column($pipes_array, 1));
// finally!
error_log(print_r($posted_data['country'],true));
error_log(print_r("in",true));
error_log(print_r($pipes_array[$pipes_val][0],true));
I posted this answer in your other question. This should work.
add_action( 'wpcf7_before_send_mail', 'cf7_pipes_so_67686211');
function cf7_pipes_so_67686211( $contact_form ) {
global $wpdb;
$mydb = new wpdb('user','password','database','localhost');
// Get Form Tags
$tags = $contact_form->scan_form_tags();
$pipe_array = array();
// Find the Values of the Pipes
foreach ($tags as $tag){
if ($tag->name === 'primarybusiness'){
$pipe_array = $tag->pipes->to_array();
}
}
// Get the form data
$form_to_DB = WPCF7_Submission::get_instance();
if ( $form_to_DB ) {
$formData = $form_to_DB->get_posted_data();
$fullname = $formData['FullName'];
foreach ($pipe_array as $value) {
// Compare and find the pipe value
if ( $posted_data['primarybusiness'][0] === $value[1] ){
$primarybusiness = $value[0];
}
}
$mydb->insert( 'mytable', array( 'fullname'=>$fullname,'primarybusiness'=>$primarybusiness), array( '%s' ) );
}
}
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);
}
}
);
PHP newb here, looking for some guidance. I am working with BuddyPress and Advanced Custom Fields (ACF). I have an ACF field 'new_user' with a value of true/false. I am trying to filter my BuddyPress Members Loop to only display users with a value of 'new_user' = true.
There are 2 code samples here.
The standard BP Members Loop. My thought here, is how do I first query my users by ACF ‘new_user’ = true and then start the bp member loop?:
if ( bp_has_members() ) :
// some code goes here
endif;
while ( bp_members() ) : bp_the_member();
//OUTPUT MEMBERS LIST HERE
endwhile;
This is a BP function to filter by Buddypress extended user fields. The idea here I believe is to replace the code in the middle specific to xprofile_get_field with the proper ACF code:
function my_custom_ids( $field_name, $field_value = '' ) {
if ( empty( $field_name ) )
return '';
global $wpdb;
$field_id = xprofile_get_field_id_from_name( $field_name );
if ( !empty( $field_id ) )
$query = "SELECT user_id FROM " . $wpdb->prefix . "bp_xprofile_data WHERE field_id = " . $field_id;
else
return '';
if ( $field_value != '' )
$query .= " AND value LIKE '%" . $field_value . "%'";
/*
LIKE is slow. If you're sure the value has not been serialized, you can do this:
$query .= " AND value = '" . $field_value . "'";
*/
$custom_ids = $wpdb->get_col( $query );
if ( !empty( $custom_ids ) ) {
// convert the array to a csv string
$custom_ids_str = 'include=' . implode(",", $custom_ids);
return $custom_ids_str;
}
else
return '';
}
Of course, I am open to solving this in another way as well. I hope this is clear.
I use specific thank you pages depend of product
I want to display the last purchase (product's name) of the current user on this page and fetch cross sell of the specific product too.
Any help would be apreciate
OF course i made some research and found tutorial like this one
https://www.skyverge.com/blog/get-all-woocommerce-orders-for-a-customer/#comment-703884
But i am not enaugh good at php to modify it for my needs
Thank you
If you want to display 50 last purchased product for example, go to function.php in Appearance>Editor>function.php then past the code below:
// Recently Sold Products shortcode for WooCommerce
add_shortcode('sold_products', 'sold_products');
function sold_products($attributes) {
global $wpdb, $woocommerce;
$defaults = array('max_products' => 50, 'title' => "Last purchased");
$parameters = shortcode_atts( $defaults, $attributes);
$max = absint($parameters['max_products']); // number of products to show
$title = sanitize_text_field($parameters['title']);
$html = '<div class="sold_products">'.PHP_EOL;
if ($title) {
$html .= '<h3>Last purchased:</h3>'.PHP_EOL;
}
$table = $wpdb->prefix.'woocommerce_order_items';
$my_query = $wpdb->prepare("SELECT * FROM $table WHERE `order_item_type`='line_item' ORDER BY `order_id` DESC LIMIT %d", $max);
$nr_rows = $wpdb->query($my_query);
if (!$nr_rows) {
$html .= '<p>Aucun produit n\'est acheté encore!</p>';
} else {
$html .= '<ul>'.PHP_EOL;
for ($offset = 0; $offset < $nr_rows; $offset++) {
if ( is_user_logged_in() ) {
$user_info = wp_get_current_user();
}
$row = $wpdb->get_row($my_query, OBJECT, $offset);
$product_name = $row->order_item_name;
$product = get_page_by_title($product_name, OBJECT, 'product');
$url = get_permalink($product->ID);
$order_id = $row->order_id;
$order = new WC_Order($order_id);
$user = $order->get_user();
if ($user) {
$user_id = $user->ID;
} else {
$user_id = 'Guest';
}
if($user_info->ID == $user_id){
$unix_date = strtotime($order->order_date);
$date = date('d/m/y', $unix_date);
$html .= '<li>'.$product_name.' acheté le '.$date.'</li>'.PHP_EOL;
}
}
$html .= '</ul>'.PHP_EOL;
}
$html .= '</div>'.PHP_EOL;
return $html;
} // end function
you can dislay this result anywhere in your pages just past this shotcode where you want it to be displayed:
[sold_products max_products="50" title="Last purchased"]
Let me tell you the scenario first say the structure of the categories in wordpress is like this
Level 1: Top
Level 2: -Nextme_1
Level 3: --Nextme_2
--Nextme_3
Level 4: ---Nextme_4
---Nextme_5
Now I require to check what is the level of the category? Say I catch a category of level 3 so I have to use different template and if its level 4. Then I need to use another template?
Anybody can give me some hint?
Thanks
Rahul
If you don't have many categories you can try to edit their slug from admin, and then in your page you get the category slug this way:
if (is_category()) {
$cat = get_query_var('cat');
$category = get_category($cat);
echo 'your slug is '. $category->slug;
}
Now, when you're editing the categories slugs try naming them after their level: cat-lvl-1, cat-lvl-2. Then in your page you extract the number from category slug using some php string function, and then you check that number:
if ($category->slug == 1 ) {
//load the template for the category of level 1
}
if ($category->slug == 2 ) {
//load the template for the category of level 2
}
and so on.
Later edit:
Try this:
function get_level($category, $level = 0)
{
if ($category->category_parent == 0) {
return $level;
} else {
$level++;
$category = get_category($category->category_parent);
get_level($category, $level);
}
}
if (is_category()) {
$cat = get_query_var('cat');
$yourcat = get_category($cat);
echo get_level($yourcat);
}
You can call the get_ancestors() function to get an array containing the parents of the given object. Then you need to count elements in the result.
function get_the_level($id, $type = 'category') {
return count( get_ancestors($id, $type) );
}
if( is_category() ) {
$level = get_the_level( $cat );
}
elseif( is_product_category() ) {
$level = get_the_level( $wp_query->get_queried_object()->term_id, 'product_cat' );
}
Thanks a lot. This is superb with slight a change the code that you have written is fine but its not returning any value.(i,e the $level) although its calculating correct. A minor change i did and its work fine now with a slight editing of you code given below..
`
function get_level($category, $level = 0)
{
if ($category->category_parent == 0) {
return $level;
} else {
$level++;
$category = get_category($category->category_parent);
return get_level($category, $level);
}
}
if (is_category()) {
$cat = get_query_var('cat');
$yourcat = get_category($cat);
echo get_level($yourcat);
}
`
Thanks #zuzuleinen
I visited this page months back. I came back today, arrow up on the above solution then still went digging. Although it is a good solution, Wordpress often offers better or close.
get_category_parents()
This function does as Rahul has typed basically. It also calls itself which seems the most logical approach and that is why Rahul gets a point from me on this. Do not use $link, return a string of categories, explode() them then count or I suppose we could count the number of times the separator has been used and add 1.
function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
$chain = '';
$parent = get_term( $id, 'category' );
if ( is_wp_error( $parent ) )
return $parent;
if ( $nicename )
$name = $parent->slug;
else
$name = $parent->name;
if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
$visited[] = $parent->parent;
$chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited );
}
if ( $link )
$chain .= ''.$name.'' . $separator;
else
$chain .= $name.$separator;
return $chain;
}