WP-Members / How to update value of radio button value? - wordpress

I'm using WP-Members. And I'm using wp_update_user function to update value.
When I try to update the "text" value, it is updated. Like this.
$test = array ( 'ID' => $id,'first_name' => $first_name);
But, when it comes to update the value of "radio", it didn't work.
$test = array ( 'ID' => $id,'plan' => $plan);
$plan is a string value
setting of the "plan"
How can I update the radio button value?

wp_update_user() only updates WordPress native fields. If you have a radio field in your form, then that would certainly be a custom field not native to WP. Any custom field is a user meta field and must be updated with update_user_meta():
update_user_meta( $id, 'plan', $plan );
Note that this would be true of any field that is not a native WP field (i.e. any custom fields you added in WP-Members, or using any other method).

Related

Populate WordPress Post dynamically on Gravity Forms

I have a custom post type named Student on WordPress that Adds new students. You can fill out the Students name, Student description and Student Admission Number.
I have also created a gravity form where a potential sponsor can submit their details when they want to sponsor a student. However, the sponsors need to fill in the Students Name and Student Admission Number on the form. Is it possible to have the fields Student Name and Student Admission Number populate dynamically on the gravity form?
The form is being displayed on the same page as the student post. The student name is the post title. The student registration number is a custom field created using advanced custom field plugin.The field label for student admission number is Student Admission Number and field name is student_admission_number on the custom field. On gravity form, we have Student Name and Student Admission Number as form fields.
How can I have the fields Student Name and Student Admission Number populate dynamically on the gravity form?
I tried adding the following code to my functions.php file but it did not work.
// Add a custom shortcode to display the form
function custom_gravity_form_shortcode( $atts ) {
$current_post = get_post();
// Get the student name and admission number from the current post
$student_name = $current_post->post_title;
$student_admission_number = get_field('student_admission_number', $current_post->ID);
// Get the form ID from the shortcode attributes
$form_id = $atts['id'];
// Prepare the form arguments
$form_args = array(
'field_values' => array(
'student_name' => $student_name,
'student_admission_number' => $student_admission_number,
),
);
// Render the form
return gravity_form( $form_id, false, false, false, $form_args, true );
}
add_shortcode( 'gravityform', 'custom_gravity_form_shortcode' );
Try the following (The shortcode you would use on the page is [stud_gravityform]:
function custom_gravity_form_shortcode($atts = array()){
$details = shortcode_atts( array(
'form_id'=>'176'//change 176 to your form id
), $atts );
$current_post = get_post();
// Get the student name and admission number from the current post
$student_name = $current_post->post_title;
$student_admission_number = get_field('student_admission_number', $current_post->ID);
// Get the form ID from the shortcode attributes
$form_id = $details['form_id'];
// Prepare the form arguments
$form_args = array(
'field_values' => array(
'student_name' => $student_name,
'student_admission_number' => $student_admission_number
)
);
// Render the form
return gravity_form( $form_id, false, false, false, $form_args, true );
}
add_shortcode( 'stud_gravityform', 'custom_gravity_form_shortcode' );//the gravityform shortcode is already taken so I changed it to "stud_gravityform"
It might be easier to rely on the {custom_field} merge tag set in the default value setting of each field that should be dynamically populated. With this, it would fetch whatever custom field you specify from the post on which the form is embedded.
Less code. And easier to configure.

Woocommerce: Translate/change order item meta key before output

There's an action called 'woocommerce_checkout_create_order_line_item' where you can use the method 'add_meta_data(meta_key, meta_value)' to add meta data to the order item.
Every example did use this syntax: add_meta_data(__('Some key name', 'woocommerce'), $value);
This is perfect as long as the admin and the customer have set the same language and the field is only informal.
But I have to process the field programmatically later. So the key must always be the same, no matter which language is used.
So I use a simple string, but it won't be translated in the admin order area, on the Thank-You page, in the shopping cart etc. anymore.
How can I override the automatic output of the order item meta data? I've tried to hook i.e. into 'woocommerce_before_order_itemmeta($item_id, $item, $product)' (Admin order page), but there I can only delete $item->delete_meta_data('my_key'), I cannot add temporary meta keys => $item->add_meta_data(__('Translated key'), $value);
I also tried:
$item->set_meta_data(array('id' => 0, 'key' => __('Translated key'), 'value' => 'test123'));
print_r($item->get_meta_data());
Thanks!
There is no need to do the translation in the meta key.
What you could do is use the hook woocommerce_order_item_display_meta_key. There you can catch your key and output a tranlation for display.
Also you can filter keys to not display all custom meta keys if you have some keys that should not show up in the order details, but are for internal use only. This can be done with woocommerce_hidden_order_itemmeta
Here is a good post on working with order item meta data: https://www.ibenic.com/manage-order-item-meta-woocommerce/

Silverstripe drop down field does not show saved value as selected

I have created a drop down field in the CMS like so:
class ProductPage extends Page {
//.....
private static $has_one = [
'TeaserImage'=>Image::Class,
'LinkedProduct'=>'Product'
];
public function getCMSFields(){
$fields = parent::getCMSFields();
$productLinkField = DropdownField::create('LinkedProduct', 'Link a Product', Product::get()->map('ID', 'ProductName'));
$productLinkField->setEmptyString('(Select one)');
$fields->addFieldToTab('Root.Main', $productLinkField, 'Content');
return $fields;
}
}
The problem is that when I select a value and save/publish the page the drop down goes back to "Select one" instead of showing the saved selection.
I have not checked the database to see if the value is being stored but I assume it is.
EDIT: Not a duplicate.
The suggested duplicate dealt with removing a field from the CMS.
This question deals with setting the drop down value to the saved selection.
The answers are however similar. The user must always append ID to a has_one field for the CMS to interact with it.
By default SilverStripe appends an 'ID' parameter to the end of has_one relation fields when saving them in the database.
As such when you override the field for relations you will need to append 'ID' to the field identifier.
DropdownField::create('LinkedProductID', 'Link a Product', Product::get()->map('ID', 'ProductName'));

Form tests: How to submit a collection to an existing form? [duplicate]

This question already has answers here:
Symfony2: Test on ArrayCollection gives "Unreachable field"
(4 answers)
Closed 6 years ago.
I use two ways to test my forms:
By using $form = …->form();
Then setting the values of the $form array (more precisely this is a \Symfony\Component\DomCrawler\Form object):
Full example from the documentation:
$form = $crawler->selectButton('submit')->form();
// set some values
$form['name'] = 'Lucas';
$form['form_name[subject]'] = 'Hey there!';
// submit the form
$crawler = $client->submit($form);
By sending the POST data directly:
The previous code doesn't work with forms which manage collections (relying on fields created by Javascript) because it throws an error if the field doesn't exist. That's why I also use this other way.
Full example from the documentation:
// Directly submit a form (but using the Crawler is easier!)
$client->request('POST', '/submit', array('name' => 'Fabien'));
This solution is the only way I know to test forms which manage collections with fields added by Javascript (see link to documentation above). But this second solution is harder to use because:
it doesn't check which fields exist, this is impractical when I have to submit a form with existing fields and a collection which relies on fields created dynamically with Javascript
it requires to add the form _token manually
My question
Is it possible to use the syntax from the first way to define the existing fields then add new dynamically created fields with the second syntax?
In other words, I would like to have something like this:
$form = $crawler->selectButton('submit')->form();
// set some values for the existing fields
$form['name'] = 'Lucas';
$form['form_name[subject]'] = 'Hey there!';
// submit the form with additional data
$crawler = $client->submit($form, array('name' => 'Fabien'));
But I get this error:
Unreachable field "name"
And $form->get('name')->setData('Fabien'); triggers the same error.
This example is not perfect because the form has no collection, but it's enough to show you my problem.
I'm looking for a way to avoid this validation when I add some fields to the existing form.
This can be done by calling slightly modified code from the submit() method:
// Get the form.
$form = $crawler->filter('button')->form();
// Merge existing values with new values.
$values = array_merge_recursive(
$form->getPhpValues(),
array(
// New values.
'FORM_NAME' => array(
'COLLECTION_NAME' => array(
array(
'FIELD_NAME_1' => 'a',
'FIELD_NAME_2' => '1',
)
)
)
)
);
// Submit the form with the existing and new values.
$crawler = $this->client->request($form->getMethod(), $form->getUri(), $values,
$form->getPhpFiles());
The array with the news values in this example correspond to a form where you have a fields with these names:
<input type="…" name="FORM_NAME[COLLECTION_NAME][A_NUMBER][FIELD_NAME_1]" />
<input type="…" name="FORM_NAME[COLLECTION_NAME][A_NUMBER][FIELD_NAME_2]" />
The number (index) of the fields is irrelevant, PHP will merge the arrays and submit the data, Symfony will transform this data in the corresponding fields.

Drupal custom user registration form

I have built a custom registration form using module_form_alter hook. I have also added the required new fields to the database with db_add_field. Now I'm able to add the values to the table in user registration/ user profile edit and the values are also getting stored in the database.. But what I'm not able to do is get the values that are stored in the database in the user profile edit form is displayed. Is there a hook to load the values from database to form on form load? Or is there any other way?
function customUser_schema_alter(&$schema) {
// Add field to existing schema.
$schema['users']['fields']['detail'] = array(
'type' => 'varchar',
'length' => 100,
);
}
function customUser_install() {
$schema = drupal_get_schema('users');
db_add_field('users', 'detail', $schema['fields']['detail']);
}
function customUser_form_alter(&$form, &$form_state, $form_id) {
// check to see if the form is the user registration or user profile form
// if not then return and don’t do anything
if (!($form_id == 'user_register_form' || $form_id == 'user_profile_form')) {
return;
}
$form['account']['detail'] = array(
'#type' => 'textfield',
'#title' => t('Additional Detail'),
);
}
A proper answer needs more details. I can only assume what you did.
You added fields to the {users} table. You didn't update the database schema which made drupal_write_record not be aware of the new fields, that being the reason they are not populated.
You created a new table {my_table} with the fields.
In both cases you need hook_user_insert()
/**
* Implements hook_user_insert().
*/
function mymodule_user_insert(&$edit, $account, $category) {
// Here you add the code to update the entry in {users} table,
// or int your custom table.
// $edit has the values from the form, $account->uid has the
// uid of the newly created user.
}
Note: If my first assumption is true that's not the drupal way to do it. You should have done the 2nd way instead. And even in that case use the hook_schema to create your table in mymodule.install instead of doing db_add_field().
For drupal 7 you could have uses the profile module (core) or profile2 to achieve that.
Based on that code
Try to change to this inside the form alter.
$account = $form['#user'];
$form['account']['detail'] = array(
'#type' => 'textfield',
'#title' => t('Additional Detail'),
'#default_value' => $account->detail,
);

Resources