Wordpress Woocommerce Add custom form in my account page - php

I need to create a custom form in a new tab in woocommerce my account page.¨
I need to save data and output them to the user
What I've tried:
add_action( 'init', 'misha_add_endpoint' );
function misha_add_endpoint() {
add_rewrite_endpoint( 'log-history', EP_PAGES );
}
add_action( 'woocommerce_account_log-history_endpoint', 'misha_my_account_endpoint_content' );
function misha_my_account_endpoint_content() {
get_current_user_id()
echo '<h3>User settings prize wheel</h3>';
$user = wp_get_current_user();
echo '
<form class="woocommerce-EditAccountForm edit-account" action="" method="post">
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="account_display_name">Steam ID <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="steam_id" id="account_display_name" value="'.esc_attr( $user->steam_id ).'"> <span><em>Vaše Steam ID, místo kam se budou posílat výherní item. </em></span>
</p>
<div class="clear"></div>
<p>
<button type="submit" class="woocommerce-Button button" name="save_prize_wheel" value="Uložit změny">Uložit změny</button>
<input type="hidden" name="action" value="save_prize_wheel">
</p>
</form>
';
}
add_action( 'woocommerce_save_prize_wheel', 'save_favorite_color_account_details', 12, 1 );
function save_favorite_color_account_details( $user_id ) {
// For Favorite color
if( isset( $_POST['steam_id'] ) )
update_user_meta( $user_id, 'steam_id', sanitize_text_field( $_POST['steam_id'] ) );
}
The data did not save.

You miss semi Colin here get_current_user_id() it should be get_current_user_id();

Related

Add/update fields on My Account page in Woocommerce

