Entering detail information on the created order - woocommerce

I want to create a field where the customer can write the reason for canceling a created order. The code below appears in other orders as well, due to the customer's own knowledge. I made several attempts to register the order, but I could not get the result I wanted. I thought about "update_post_meta" and "order_id" but I guess I failed. Also, I couldn't decide where it would make sense for this area to appear on the admin page. Thanks.
// Display user custom field
add_action( 'woocommerce_order_details_before_order_table', 'add_user_custom_url_field_to_order' );
function add_user_custom_url_field_to_order( $order ) {
global $current_user;
$custom_url = get_user_meta( $current_user->ID, 'custom_URL', true );
?>
<form method="post">
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="custom_URL"><?php _e( 'URL', 'woocommerce' ); ?></label>
<input type="text" name="custom_URL" id="custom_URL" value="<?php echo $custom_url; ?>" />
</p>
<input type="submit" name="submit-custom_URL" value="<?php _e('RUN', 'woocommerce'); ?>" /><br/>
</form>
<?php
}
// Save the field as custom user data
add_action( 'template_redirect', 'save_user_custom_url_field_from_order' );
function save_user_custom_url_field_from_order() {
global $current_user;
if( isset($_POST['custom_URL']) ){
update_user_meta( $current_user->ID, 'custom_URL', sanitize_url( $_POST['custom_URL'] ) );
wc_add_notice( __("Submitted data has been saved", "woocommerce") );
}
}
Code source: https://stackoverflow.com/a/62777930/14597323

Related

How to save checkbox as 'checked' once user registers or save account details

