I try to set the billing email field on WooCommerce checkout to read only when a user is logged in. I tried with the following code snipped but it does not work at all
What I'm doing wrong?
add_filter( 'woocommerce_checkout_fields', 'make_billing_email_readonly' );
function make_billing_email_readonly( $fields ) {
if ( is_user_logged_in() ) {
$fields['billing']['billing_email']['custom_attributes'] = array( 'readonly' => 'readonly' );
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'make_billing_email_readonly' );
function make_billing_email_readonly( $fields ) {
if ( is_user_logged_in() ) {
$fields['billing']['billing_email']['custom_attributes']['readonly'] = 'readonly';
}
return $fields;
}
I have disabled the invoice payment mehtod for one of the user roles in my site ('customer') but now I need to add another user role ('business') to this rule and I can't figure out how to make it work. When I add the second role, the code stops working altogether and it ends up showing the gateway to all users.
Here's the code I'm using to disable the gateway:
I'm not very experienced with PHP so any help will be tremendously appreciated.
If there's any chance you can correct my code to fit the use case, I would be very grateful.
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_private' );
function payment_gateway_disable_private( $available_gateways ) {
$user = wp_get_current_user();
if ( isset( $available_gateways['igfw_invoice_gateway'] ) && !is_user_logged_in() || isset( $available_gateways['igfw_invoice_gateway'] ) && in_array('customer', $user->roles) ) {
unset( $available_gateways['igfw_invoice_gateway'] );
}
return $available_gateways;
}
Thoughts?
There is a mistake in your if statement (also you can use current_user_can() function for user roles) like:
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_private' );
function payment_gateway_disable_private( $available_gateways ) {
if ( ( ! is_user_logged_in() || current_user_can('customer') || current_user_can('business') )
&& isset( $available_gateways['igfw_invoice_gateway'] ) ) {
unset( $available_gateways['igfw_invoice_gateway'] );
}
return $available_gateways;
}
or with the global $current_user; and array_intersect() function:
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_private' );
function payment_gateway_disable_private( $available_gateways ) {
global $current_user;
// Here define your user roles
$user_roles = array( 'customer', 'business' );
if ( ( ! is_user_logged_in() || array_intersect( $current_user->roles, $user_roles ) )
&& isset( $available_gateways['igfw_invoice_gateway'] ) ) {
unset( $available_gateways['igfw_invoice_gateway'] );
}
return $available_gateways;
}
It should better work now.
When I add these lines to my code
It seems Whenever a user put an order in WooCommerce it gets fired and overwrite user_pass2 with an empty string
I expect this line of code run only when a user update the profile
function my_profile_update( $user_id ) {
if ( ! is_admin() ) {
update_user_meta($user_id, 'user_pass2', password_hash($_POST['password_1'], PASSWORD_DEFAULT));
}
}
add_action( 'profile_update', 'my_profile_update' );
We need to just check for the $_POST argument that has to be available
function my_profile_update( $user_id ) {
if ( ! is_admin() && isset($_POST['password_1']) ) {
update_user_meta($user_id, 'user_pass2', password_hash($_POST['password_1'], PASSWORD_DEFAULT));
}
}
add_action( 'profile_update', 'my_profile_update' );
please help! I'm trying to define Payment Gateway by changing order details from admin.
As a default option I want to use 'bacs' payment gateway. Customer make order and then I want to change order and turn payment method to custom 'payment2' gateway.
For this, I've made metabox with checkbox which should turn on/off 'payment2' method and unset default 'bacs'. Checkbox working properly.
But, I can't get it to work. First of all, I can't get post meta with checkbox value. Check code below please:
function show_payment2_payment_gateway( $available_gateways ) {
$use_payment2 = get_post_meta( $post->ID, 'use_payment2', true );
if($use_payment2 == "yes") {
unset( $available_gateways['bacs'] );
}
else {
unset( $available_gateways['payment2'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'show_payment2_payment_gateway', 10, 1 );
UPD
This is my code for backend checkbox. As I said it's working well and save meta value as 'yes'
//
//Adding Meta container admin shop_order pages
//
add_action( 'add_meta_boxes', 'mv_add_meta_boxes' );
if ( ! function_exists( 'mv_add_meta_boxes' ) )
{
function mv_add_meta_boxes()
{
global $woocommerce, $order, $post;
add_meta_box( 'mv_other_fields', __('PAYMENT2','woocommerce'), 'mv_add_other_fields_for_packaging', 'shop_order', 'side', 'core' );
}
}
//
//adding Meta field in the meta container admin shop_order pages
//
if ( ! function_exists( 'mv_save_wc_order_other_fields' ) )
{
function mv_add_other_fields_for_packaging()
{
global $woocommerce, $order, $post;
$meta_field_data = get_post_meta( $post->ID, 'use_payment2', true );
$meta_field_data_checked = $meta_field_data["use_payment2"][0];
if($meta_field_data == "yes") $meta_field_data_checked = 'checked="checked"';
echo '
<label for="use_epay">TURN PAYMENT2 ON?</label>
<input type="hidden" name="mv_other_meta_field_nonce" value="' . wp_create_nonce() . '">
<input type="checkbox" name="use_payment2" value="yes" '.$meta_field_data_checked.'>';
}
}
//
//Save the data of the Meta field
//
add_action( 'save_post', 'mv_save_wc_order_other_fields', 10, 1 );
if ( ! function_exists( 'mv_save_wc_order_other_fields' ) )
{
function mv_save_wc_order_other_fields( $post_id ) {
// We need to verify this with the proper authorization (security stuff).
// Check if our nonce is set.
if ( ! isset( $_POST[ 'mv_other_meta_field_nonce' ] ) ) {
return $post_id;
}
$nonce = $_REQUEST[ 'mv_other_meta_field_nonce' ];
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce ) ) {
return $post_id;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Check the user's permissions.
if ( 'page' == $_POST[ 'post_type' ] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
// --- Its safe for us to save the data ! --- //
// Sanitize user input and update the meta field in the database.
update_post_meta( $post_id, 'use_payment2', $_POST[ 'use_payment2' ] );
}
}
UPD
This is working code for Back-End (custom checkbox metabox). It save checkbox value and change payment method in order details:
//
//Adding Meta container admin shop_order pages
//
add_action( 'add_meta_boxes', 'mv_add_meta_boxes' );
if ( ! function_exists( 'mv_add_meta_boxes' ) )
{
function mv_add_meta_boxes()
{
global $woocommerce, $order, $post;
add_meta_box( 'mv_other_fields', __('PAYMENT2','woocommerce'), 'mv_add_other_fields_for_packaging', 'shop_order', 'side', 'core' );
}
}
//
//adding Meta field in the meta container admin shop_order pages
//
if ( ! function_exists( 'mv_save_wc_order_other_fields' ) )
{
function mv_add_other_fields_for_packaging()
{
global $woocommerce, $order, $post;
$meta_field_data = get_post_meta( $post->ID, 'use_payment2', true );
echo '<label for="use_payment2">USE PAYMENT2?</label>
<input type="hidden" name="mv_other_meta_field_nonce" value="' . wp_create_nonce() . '">';
if($meta_field_data == "yes") {
$meta_field_data_checked = 'checked="checked"';
echo'<input type="checkbox" name="use_payment2" value="yes" '.$meta_field_data_checked.'>';
}
else {
echo'<input type="checkbox" name="use_payment2" value="yes">';
}
}
}
//Save the data of the Meta field
add_action( 'save_post', 'mv_save_wc_order_other_fields', 10, 1 );
if ( ! function_exists( 'mv_save_wc_order_other_fields' ) )
{
function mv_save_wc_order_other_fields( $post_id ) {
// We need to verify this with the proper authorization (security stuff).
// Check if our nonce is set.
if ( ! isset( $_POST[ 'mv_other_meta_field_nonce' ] ) ) {
return $post_id;
}
$nonce = $_REQUEST[ 'mv_other_meta_field_nonce' ];
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce ) ) {
return $post_id;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Check the user's permissions.
if ( 'page' == $_POST[ 'post_type' ] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
// --- Its safe for us to save the data ! --- //
// Sanitize user input and update the meta field in the database.
$use_payment2 = sanitize_text_field($_POST[ 'use_payment2' ]);
update_post_meta( $post_id, 'use_payment2', $use_payment2 );
if($_POST[ 'use_payment2' ] == 'yes') {
update_post_meta( $post_id, '_payment_method', 'payment2' );
}
elseif (get_post_meta( $post_id, '_payment_method', true ) != 'bacs') {
update_post_meta( $post_id, '_payment_method', 'bacs' );
}
}
}
But, how I can use checkbox state on my front-end? I still can't get checkbox value using this code:
function show_payment2_payment_gateway( $available_gateways ) {
global $woocommerce, $order, $post;
$payment_method = get_post_meta( $post_id, 'use_payment2', true );
if(isset($payment_method) == 'yes') {
unset( $available_gateways['bacs'] );
}
else {
unset( $available_gateways['payment2'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'show_payment2_payment_gateway', 10, 1 );
Now, it's always showing Payment2 option even if checkbox is checked or unchecked.
Update 2 related to your comments (and your question update)
The hook your are using is a front end hook (not admin), so it will not work.
To achieve what want, you need to replace some code inside the function that is going to save your custom checkbox value when you update the order in backend (Admin) edit order pages.
So your code will be now like this:
add_action( 'save_post', 'mv_save_wc_order_other_fields', 10, 1 );
if ( ! function_exists( 'mv_save_wc_order_other_fields' ) )
{
function mv_save_wc_order_other_fields( $post_id ) {
// We need to verify this with the proper authorization (security stuff).
// Check if our nonce is set.
if ( ! isset( $_POST[ 'mv_other_meta_field_nonce' ] ) )
return $post_id;
// Passing the value to a variable
$nonce = $_REQUEST[ 'mv_other_meta_field_nonce' ];
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user's permissions.
if ( 'page' == $_POST[ 'post_type' ] ) {
if ( ! current_user_can( 'edit_page', $post_id ) )
return $post_id;
} else {
if ( ! current_user_can( 'edit_post', $post_id ) )
return $post_id;
}
// --- Its safe for us to save the data ! --- //
// Sanitize user input and update the meta field in the database.
$use_payment2 = sanitize_text_field($_POST[ 'use_payment2' ]);
update_post_meta( $post_id, 'use_payment2', $use_payment2 );
// Updating securely the data with your conditions
if($use_payment2 == 'yes')
update_post_meta( $post_id, '_payment_method', 'payment2' );
else
update_post_meta( $post_id, '_payment_method', 'bacs' );
}
}
This should work as you expect now…
Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
As this code comme from one of my answers, you are not obliged to keep the same functions beginning names with "mv_" that was related to the username of the question. You can change it to "dan_" for example…
Reference: WooCommerce : Add custom Metabox to admin order page
The function that lists default payment gateways in WooCommerce is core_gateways(). This function is hooked to a filter called woocommerce_payment_gateways. So, the first step is to remove that filter and add our own. I will work only in the functions.php file within the theme folder (remember? Never modify core files). To do so, we’ll use the remove_filter() and the add_filter() functions:
remove_filter( 'woocommerce_payment_gateways', 'core_gateways' );
add_filter( 'woocommerce_payment_gateways', 'my_core_gateways' );
Now that we have removed the filter, you can see that in the add_filter() function we have a callback named my_core_gateways. This callback is the name of a function that will replace the default core_gateways() function. This function is the one that list WooCommerce default payment gateways. I will change the content of that function and replace the call to the WC_Gateway_BACS class. This class is the bank transfer default class. Here is the code of that new function:
/**
* core_gateways function modified.
*
* #access public
* #param mixed $methods
* #return void
*/
function my_core_gateways( $methods ) {
$methods[] = 'WC_Gateway_BACS_custom';
$methods[] = 'WC_Gateway_Cheque';
$methods[] = 'WC_Gateway_COD';
$methods[] = 'WC_Gateway_Mijireh';
$methods[] = 'WC_Gateway_Paypal';
return $methods;
}
As you can see the only change I made is that I replaced WC_Gateway_BACS by WC_Gateway_BACS_custom.
Are you still with me huh? Well, to summarize, I need to remove the filter that calls the default payment gateways, and use a custom function. In this custom function, I replace the call to the BACS class, and now i need to create this new BACS class. To do so, use that code:
class WC_Gateway_BACS_custom extends WC_Gateway_BACS {
/**
* Process the payment and return the result
*
* #access public
* #param int $order_id
* #return array
*/
function process_payment( $order_id ) {
global $woocommerce;
$order = new WC_Order( $order_id );
// Mark as processing (that's what we want to change!)
$order->update_status('processing', __( 'Awaiting BACS payment', 'woocommerce' ));
// Reduce stock levels
$order->reduce_order_stock();
// Remove cart
$woocommerce->cart->empty_cart();
// Return thankyou redirect
return array(
'result' => 'success',
'redirect' => add_query_arg('key', $order->order_key, add_query_arg('order', $order->id, get_permalink(woocommerce_get_page_id('thanks'))))
);
}
}
In this snippet, I only changed the default status from “on-hold” to “processing”…. and boom the magic appears! Now each order paid using the BACS payment gateway will be marked as processing, not as on hold.
After few days of headache I found easy way how to show defined payment gateway only when I send link to customer.
Now customer can make order with default 'bacs' method, and Admin can check it before payment. Then admin change order status to Waiting for payment and link sends to customer. When customer opens link, my custom payment gateway becomes active.
I decided to use woocommerce endpoints to check if it 'order-pay' page. I used code below:
function show_payment2_payment_gateway( $available_gateways ) {
global $woocommerce, $order, $post;
if (is_wc_endpoint_url( 'order-pay' )) {
unset( $available_gateways['bacs'] );
}
else {
unset( $available_gateways['payment2'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'show_payment2_payment_gateway', 10, 1 );
Now it works exactly as I wanted before. I hope this will be useful. Thanks to #LoicTheAztec for help!
I am looking for a way to disable the coupon field for wholesalers in WooCommerce on the cart and checkout pages. I am using a custom theme and have the WooCommerce Role Based Methods plug-in in conjunction with WooCommerce Wholesale Pricing. I have tried the following in functions.php:
// hide coupon field on cart page for wholesale
function hide_coupon_field_on_cart( $enabled ) {
if( ! current_user_can( 'wholesale_customer' ) && is_cart() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_cart' );
// hide coupon field on checkout page for wholesale
function hide_coupon_field_on_checkout( $enabled ) {
if( ! current_user_can( 'wholesale_customer' ) && is_checkout() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_checkout' );
But that didn't work. What am I doing wrong?
Try this:
function woo_get_user_role() {
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
// hide coupon field on cart page for wholesale
function hide_coupon_field_on_cart( $enabled ) {
if(woo_get_user_role() =='wholesale_customer' && is_cart() || is_checkout() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_cart' );
You can also merge both functions into one.
Try this:
$current_user = wp_get_current_user();
if ( !($current_user instanceof WP_User) )
return true;
$roles = $current_user->roles;
foreach($roles as $role){
// wholesaler is your role name, not display name
if( $role == "wholesaler" ){
$isWholesalers = 1;
}
}
if(!isset($isWholesalers)){
return true;
}
if(is_cart()/*is_checkout*/ )
return false;