I need to create My Account page for registered users with changeable fields on backend and frontend.
Now the fields are updated if I change the values through the admin panel. However, if I change the field values through My Account, the fields are not saved or updated.
What do i need to change or add to the code?
I used the following LoicTheAztec's answers:
Add a custom field in Woocommerce Edit Account page
Adding some my account custom fields to admin user pages in Woocommerce
Adding an additional custom field in Woocommerce Edit Account page
My code:
Template:
$user = wp_get_current_user();
<form class="tabs-content__page_inner woocommerce-EditAccountForm edit-account" action="" method="post"
<div class="pers-acc__tab-fields_field tab-field woocommerce-form-row form-row">
<label for="account_phone" class="tab-field__title"><?php esc_html_e( 'Телефон', 'woocommerce' ); ?></label>
<div class="tab-field__input-inner">
<input type="text" class="tab-field__input woocommerce-Input woocommerce-Input--text input-text" name="account_phone" id="account_phone" value="<?php echo esc_attr( $user->account_phone ); ?>" />
</div>
</div>
</form>
functions.php
<?php
add_action('woocommerce_save_account_details', 'save_phone_number_account_details', 20, 1);
function save_phone_number_account_details($user_id) {
if( isset( $_POST['account_phone'] ) ) {
update_user_meta( $user_id, 'account_phone', sanitize_text_field( $_POST['account_phone'] ) );
}
}
//custom field in admin console
add_action( 'show_user_profile', 'add_extra_custom_user_data', 1, 1 );
add_action( 'edit_user_profile', 'add_extra_custom_user_data', 1, 1 );
function add_extra_custom_user_data( $user )
{
?>
<h3><?php _e("Other details",'woocommerce' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="account_phone"><?php _e( 'Custom phone', 'woocommerce' ); ?></label></th>
<td><input type="text" name="account_phone" value="<?php echo esc_attr(get_the_author_meta( 'account_phone', $user->ID )); ?>" class="regular-text" /></td>
</tr>
</table>
<br />
<?php
}
//save custom field value in admin console
add_action( 'personal_options_update', 'save_extra_custom_user_data' );
add_action( 'edit_user_profile_update', 'save_extra_custom_user_data' );
function save_extra_custom_user_data( $user_id )
{
if( ! empty($_POST['account_phone']) )
update_user_meta( $user_id, 'account_phone', sanitize_text_field( $_POST['account_phone'] ) );
}

Add and save a text field in WooCommerce customer order detail pages

I have been able to add a custom text input field, in woocommerce's order details page, but I can't save the data submitted data.
This is the code:
add_action( 'woocommerce_order_details_before_order_table', 'add_custom_Field',10,2 );
function add_custom_Field( $order_id) {
$user = wp_get_current_user();
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
?>
<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 esc_attr( $order_id->custom_URL ); ?>" />
</p>
<input type="submit" name="test" id="test" value="RUN" /><br/>
</form>
<?php
function submit()
{
update_user_meta( $user_id, 'custom_URL', sanitize_text_field( $_POST['custom_URL'] ) );
echo "Your function on button click is working";
}
if(array_key_exists('test',$_POST)){
submit();
}
}
Do you know what I'm doing wrong?
The following will allow you to add a user custom field to customer order detail and to save its value on submission:
// 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 goes in functions.php file of your active child theme (or active theme). Tested and works.
Note based on your comment:
If the custom url has to be different for each order, you can't save it as user meta data, but instead as $order item meta data... But the code will be slightly different and you will need to define how will be your custom urls.

Customize WooCommerce login form user fields

I would like to include a placeholder in the woocommerce login form.
Looking at this for a starting point:
woocommerce add placeholder text to edit account form:
add_filter( 'woocommerce_form_field_args', 'custom_form_field_args', 10, 3 );
function custom_form_field_args( $args, $key, $value ) {
if ( $args['id'] == 'username' ) {
$args['placeholder'] = 'My placeholder text';
}
return $args;
};
Not working as it is. Open to any suggestions.
As the login fields are hard coded in myaccount/form-login.php template file, the only way to add placeholder(s) to those fields, requires overriding the related template via the active child theme (or active theme)
Once you have copied the myaccount/form-login.php template file located in WooCommerce plugin under the "templates" folder to (as explained on the template):
<?php
/**
* Login Form
*
* This template can be overridden by copying it to yourtheme/woocommerce/myaccount/form-login.php.
Open/Edit form-login.php copied file, and add to all desired <input> html tags:
placeholder="placeholder text"
Like for example replacing from line 38 to 45 with the following:
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="username"><?php esc_html_e( 'Username or email address', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="username" id="username" placeholder="<?ph esc_html_e("Here type your Username (or email address)"); ?>" autocomplete="username" value="<?php echo ( ! empty( $_POST['username'] ) ) ? esc_attr( wp_unslash( $_POST['username'] ) ) : ''; ?>" /><?php // #codingStandardsIgnoreLine ?>
</p>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="password"><?php esc_html_e( 'Password', 'woocommerce' ); ?> <span class="required">*</span></label>
<input class="woocommerce-Input woocommerce-Input--text input-text" type="password" name="password" id="password" placeholder="<?ph esc_html_e(""Here type your password"); ?>" autocomplete="current-password" />
</p>
Then save… You will get something like:
Related threads:
WooCommerce condition for is_account_page(), but only the login portion
Add a field to Woocommerce registration form and in admin edit user
Sync additional Billing registration fields with default Wordpress fields in WooCommerce

WordPress Update User Meta Front End Profiles

i created a plugin to create a user profile page. On this the user is able to update custom meta information through a form. Here is my function:
// Function to edit User Meta
function personalfragebogen_konto_bearbeiten() {
global $current_user;
// Get User Meta
$strasse = get_user_meta( $current_user->ID, '_strasse', true);
// Create Form
<form name="personalfragebogen" action="" method="POST">
<span class="full" >
<span class="two_fifth first">
<h3><?php _e( 'Straße:', 'themesdojo' ); ?></h3>
</span>
<span class="three_fifth">
<input type="text" name="strasse" id="strasse" value="<?php echo $strasse; ?>" class="input-textarea"/>
</span>
</span>
<button type="submit">Speichern</button>
</form>
// Get New User Meta
$strasse = $_POST['strasse'];
// Update/Create User Meta
update_user_meta( $current_user->ID, '_strasse', $strasse);
// Add Hook
add_action( 'personalfragebogen_init', 'personalfragebogen_konto_bearbeiten');
function personalfragebogen_init() {
do_action('personalfragebogen_init');
}
Everything works fine, except of one thing. When I submit the form the data saves to the database and the page refreshes. But now on my refreshed page the form is empty. When refresh the page again then the data is shown. Whats the problem about this?
Thank you in advance!
Something like this could work...
<?php
// Function to edit User Meta
function personalfragebogen_konto_bearbeiten() {
global $current_user;
// Get New User Meta
if(isset($_POST['strasse'])) {
$strasse = $_POST['strasse'];
// Update/Create User Meta
update_user_meta( $current_user->ID, '_strasse', $strasse);
else {
// Get User Meta
$strasse = get_user_meta( $current_user->ID, '_strasse', true);
}
?>
<form name="personalfragebogen" action="" method="POST">
<span class="full" >
<span class="two_fifth first">
<h3><?php _e( 'Straße:', 'themesdojo' ); ?></h3>
</span>
<span class="three_fifth">
<input type="text" name="strasse" id="strasse" value="<?php echo $strasse; ?>" class="input-textarea"/>
</span>
</span>
<button type="submit">Speichern</button>
</form>
<?php
}
// Add Hook
add_action( 'personalfragebogen_init', 'personalfragebogen_konto_bearbeiten');
function personalfragebogen_init() {
do_action('personalfragebogen_init');
}

ask without log in or entering email in DWQA (wordpress plugin)

IN QW question & Answer ( wordpress plugin ) ,how can user add question without registering(log in) or entering their email address ?
I want that all user(guest) can be able to ask their questions without entering their email address or even registering.
I had the same issue and I just resolved it.
DWQA version 1.3.3
You have to replace the function
function dwqa_require_field_submit_question()
with your own version. You find the old function at line 79 in your template-functions.php in the plugin folder.
Just removing the function from the queue will make it impossible to add new questions. So you just add the adjusted version of the function to your functions.php in your theme folder and and remove the standard function and add the adjusted function.
Add following to your functions.php:
function dwqa_require_field_submit_question_adjusted(){
?>
<input type="hidden" name="dwqa-action" value="dwqa-submit-question" />
<?php wp_nonce_field( 'dwqa-submit-question-nonce-#!' ); ?>
<?php
$subscriber = get_role( 'subscriber' );
?>
<?php if ( ! is_user_logged_in() && ! dwqa_current_user_can( 'post_question' ) ) { ?>
<input type="hidden" name="login-type" id="login-type" value="sign-up" autocomplete="off">
<div class="question-register clearfix">
<label for="user-email"><?php _e( 'You need an account to submit question and get answers. Create one:','dwqa' ) ?></label>
<div class="register-email register-input">
<input type="text" size="20" value="" class="input" placeholder="<?php _e( 'Type your email','dwqa' ) ?>" name="user-email">
</div>
<div class="register-username register-input">
<input type="text" size="20" value="" class="input" placeholder="Choose an username" name="user-name-signup" id="user-name-signup">
</div>
<div class="login-switch"><?php _e( 'Already a member?','dwqa' ) ?> <a class="credential-form-toggle" href="<?php echo wp_login_url(); ?>"><?php _e( 'Log In','dwqa' ) ?></a></div>
</div>
<div class="question-login clearfix dwqa-hide">
<label for="user-name"><?php _e( 'Login to submit your question','dwqa' ) ?></label>
<div class="login-username login-input">
<input type="text" size="20" value="" class="input" placeholder="<?php _e( 'Type your username','dwqa' ) ?>" id="user-name" name="user-name">
</div>
<div class="login-password login-input">
<input type="password" size="20" value="" class="input" placeholder="<?php _e( 'Type your password','dwqa' ) ?>" id="user-password" name="user-password">
</div>
<div class="login-switch"><?php _e( 'Not yet a member?','dwqa' ) ?> <a class="credential-form-toggle" href="javascript:void( 0 );" title="<?php _e( 'Register','dwqa' ) ?>"><?php _e( 'Register','dwqa' ) ?></a></div>
</div>
<?php } else if ( ! is_user_logged_in() && dwqa_current_user_can( 'post_question' ) ) { ?>
//Here was the old code which had shown the E-mail input and login information
<?php }
}
This function will replace the old function
function dwqa_require_field_submit_question()
Now just remove the old function and add the new to the queue in your functions.php :)
remove_action( 'dwqa_submit_question_ui', 'dwqa_require_field_submit_question' );
add_action( 'dwqa_submit_question_ui', 'dwqa_require_field_submit_question_adjusted' );
That is it :). Maybe not the best way, but it avoids changing the plugin code.
Have a nice day
//Removes BuddyBar from non-admins only
function splen_remove_admin_bar() { if( !is_super_admin() )
add_filter( ‘show_admin_bar’, ‘__return_false’ );
}
add_action(‘wp’, ‘splen_remove_admin_bar’);

Categories