For edit woocommerce (WordPressss plugin) account page fields and make to read-only, easy edit woocommerce/templates/myaccount/form-edit-account.php file and only add read-only to end of fields tags end (sorry my English and programming is not professional and good)
But for edit woocommerce checkout page fields and make one, or two and more fields, for example, email field, to read-only, cannot do the previous way
Please help me to do it, I have not idea.
It would be much better if, teach me to do it from functions.php easy and better and durable way (if it is possible).
thank you again
You can use the following code in your theme's "function.php" file.
add_action('woocommerce_checkout_fields','customization_readonly_billing_fields',10,1);
function customization_readonly_billing_fields($checkout_fields){
$current_user = wp_get_current_user();;
$user_id = $current_user->ID;
foreach ( $checkout_fields['billing'] as $key => $field ){
if($key == 'billing_address_1' || $key == 'billing_address_2'){
$key_value = get_user_meta($user_id, $key, true);
if( strlen($key_value)>0){
$checkout_fields['billing'][$key]['custom_attributes'] = array('readonly'=>'readonly');
}
}
}
return $checkout_fields;
}
This function will check if the address fields have value (i.e. if the address is specified), and if it has value, makes the field/s readonly. Else keeps the fields open to add data for the user. You can apply the same concept for each required fields.
Hope this helps.
Thank You!
very thank you for answer mr amritosh pandey
this code work correctly
for only email field , remove:
if($key == 'billing_address_1' || $key == 'billing_address_2'){
and i change to
if($key == 'billing_email'){
and work perfect and correctly
my new queston is > if use for two field , example: mobile (tel) and email or more field
Should i repeat all codes?
from add_action to return $checkout_fields; }
or can repeat
if($key line 2 or 3 or more?
example:
...
...
if($key == 'billing_email'){
if($key == 'billing_phone'){
if($key == 'billing_postcode'){
...
...
thank you
Related
My every product in WooCommerce has several attributes. One of them is named my-availability and I need to change its value dynamically depending on stock change and some condition logic. So when the amount of pieces on stock is changed (usually decreased by order), the value of my-availability attribute will change. It should not add any new attributes, just change the value of the existing one. I do not use product variations at all.
I am total beginner and trying to build this piece of code for few days using a lot of googling. Now I have something like this:
function changeatt( $order )
{
$items = $order->get_items();
foreach( $items as $item ) {
$value2 = get_field( "naceste", $item['product_id']); //need to get value of custom field "naceste"
$value = get_post_meta( $item['product_id'], '_stock', true ); //need to get how many pcs is on stock
if ($value == 0 && $value2 > 0)
{$avl = 'Coming soon';}
if ($value == 0 && $value2 == 0)
{ $avl = 'Not in stock';}
if ($value > 0)
{ $avl = 'In stock';}
// update_post_meta( $item['product_id'], 'my-availability', $avl );
wp_set_object_terms( $item['product_id'], $avl, 'pa_my-availability', false);
}
}
add_action( 'woocommerce_reduce_order_stock', changeatt );
This code actually creates new custom field my-availability with the correct value, but I need to save it to existing attribute named my-availability instead. What am I missing?
I found it! update_post_meta did not work for changing attributes, do not know why. wp_set_object_terms must be used instead and pa_ must be added to the name of the attribute. I have edited the code in the question to the working version. The previous not-working line has been removed by // for other beginners, who will be solving the same situation to learn from this one. It took me few days to solve this.
Now I am pretty curious, how to find out, which types of variables can be changed using update_post_meta and which types using wp_set_object_terms.
I have some e-commerce website where the customer billing address is predefined on the back-end.
I need to set the "Billing Address" fields as 'readonly' to avoid the customer to replace the information placed there... but i donĀ“t know how/where to do it...
Is it possible?
Put following code in your theme's "function.php" file.
add_action('woocommerce_checkout_fields','customization_readonly_billing_fields',10,1);
function customization_readonly_billing_fields($checkout_fields){
$current_user = wp_get_current_user();;
$user_id = $current_user->ID;
foreach ( $checkout_fields['billing'] as $key => $field ){
if($key == 'billing_address_1' || $key == 'billing_address_2'){
$key_value = get_user_meta($user_id, $key, true);
if( strlen($key_value)>0){
$checkout_fields['billing'][$key]['custom_attributes'] = array('readonly'=>'readonly');
}
}
}
return $checkout_fields;
}
This function checks if the address fields have value (i.e. if the address is specified), and if it has value, makes the field/s readonly. Else keeps the fields open to add data for user.
Hope this helps.
You have not specified which form you want to customize making the billing address fields read-only. Normally the billing address fields appear on two types of forms on a WooCommerce site:
On a checkout form
On a my-account/edit-address/billing/ page
If your case is the first one, then zipkundan's answer is the best one. But if your case is the second one, then copy and paste the following code to your active theme's (or child theme if any) functions.php file:
add_filter('woocommerce_address_to_edit', 'cb_woocommerce_address_to_edit');
function cb_woocommerce_address_to_edit($address){
array_key_exists('billing_first_name', $address)?$address['billing_first_name']['custom_attributes'] = array('readonly'=>'readonly'):'';
array_key_exists('billing_last_name', $address)?$address['billing_last_name']['custom_attributes'] = array('readonly'=>'readonly'):'';
array_key_exists('billing_email', $address)?$address['billing_email']['custom_attributes'] = array('readonly'=>'readonly'):'';
array_key_exists('billing_email-2', $address)?$address['billing_email-2']['custom_attributes'] = array('readonly'=>'readonly'):'';
return $address;
}
The above code will make the following fields read-only:
Billing first name
Billing last name
Billing email address
Billing confirm email address
Array keys for other form fields on the same page are as follows:
billing_company
billing_country
billing_address_1
billing_address_2
billing_city
billing_state
billing_postcode
billing_phone
Additionally, you can make the read-only fields appear slightly faded out. So, add the following CSS to your theme's style.css
.woocommerce-address-fields input[readonly="readonly"]{
opacity: 0.5;
}
$checkout_fields['billing'][$key]['custom_attributes'] = array('readonly'=>'readonly');
this solves the problem
This one worked for me.
https://www.sitekickr.com/snippets/woocommerce/make-checkout-field-read
add_filter('woocommerce_billing_fields', 'my_woocommerce_billing_fields');
function my_woocommerce_billing_fields($fields)
{
$fields['billing_first_name']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_last_name']['custom_attributes'] = array('readonly'=>'readonly');
return $fields;
}
I have a ticket support form on my site which right now has a field which returns (in the admin area) the name of the person who submitted the form.
Anyone know how I would modify this to display their user role instead? ie. Subscriber, Editor, etc.
$raised_by='';
if($ticket->type=='user'){
$user=get_userdata( $ticket->created_by );
$raised_by=$user->display_name;
}
I'm guessing it'll be something with this stuff in it...but I'm not too savy when it comes to this.
function get_user_role() {
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
Please change last line of your code to this:
$raised_by=ucwords($user->roles[0]);
So that your current code which display First Name i.e.
$raised_by='';
if($ticket->type=='user'){
$user=get_userdata( $ticket->created_by );
$raised_by=$user->display_name;
}
Above code will become:
$raised_by='';
if($ticket->type=='user'){
$user=get_userdata( $ticket->created_by );
$raised_by=ucwords($user->roles[0]);
}
Update: To remove underscore with space your code may become as:
$raised_by='';
if($ticket->type=='user'){
$user=get_userdata( $ticket->created_by );
$raised_by= ucwords(str_replace("_"," ",$user->roles[0]));
}
You may notice, I have added ucwords function of PHP also, it is to make sure , roles on the screen look good, i.e. admin will be shown as Admin etc.
Also you may notice roles[0], 0 means that data currently we have there is as an array. So we are picking the first user roles from all the roles assigned to the user. I am sure it will be sufficient for your needs.
Let me know if this solves your issue or you still need any help. You can post in comments. Or Update your question.
You could use this line of code.
$raised_by='';
if($ticket->type == 'user'){
$user = get_userdata( $ticket->created_by );
$raised_by = implode(', ', $user_info->roles);
}
Or, if you prefer to use the get_user_role function that you've written,
slightly modify it to take the user ID as input and return the user role.
function get_user_role($user_id) {
$user_info = get_userdata($user_id);
$user_roles = $user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
You could use it like as shown below to output the user role.
$raised_by='';
if($ticket->type == 'user'){
$user = get_userdata( $ticket->created_by );
$raised_by = get_user_role($user->ID);
}
I was wondering if there is a way to separate my processing orders email via the purchased products category.
I know that's not very clear :P
For example:
I have two products. Test1 and Test2. Test1 has the category 'ICT Sale', while Test2 has 'Private Sale'.
If someone purchases Test1 I would like the email to say 'Thank you for buying an ICT product'
If someone purchases Test2 I want it to say 'This is a private sale!'
Unfortunately I'm more fluent with JS than PHP and WP hooks (and I'm assuming a if / else statement won't work here :P)
Thanks for any help in advance!
You can copy the email template in your theme and then do what ever you want.
For exemple, copy woocommerce/templates/emails/customer-processing-order.php to themes/yourtheme/woocommerce/emails/customer-processing-order.php
then edit customer-processing-order.php something like this should to your trick (you need to complete then to display whatever you want)
$ict = false;
$private = false;
foreach ( $order->get_items() as $item ){
$categories = get_the_terms( $item['product_id'] , 'product_cat' );
foreach( $categories as $categorie ) {
if ($categorie->slug == 'ICT') {
$ict = true;
}elseif( $categorie->slug == 'private-sale-slug' ) {
$private = true;
}
}
}
Hi bro Please go to wocommerce product page (wp-admin/edit.php?post_type=product) and
click on the quickedit of test2 product. Here see you selected checkbox on private so
comout message ('This is a private sale!') when purchage product test2 .please remove
selected checkbox on private.
In wordpress, I need to program it such that anytime someone enters or updates a post meta called "start_date", a bit of code is run on what is entered before it is saved.
I need to take what is entered and convert it to a unix timestamp.
Is there a way to do this?
If not, is there a way to add the code on publish or update of the post such that it checks for that meta and updates it if needed?
Assuming you're creating the metaboxes and custom fields with your plugin, you can do the following. Otherwise, it depends on how their saving the data as it could overwrite yours.
Here's something to get you started though depending on what the case is.
add_action('save_post', 'update_the_post_meta', 100, 2);
function update_the_post_meta($post_id, $post) {
if ( defined('DOING_AJAX') && DOING_AJAX ) { return; }
if ( defined('DOING_CRON') && DOING_CRON ) { return; }
if ($post->post_type == 'revision') { return; }
if ( isset($_REQUEST['start_date']) ) :
//do your timestamp code here and save it in $timestamp
add_post_meta($post_id, 'start_date', $timestamp, true) or update_post_meta($post_id, 'start_date', $timestamp);
else :
delete_post_meta($post_id, 'start_date');
endif;
}
Right now the priority of the add_action is set to 100 (the higher the number, the less priority it has). So, if you're trying to override someone else's function, you may need to increase the priority number. Also, this is assuming the name of the input field is "start_date".