I try to get my coupon field hidden but I would like to keep it on for logged-in users.
On this code, it does not work.
What can be my solution?
function hide_coupon_field_on_cart( $enabled ) {
if( !is_user_logged_in() )
return $disabled;
if ( is_cart() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_cart' );
function hide_coupon_field_on_cart( $enabled ) {
if( !is_user_logged_in() ){
return false;
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_cart' );
Related
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 want to allow backorders for specific customer roles.
Code i writed is:
// BackOrder Allow Part #1
add_filter( 'woocommerce_product_backorders_allowed', 'woocommerce_product_backorders_allowed', 10, 3 );
function woocommerce_product_backorders_allowed( $backorder_allowed, $product_id, $product ){
if ( current_user_can('administrator') ) {
$backorder_allowed = true;
} else {
$backorder_allowed = false;
}
return $backorder_allowed;
}
// BackOrder Allow Part #2
add_filter( 'woocommerce_product_get_stock_status', 'woocommerce_product_get_stock_status', 10, 2 );
function woocommerce_product_get_stock_status( $stock_status, $product ) {
if ( current_user_can('administrator') ) {
return 'onbackorder';
}
else {
return 'outofstock';
}
}
Seems this gived result only for simple product.
How i can make work for other product types.
I need to work for simple, variable and yith-bundle.
Any help will be appreciated!
thanks!
There are some mistakes and missing things in your code. Use the following instead:
// Custom conditional function targeting specific user roles
function is_allowed_user_role() {
$targeted_roles = array('administrator', 'shop_manager'); // Here define your targeted user roles
return (bool) array_intersect( wp_get_current_user()->roles, $targeted_roles );
}
add_filter( 'woocommerce_product_get_stock_status', 'filter_product_stock_status' );
add_filter( 'woocommerce_product_variation_get_stock_status', 'filter_product_stock_status' );
function filter_product_stock_status( $stock_status ) {
if ( ! is_allowed_user_role() && 'onbackorder' === $stock_status ) {
$stock_status = 'outofstock';
}
return $stock_status;
}
add_filter( 'woocommerce_product_get_backorders', 'filter_product_get_backorders' );
add_filter( 'woocommerce_product_variation_get_backorders', 'filter_product_get_backorders' );
function filter_product_get_backorders( $backorders ){
return is_allowed_user_role() ? $backorders : 'no';
}
add_filter( 'woocommerce_product_backorders_allowed', 'filter_product_backorders_allowed', 10, 3 );
function filter_product_backorders_allowed( $allowed, $product_id, $product ){
return is_allowed_user_role() ? $allowed : false;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
I did change on second function. If it's outofstock, like qty = 0 or stockstatus be outofstock, to allow backorder
// Custom conditional fuction to target specific user roles
function is_allowed_user_role() {
$targeted_roles = array('administrator', 'shop_manager'); // Here define your targeted user roles
return (bool) array_intersect( wp_get_current_user()->roles, $targeted_roles );
}
add_filter( 'woocommerce_product_get_stock_status', 'filter_product_stock_status' );
add_filter( 'woocommerce_product_variation_get_stock_status', 'filter_product_stock_status' );
function filter_product_stock_status( $stock_status ) {
if ( is_allowed_user_role() && 'outofstock' === $stock_status ) {
$stock_status = 'onbackorder';
}
return $stock_status;
}
add_filter( 'woocommerce_product_get_backorders', 'filter_product_get_backorders' );
add_filter( 'woocommerce_product_variation_get_backorders', 'filter_product_get_backorders' );
function filter_product_get_backorders( $backorders ){
return is_allowed_user_role() ? $backorders : 'no';
}
add_filter( 'woocommerce_product_backorders_allowed', 'filter_product_backorders_allowed', 10, 3 );
function filter_product_backorders_allowed( $allowed, $product_id, $product ){
return is_allowed_user_role() ? $allowed : false;
}
I'd like to exclude admin from some changes to my site but not sure how to do it.
I have this php:
add_action('wp_dashboard_setup',
'wpse_73561_remove_all_dashboard_meta_boxes', 9999
);
function
wpse_73561_remove_all_dashboard_meta_boxes()
{
global $wp_meta_boxes;
$wp_meta_boxes['dashboard']['normal']['core'] = array();
$wp_meta_boxes['dashboard']['side']['core'] = array();
}
This works to apply changes in general but to exclude admin I tried this and added it to the first php:
add_action( 'admin_head', 'wpso_add_admin_custom_css' );
function wpso_add_admin_custom_css() {
// Bail if Admin.
if ( current_user_can( 'manage_options' ) ) {
return;
}
So what's wrong with code:
add_action( 'admin_head', 'wpso_add_admin_custom_css' );
function wpso_add_admin_custom_css() {
// Bail if Admin.
if ( current_user_can( 'manage_options' ) ) {
return;
>?
add_action('wp_dashboard_setup',
'wpse_73561_remove_all_dashboard_meta_boxes', 9999
);
function
wpse_73561_remove_all_dashboard_meta_boxes()
{
global $wp_meta_boxes;
$wp_meta_boxes['dashboard']['normal']['core'] = array();
$wp_meta_boxes['dashboard']['side']['core'] = array();
}
You can use is_admin() to only apply those actions to admin, or non-admin, requests. https://codex.wordpress.org/Function_Reference/is_admin
Eg:
if (!is_admin()) {
add_action( 'admin_head', 'wpso_add_admin_custom_css' );
function wpso_add_admin_custom_css() {
// ...
}
}
You can check user access with current_user_can() and bail if admin.
function wpse_73561_remove_all_dashboard_meta_boxes() {
global $wp_meta_boxes;
if ( current_user_can( 'manage_options' ) ) {
return;
}
$wp_meta_boxes['dashboard']['normal']['core'] = array();
$wp_meta_boxes['dashboard']['side']['core'] = array();
}
add_action( 'wp_dashboard_setup', 'wpse_73561_remove_all_dashboard_meta_boxes', 9999 );
I've created a custom WooCommerce checkout field with Woothemes Checkout Field Editor labeled "po_number". I would like the PO Number checkout field to only display for the user role "distributor".
So far I've been unsuccessful in overriding the checkout fields. I'm using Wordpress 4.5.1 / Woocommerce 2.5.5. Here's the code I've placed in my child theme's functions.php. I've also tested to make sure it is not a theme conflict.
Any help is greatly appreciated.
This is my code:
function custom_override_checkout_fields( $fields ) {
if ( ! current_user_can( 'distributor' ) && isset( $fields['billing']['po_number'] ) ) {
unset($fields['billing']['po_number']);
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
The current_user_can() function is related to capabilities of the user roles, but not to detect the user roles themselves. For that reason is not working in your code.
You need to set a conditional function for this purpose (user roles):
function is_user_role( $role, $user_id = null ) {
if ( is_numeric( $user_id ) ) {
$user = get_userdata( $user_id );
} else {
$user = wp_get_current_user();
}
if ( empty( $user ) ) {
return false;
}
if ( in_array( $role, (array) $user->roles ) == 1) {
return true;
} else {
return false;
}
}
Then in your code you can use that function:
function custom_override_checkout_fields( $fields ) {
if ( !is_user_role( 'distributor' ) && isset( $fields['billing']['po_number'] ) ) {
unset($fields['billing']['po_number']);
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
This should work in your code.
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;