For new users: A checkbox has been added to the Woocommerce Registration form. Once user checks that checkbox it needs to display checked in the WordPress Profile of that user as well as the account details tabs (Above the save details button).
For Current users: The checkbox is unchecked in their WordPress Profiles as well as their Woocommerce account details tab (Above the save details button).
Currently, I am able to check the checkbox upon registering, which then shows that it is checked in my WordPress Profile, however not checked on my Woocommerce Account Details Tab.
Hope this makes more sense. 😶
Here is the code I am using below:
// user consent on registration
//
// Add the checkbox to registration form
add_action( 'register_form', 'foo_add_privacy_policy_field' );
function foo_add_privacy_policy_field() {
?>
<p>
<input type="checkbox" name="foo_privacy_policy" id="foo_privacy_policy" class="checkbox" style="float: left; height: 50px;" />
<label for="foo_privacy_policy">
<?php _e( 'Privacy Policy', 'foo' ) ?>
</label>
</p>
<?php
}
// Validate the checkbox value in the registration form so that it is required
add_filter( 'registration_errors', 'foo_privacy_policy_auth', 10, 3 );
function foo_privacy_policy_auth( $errors, $sanitized_user_login, $user_email ) {
if ( !isset( $_POST[ 'foo_privacy_policy' ] ) ):
$errors->add( 'policy_error', "<strong>ERROR</strong>: Please accept the privacy policy." );
return $errors;
endif;
return $errors;
}
// Fill the meta 'foo_privacy_policy' with the value of the checkbox
add_action( 'personal_options_update', 'foo_privacy_policy_save' );
add_action( 'edit_user_profile_update', 'foo_privacy_policy_save' );
add_action( 'woocommerce_save_account_details', 'foo_privacy_policy_save' );
add_action( 'user_register', 'foo_privacy_policy_save' );
function foo_privacy_policy_save( $user_id ) {
if ( isset( $_POST[ 'foo_privacy_policy' ] ) ) {
update_user_meta( $user_id, 'foo_privacy_policy', $_POST[ 'foo_privacy_policy' ] );
} else {
update_user_meta( $user_id, 'foo_privacy_policy', 'off' );
}
}
// Add the checkbox to user profile home
add_action( 'woocommerce_register_form', 'foo_show_extra_profile_fields' );
add_action( 'woocommerce_edit_account_form', 'foo_show_extra_profile_fields' );
add_action( 'show_user_profile', 'foo_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'foo_show_extra_profile_fields' );
function foo_show_extra_profile_fields( $user ) {
?>
<h3>
<?php esc_html_e( 'Privacy Policy', 'foo' ); ?>
</h3>
<table class="form-table">
<tr>
<td>
<input type="checkbox" name="foo_privacy_policy" id="foo_privacy_policy" class="checkbox" value='1' style="float: left; height: 50px;" <?php if (esc_attr(get_the_author_meta('foo_privacy_policy', $user->ID))=='on' ){ echo "checked"; } ?> />
<label for="foo_privacy_policy">
<?php _e( 'Privacy Policy', 'foo' ) ?>
</label>
</td>
</tr>
</table>
<?php
}

Wordpress-Update Birth Date in Woocommerce Account

I tried to implement custom field into WooCommerce registration page on my site, and works great, but if want to Modify that date into My Account page, changes are not saved. Looks like i have missed something somewhere that don't proceed data to database. This is code for custom field:
<p class="form-row form-row-last">
<label for="reg_billing_birthdate"><?php _e( 'Date of Birth', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="billing_birthdate" id="reg_billing_birthdate" value="<?php if ( ! empty( $_POST['billing_birthdate'] ) ) esc_attr_e( $_POST['billing_birthdate'] ); ?>" />
</p>
column billing_birthdate is created into wp_usermeta table, But i added this code also into functions.php into theme:
and this code to proceed data to database..
/**
* Below code save extra fields.
*/
function wooc_save_extra_register_fields( $customer_id ) {
if ( isset( $_POST['billing_birthdate'] ) ) {
// Billing Address field which is by default
update_user_meta( $customer_id, 'last_name', sanitize_text_field( $_POST['billing_birthdate'] ) );
// Last name field which is used in WooCommerce
update_user_meta( $customer_id, 'billing_birthdate', sanitize_text_field( $_POST['billing_birthdate'] ) );
}
}
I alse added this code to functions.php file, to be able to edit that field into edit-account page.
/**
* To display additional field at My Account page
* Once member login: edit account
*/
add_action( 'woocommerce_edit_account_form', 'my_woocommerce_edit_account_form' );
function my_woocommerce_edit_account_form() {
$user_id = get_current_user_id();
$user = get_userdata( $user_id );
if ( !$user )
return;
$birthdate = get_user_meta( $user_id, 'birthdate', true );
?>
<fieldset>
<p class="form-row form-row-thirds">
<label for="birthdate">Birth date:</label>
<input type="text" name="birthdate" value="<?php echo esc_attr( $birthdate ); ?>" class="input-text" />
<br />
<span style="font-size: 12px;">(Birth date format: DD-MM-YYYY. eg: 31-12-2005)</span>
</p>
</fieldset>
<?php
} // end func
/**
* This is to save user input into database
* hook: woocommerce_save_account_details
*/
add_action( 'woocommerce_save_account_details', 'my_woocommerce_save_account_details' );
function my_woocommerce_save_account_details( $user_id ) {
update_user_meta( $user_id, 'birthdate', htmlentities( $_POST[ 'birthdate' ] ) );
} // end func
So my question is where is my error my after editing that field into my-account page, changes are not reflected into account dashboard.
What I found that, you are updating the field called 'billing_birthdate' at the time you are inserting the data. But when you are editing you are updating the field 'birthdate'.
For update :
update_user_meta( $customer_id, 'billing_birthdate',htmlentities( $_POST['birthdate'] ) );
Hope this will work for you.

Custom User Fields on Registration

I'm working on a custom plugin for a client and I need to add some custom user fields. I've searched through the Codex but couldn't come across the answer.
I basically need to add some new rows to the users table in the MySQL and then add some extra fields during the registration. I'm sure there are other plugins out there that allow you to add custom user fields but I'd like to incorporate it directly into my plugin.
How can I do it?
I've answered a similar Question at WordPress Answers: Checkboxes in registration form.
You need the action hooks register_form (to inject your input fields) and user_register (to process it). The rest of the code is just sample code to check the results in the pages Profile and User Edit.
// REGISTRATION
add_action( 'register_form', 'signup_fields_wpse_87261' );
add_action( 'user_register', 'handle_signup_wpse_87261', 10, 2 );
// PROFILE
add_action( 'show_user_profile', 'user_field_wpse_87261' );
add_action( 'personal_options_update', 'save_profile_fields_87261' );
// USER EDIT
add_action( 'edit_user_profile', 'user_field_wpse_87261' );
add_action( 'edit_user_profile_update', 'save_profile_fields_87261' );
function signup_fields_wpse_87261() {
?>
<label>
<input type="checkbox" name="custom_feature_a" id="custom_feature_a" />
Enable feature A?
</label>
<br />
<label>
<input type="checkbox" name="custom_feature_b" id="custom_feature_b" />
Enable feature B?
</label>
<hr />
<?php
}
function handle_signup_wpse_87261( $user_id, $data = null )
{
$feat_a = isset( $_POST['custom_feature_a'] ) ? $_POST['custom_feature_a'] : false;
$feat_b = isset( $_POST['custom_feature_b'] ) ? $_POST['custom_feature_b'] : false;
if ( $feat_a )
{
add_user_meta( $user_id, 'custom_feature_a', $feat_a );
}
if ( $feat_b )
{
add_user_meta( $user_id, 'custom_feature_b', $feat_b );
}
}
function user_field_wpse_87261( $user )
{
$feat_a = get_user_meta( $user->ID, 'custom_feature_a', true );
$feat_b = get_user_meta( $user->ID, 'custom_feature_b', true );
?>
<h3><?php _e('Custom Fields'); ?></h3>
<table class="form-table">
<tr>
<td>
<label><?php
printf(
'<input type="checkbox" name="custom_feature_a" id="custom_feature_a" %1$s />',
checked( $feat_a, 'on', false )
);
?>
<span class="description"><?php _e('Custom Feature A?'); ?></span>
</label>
</td>
</tr>
<tr>
<td>
<label><?php
printf(
'<input type="checkbox" name="custom_feature_b" id="custom_feature_b" %1$s />',
checked( $feat_b, 'on', false )
);
?>
<span class="description"><?php _e('Custom Feature B?'); ?></span>
</label>
</td>
</tr>
</table>
<?php
}
function save_profile_fields_87261( $user_id )
{
$feat_a = isset( $_POST['custom_feature_a'] ) ? $_POST['custom_feature_a'] : false;
$feat_b = isset( $_POST['custom_feature_b'] ) ? $_POST['custom_feature_b'] : false;
update_usermeta( $user_id, 'custom_feature_a', $feat_a );
update_usermeta( $user_id, 'custom_feature_b', $feat_b );
}

Meta box only saving the first of multiple input fields

To keep it short: I built a meta box with 3 fields, but it's only saving the first field. What could I be doing wrong?
// --- METABOX: CAREER ... CONTENTS --- //
function career_meta(){
global $post;
$career1 = get_post_meta( $post->ID, 'career1', true );
$career2 = get_post_meta( $post->ID, 'career2', true );
$career3 = get_post_meta( $post->ID, 'career3', true );
?>
<label for="career_subtitle">Subtitle</label><input type="text" class="widefat" id="career-subtitle" name="career_subtitle" value="<?php echo $career1; ?>" />
<label for="career_text_1">Left Column</label><textarea class="widefat" id="career-text-1" name="career_text_1" value="<?php echo $career2; ?>"></textarea>
<label for="career_text_2">Right Column</label><textarea class="widefat" id="career-text-2" name="career_text_2" value="<?php echo $career3; ?>"></textarea>
<?php }
// --- METABOX: CAREER ... SAVE --- //
add_action('save_post', 'save_career');
function save_career(){
global $post;
update_post_meta($post->ID, "career1", $_POST["career_subtitle"]);
update_post_meta($post->ID, "career2", $_POST["career_text_1"]);
update_post_meta($post->ID, "career3", $_POST["career_text_2"]);
}
add_action('save_post','function_save_var');
function function_save_var()
{
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
}
That's what I have right now. It saves the first field (career_subtitle) but not the 2 other ones. Fixing this is quite important so I would really appreciate an effective answer :)
This maynot be the solution to your question but are you sure that textarea has a value attribute????
This could solve your problem:-
<label for="career_text_1">Left Column</label><textarea class="widefat" id="career-text-1" name="career_text_1"><?php echo $career2; ?></textarea>
<label for="career_text_2">Right Column</label><textarea class="widefat" id="career-text-2" name="career_text_2"><?php echo $career3; ?></textarea>

Add Custom ComboBox Filters to Admin User List in Wordpress

I want to add 2 combo box in Admin User List Panel. For example the first one will be a combo box with countries and the other combo will be User Age.
So I want to add these combos in order to filter the user list.
Could you please shed some light here?.
Thank you.
This is what i'm looking for:
add_action('restrict_manage_posts', 'my_restrict_manage_posts');
function my_restrict_manage_posts()
{
global $typenow;
if ($typenow == 'your_custom_post_type') {
$args = array(
'show_option_all' => "Show All Categories",
'taxonomy' => 'your_custom_taxonomy',
'name' => 'your_custom_taxonomy'
);
wp_dropdown_categories($args);
}
}
add_action('request', 'my_request');
function my_request($request)
{
if (is_admin() && $GLOBALS['PHP_SELF'] == '/wp-admin/edit.php' && isset($request['post_type']) && $request['post_type'] == 'your_custom_post_type') {
$request['term'] = get_term($request['your_custom_taxonomy'], 'your_custom_taxonomy')->name;
}
return $request;
}
You can add your own fields to a user add/edit field.
this example shows how to add a address input field, if you get this working try switching it with the dropdown's that you need. If that's what you mean by 'combobox'
function fb_add_custom_user_profile_fields( $user ) {
?>
<h3><?php _e('Extra Profile Information', 'your_textdomain'); ?></h3>
<table class="form-table">
<tr>
<th>
<label for="address"><?php _e('Address', 'your_textdomain'); ?>
</label></th>
<td>
<input type="text" name="address" id="address" value="<?php echo esc_attr( get_the_author_meta( 'address', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e('Please enter your address.', 'your_textdomain'); ?></span>
</td>
</tr>
</table>
<?php }
function fb_save_custom_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return FALSE;
update_usermeta( $user_id, 'address', $_POST['address'] );
}
add_action( 'show_user_profile', 'fb_add_custom_user_profile_fields' );
add_action( 'edit_user_profile', 'fb_add_custom_user_profile_fields' );
add_action( 'personal_options_update', 'fb_save_custom_user_profile_fields' );
add_action( 'edit_user_profile_update', 'fb_save_custom_user_profile_fields' );

Resources