I have a Wordpress site where each user is linked to an 'upline' user, with that upline user's ID stored in a custom meta field 'direct_upline'.
On a particular gravity form, I currently populate a hidden field with value of {user:direct_upline}.
For example, User A fills out the form. User A's direct_upline is User B. I would like to then populate User B's direct_upline, and so on (probable 10 levels deep).
Any idea how to most efficiently perform this? Below is a function to populate multiple fields in one function; I was thinking I may could do this in conjunction with $all_meta_for_user = get_user_meta( $user_id );
add_filter( 'gform_field_value', 'populate_fields', 10, 3 );
function populate_fields( $value, $field, $name ) {
$values = array(
'field_one' => 'value one',
'field_two' => 'value two',
'field_three' => 'value three',
);
return isset( $values[ $name ] ) ? $values[ $name ] : $value;
}
Related
I would like to add a note to the new order email notifications that the admin gets. This note should indicate whether the order is from a new customer, or from a returning customer.
This so that the people in the warehouse know what they should add in the shippingbox. We give new customers a ticket and don't want customers who order again to receive the same ticket, they get a diffrent ticket.
This should apply to both logged in users and users who are not logged in. A check via email address perhaps?
This code is using the has_bought for all customer orders - source
function has_bought() {
// Get all customer orders
$customer_orders = get_posts( array(
'numberposts' => 1, // one order is enough
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => 'shop_order', // WC orders post type
'post_status' => 'wc-completed', // Only orders with "completed" status
'fields' => 'ids', // Return Ids "completed"
) );
// return "true" when customer has already at least one order (false if not)
return count($customer_orders) > 0 ? true : false;
}
But I don't get it on the the new order email that admins receive, I would like to show it as:
Customer: new OR returning
Number of purchases: 4
How would i go about this and is this possible to create?
Some comments/suggestions:
To display text in email notifications, you can use different hooks, depending on where exactly you want to display the text. For example, the woocommerce_email_order_details hook is very suitable for this
To target a specific email notification you can use $email->id
Via wc_get_orders() you can get and count the existing orders for a customer based on the email address. If desired, pass a series of arguments that define the criteria for the search
So you get:
function action_woocommerce_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
// Target specific email notification
if ( $email->id == 'new_order' ) {
// Get email
$customer_email = $order->get_billing_email();
// NOT empty
if ( ! empty ( $customer_email ) ) {
// Get orders from customer by email and statuses
$orders_by_customer_email = wc_get_orders( array(
'customer' => $customer_email,
'status' => array( 'wc-on-hold','wc-processing','wc-completed' ),
'limit' => -1,
'return' => 'ids'
));
// When new customer
if ( count( $orders_by_customer_email ) == 1 ) {
$customer = __( 'new', 'woocommerce' );
} else {
$customer = __( 'returning', 'woocommerce' );
}
// Output
echo '<p style="color:red;font-size:14px;">' . sprintf( __( 'Customer: %s, number of purchases: %d', 'woocommerce' ), $customer, count( $orders_by_customer_email ) ) . '</p>';
}
}
}
add_action( 'woocommerce_email_order_details', 'action_woocommerce_email_order_details', 10, 4 );
I am trying to set the default field value of a custom confirm email field in checkout.
If billing_email exists in user meta I would like to set the meta value as default value for the field.
If billing_email not exists in user meta I want to set the meta value of user_email as default value for the field.
I tried this:
$fields['billing_email_confirm'] = array(
'label' => 'confirm email *',
'class' => array( 'form-row-last' ),
'priority' => 9999,
);
$current_user = wp_get_current_user();
$havemeta = get_user_meta($customer_id, 'billing_email', false);
if ($havemeta)
{
$fields['billing_email_confirm']['default'] = $current_user->user_email;
} else {
$fields['billing_email_confirm']['default'] = $current_user->billing_email;
}
return $fields;
}
It works if the billing_email exists in user meta. But if not the field is empty.
There are few logical mistakes in your code, here is cleaned code with explanations:
$fields['billing_email_confirm'] = array(
'label' => 'confirm email *',
'class' => array( 'form-row-last' ),
'priority' => 9999,
);
// Check is customer ID is available or not?
if ( ! empty( $customer_id ) ) {
// Get customer data using customer id.
$customer = get_userdata( $customer_id );
// Get billing email from user meta.
// 3rd param should be true as we need single value not an array.
$billing_email = get_user_meta( $customer_id, 'billing_email', true );
// Check if billing email is empty or not?
if ( ! empty( $billing_email ) ) {
// when billing email is not empty.
$fields['billing_email_confirm']['default'] = $billing_email;
} else {
// When billing email is empty use, user account email.
$fields['billing_email_confirm']['default'] = $customer->user_email;
}
}
I am trying to dynamically populate two dropdown fields in a Gravity Forms form. The first field dynamically populates with the terms available in a custom post type. I want the second dynamically populated field to contain the list of all post titles within the custom post type AND have those titles filtered by the term selected in the previous dropdown. Is it possible to get the value of a dropdown within Gravity Forms and pass that value as a parameter in $args to use the get_posts($args) function?
I started using the following tutorial as a guide. https://docs.gravityforms.com/dynamically-populating-drop-down-fields/
add_filter( 'gform_pre_render_3', 'populate_procedures' );
add_filter( 'gform_pre_validation_3', 'populate_procedures' );
add_filter( 'gform_pre_submission_filter_3', 'populate_procedures' );
add_filter( 'gform_admin_pre_render_3', 'populate_procedures' );
function populate_procedures( $form ) {
// Procedure Category Dropdown
foreach ( $form['fields'] as &$field ) {
The first field. The following code populates a dropdown field containing a list of all of the terms within a custom post type (procedure):
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate_procedure_categories' ) === false ) {
continue;
}
$terms = get_terms( array(
'taxonomy' => 'procedure_category',
'orderby' => 'name',
'order' => 'ASC',
) );
// you can add additional parameters here to alter the posts that are retrieved
// more info: http://codex.wordpress.org/Template_Tags/get_posts
//$posts = get_posts( 'post_type=procedure&numberposts=-1&post_status=publish' );
$choices = array();
foreach ( $terms as $term ) {
$choices[] = array( 'text' => $term->name, 'value' => $term->name );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select Procedure Category';
$field->choices = $choices;
The second field. The following code dynamically populates the field with all of the the post titles of the custom post type (procedure). I want to filter these results based upon the value selected above.
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate_procedures' ) === false ) {
continue;
}
$args = array(
'post_status' => 'publish',
'post_type' => 'procedure',
'procedure_category' => 'cardiovascular',
);
$posts = get_posts( $args );
$choices = array();
foreach ( $posts as $post ) {
$choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select Procedure';
$field->choices = $choices;
}
return $form;
}
The second dynamically populated field successfully pulls in the filtered list of post titles based on the $args if I explicitly listed the term (in the example above I used 'cardiovascular'). What I am wondering is if there is a way to grab the value of the previous field and use that to filter the results of the second field (without having to reload the page). Any ideas? Does Gravity Forms have a functionality like this built in?
Using this method, you would need to use multiple pages and add the second Drop Down field to the second page on the form. Then, when the user submits the first page, you can access the value of the first Drop Down from the $_POST. Gravity Forms has a helper method for doing this called rgpost(). Here's what your $args might look like:
$args = array(
'post_status' => 'publish',
'post_type' => 'procedure',
'procedure_category' => rgpost( 'input_FIELDID' ),
);
Replace FIELDID with the field ID of your first Drop Down.
With that said, if you want to accomplish this without having to touch any code, try Gravity Forms Populate Anything.
https://gravitywiz.com/documentation/gravity-forms-populate-anything/
Using Wordpress/Woocommerce.
I have this code which adds a custom billing field in checkout page called: "NIF/CIF". It works fine but its value it not saved in the customer account "Billing Address" data.
Once the customer makes a first order all billing address values are saved in his account: Address, State, Country, etc. But my custom field is not saved.
I guess that in my function is missing a line of code to save its value in the database, but I don't know how to start with this.
/*******************************
CUSTOM BILLING FIELD
*********************************/
add_filter('woocommerce_billing_fields', 'custom_woocommerce_billing_fields');
function custom_woocommerce_billing_fields($fields)
{
$fields['nif_cif'] = array(
'label' => __('NIF/CIF', 'woocommerce'), // Add custom field label
'placeholder' => _x('NIF/CIF', 'placeholder', 'woocommerce'), // Add custom field placeholder
'required' => true, // if field is required or not
'clear' => false, // add clear or not
'type' => 'text', // add field type
'class' => array('my-css') // add class name
);
return $fields;
}
Here is an example of how to save your custom field:
add_action( 'woocommerce_checkout_order_processed', 'prefix_save_field_on_checkout', 11, 2 );
function checkout_order_processed_add_referral_answer( $order_id, $posted ) {
if ( ! isset( $_POST['nif_cif'] ) ) {
return;
}
$order = wc_get_order( $order_id );
// WC < 3.0
update_post_meta( $order->id, 'order_meta_field_name', wc_clean( $_POST['nif_cif'] ) );
// WC > 3.0
$order->add_meta_data( 'order_meta_field_name', wc_clean( $_POST['nif_cif'] ), true );
$order->save();
}
Adding an extra field through 'woocommerce_billing_fields' hook is not enough. you are missing out two things.
Proper validation of NIF/CIF field using
'woocommerce_after_checkout_validation' hook
Saving data in order
using 'woocommerce_checkout_order_processed' hook
I have two pods: course and teacher.
Each course has a teacher.
I use shortcodes in order to build a form to define new course:
[pods name='course' form='1' fields='name, teacher' ]
When defining a new course, the user can choose the teacher for this course.
By default, the name of the teacher is displayed in a drop down list. I wonder if I can change the output of the teachers in the drop down list.
For example, in addition to the name I want to display a certain field, such as location of the teacher in the drop down list.
Is this possible using the built-in shortcodes of Pods 2?
Update:
Following the instructions of Scott, I solved the problem. I wrote the solution into the comment section but formating was lost. Below, I put the code again:
function pods_teacher_pick_data($data, $name, $value, $options, $pod, $id){
if ($name == "pods_field_teachers") {
foreach ($data as $id => &$value) {
$p = pods('teacher', $id);
$name = $p->display('name');
$city = $p->display('profile.city.name');
$value = $name . ' - ' . $city;
}
}
return $data;
}
add_filter('pods_field_pick_data', 'pods_teacher_pick_data', 1, 6);
Not built in yet, but you can take over the data output using the filter: pods_field_pick_data
$data = apply_filters( 'pods_field_pick_data', $data, $name, $value, $options, $pod, $id );
Adding a filter to that filter should give you the ability to change what appears in the drop-down, or other relationship input types.
Edit: I just added a similar filter for filtering the autocomplete data too.
$pick_data = apply_filters( 'pods_field_pick_data_ajax', array(), $field[ 'name' ], null, $field, $pod, 0, $data );
$data in this array is actually the fully setup PodsData object
EDIT (02/07/2013):
In Pods 2.3, I've added a quick function that should streamline adding custom relationship objects. This is preferrer over overriding an existing relationship or using the custom simple definition dynamically. Pretty easy to use, check it out at https://github.com/pods-framework/pods/issues/1033
$options = array(
'group' => 'Special Relationships', // Whatever you want the selection group to be, defaults to label
'simple' => false, // Whether this field is related by strings or integer IDs, integer IDs get stored in wp_podsrel, strings are stored in the field itself either serialized (meta-based) or json encoded (table-based)
'data' => array( // Custom define the items to select from manually
1 => 'Gravity Form 1',
2 => 'Gravity Form 2'
),
'data_callback' => 'get_custom_gravity_forms_list', // Provide a callback function to call to define the data (instead of setting 'data' above)
'value_to_label_callback' => 'get_custom_gravity_forms_list', // Provide a callback function to define the data when called through PodsField_Pick::value_to_label
'simple_value_callback' => 'get_custom_gravity_forms_list' // Provide a callback function to define the data when called through PodsField_Pick::simple_value
);
pods_register_related_object( 'gravity-form', 'Gravity Forms', $options );
For ajax, it seems need to use a bit different filter pods_field_pick_data_ajax_items like this:
function pods_teacher_pick_data_ajax_items($items, $name, $null, $field, $pod, $id){
if ($name == "pods_meta_categorie" || $name == 'categorie') {
foreach ($items as &$item) {
$id = $item['id'];
$value = $item['text'];
$p = pods('dvd_categories', $id);
$code = $p->display('code');
$item['text'] .= ' - ' . $code;
}
}
return $items;
}
add_filter('pods_field_pick_data_ajax_items', 'pods_teacher_pick_data_ajax_items', 1, 6);
Note different structure of arrays $data and $items.
Filter pods_teacher_pick_data_ajax looks very strange and useless to me as it doesnt accept $data array.