Alright, I've been pulling my hair out the last couple of days trying to figure this out.
I have a wholesale plugin in wordpress install with woocommerce. It gives the user "wholesale_customer" special rates over everyone else. I want to be able to offer local delivery to only the "wholesale_customer" user role but can't seem to figure out how to do it.
I've gotten this code from #mcorkum but it's still not working.
/**
* Add local delivery for wholesale customers
*/
function wholesale_local_delivery($available_methods) {
global $woocommerce;
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
if ( isset( $available_methods['local_delivery'] ) ) {
if ($user_role == 'wholesale_customer' ) {
unset( $available_methods['local_delivery'] );
}
}
return $available_methods;
}
add_filter( 'woocommerce_package_rates', 'wholesale_local_delivery', 10, 1);
I know this achievable with a plugin, but I'd rather not use plugins or pay for it for that matter.
Does anyone see anything that I'm not seeing?
/**
* Add local delivery for wholesale customers
*/
function wholesale_local_delivery($available_methods) {
global $woocommerce;
global $current_user;
if ( isset( $available_methods['local_delivery'] ) ) {
if ( !current_user_can( 'wholesale_customer' ) ) {
unset( $available_methods['local_delivery'] );
}
}
return $available_methods;
}
add_filter( 'woocommerce_package_rates', 'wholesale_local_delivery', 10, 1);
Try the above code by pasting it in your theme's functions.php file. And let me know if this worked for you.
I'm not a wordpress dev, but that code doesn't look like it is providing a user with role "wholesale_customer" the "local_delivery" option. On the contrary in fact, it looks to be removing the local delivery option if the user role IS "wholesale_customer":
if ( isset( $available_methods['local_delivery'] ) ) {
if ($user_role == 'wholesale_customer' ) {
unset( $available_methods['local_delivery'] );
}
}
If I was to take this code simply at face value (As I am not a wordpress dev) I would re-write this function to be easier to understand and read:
function wholesale_local_delivery($available_methods)
{
global $woocommerce;
global $current_user;
// Return early if no local delivery option is available
if (!isset($available_methods['local_delivery'])) {
return $available_methods;
}
// Determine if the user has a user role of wholesale customer
$hasRoleWholeSaleCustomer = false;
foreach ($current_user->roles as $role) {
if ($role === 'wholesale_customer') {
$hasRoleWholeSaleCustomer = true;
break;
}
}
// If the user does not have the role wholesale customer
// And for the code here to be being processed the local delivery
// option must be available
if (!$hasRoleWholeSaleCustomer) {
unset($available_methods['local_delivery']);
}
// Return the available methods applicable to the users roles
return $available_methods;
}
Hope someone else with experience in woocomerce, can give a better answer. But in the meantime, you can try this re-write and see if it works for you.
Goodluck.
Related
I have created a new role in WordPress for WooCommerce. The only thing I require this role to see is:
WooCommerce --> Orders Only
Users - All Submenus
WP All Import - All Submenus except for Settings
WP All Export - All Submenus except for Settings
I successfully dwindled down to the list but am having issues isolating the WooCommerce Dashboard sub-menu.
I used this code to get a full list:
if (!function_exists('debug_admin_menus')):
function debug_admin_menus() {
if ( !is_admin())
return;
global $submenu, $menu, $pagenow;
if ( current_user_can('manage_options') ) { // ONLY DO THIS FOR ADMIN
if( $pagenow == 'index.php' ) { // PRINTS ON DASHBOARD
echo '<pre>'; print_r( $menu ); echo '</pre>'; // TOP LEVEL MENUS
echo '<pre>'; print_r( $submenu ); echo '</pre>'; // SUBMENUS
}
}
}
add_action( 'admin_notices', 'debug_admin_menus' );
endif;
I found that component being: wc-admin where I added:
remove_submenu_page('woocommerce', 'wc-admin');
BUT it isn't removing it. Does anyone know why? I've also tried:
add_action( 'admin_menu', 'remove_menu_pages', 999);
function remove_menu_pages() {
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
if($user_role == "custom_shop_admin") {
$remove_submenu = remove_submenu_page('woocommerce', 'wc-admin');
}
}
Everywhere I've looked up has ('woocommerce', 'wc-admin') as the dashboard. Not sure if I can't remove it, to redirect it to the orders page perhaps for that role only. I know there has been a recent WooCommerce Update so not sure if that has anything to do with it.
Thank you!
I know its been a long time no talk here. I had the same problem. I have figured it out and it may help people out in the future to solve the problem really quick. Here is the solution.
function remove_menus_custom(){
global $current_user;
if(is_admin(){
remove_submenu_page('woocommerce','wc-admin');
}
}
add_action( 'admin_menu', 'remove_menus_custom', 999 );
When customer click cancel subscription, they still can logged in the my account section, but I want to let user cannot access anything on my account section. From this point, I would like to make the function on "if the user cancels payment, it also delete the account too" to make them cannot access, so I try to investigate and write the code below.
add_action( 'woocommerce_order_status_cancelled',
'custom_woocommerce_auto_delete_user' );
function custom_woocommerce_auto_delete_user( $order_id ) {
global $woocommerce;
$order = new WC_Order( $order_id );
$order_status = $order->get_status();
if ( !$order_id )
return false;
if ('cancelled' == $order_status) {
$current_user = wp_get_current_user();
wp_delete_user( $current_user->ID,true );
return true;
}
return false;
}
However, I'm just the beginner of woocommerce and I don't know which solution is the best for this issue. I'm so glad for anyone that come to response to me :)
I want to delete the users who are created when trying to buy a subscription but the payment gets failed although the user anyhow creates. I am just to confirm if this code works for Me or not?
add_action('woocommerce_subscription_status_failed', 'custom_woocommerce_subscription_status_failed');
function custom_woocommerce_subscription_status_failed($order_id)
{
global $woocommerce;
$order = new WC_Order( $order_id );
$order_status = $order->get_status();
if ($order_status`enter code here` == 'failed') {
$current_user = wp_get_current_user();
wp_delete_user( $current_user->ID,true );
return true;
}
return false;
}
In WooCommerce, I'm trying to apply set_is_vat_exempt() method for customer and guest user.
For logged customer it is working fine. Can anyone suggest how can I do it?
One issue could be like as the user is not logged in so may be $woocommerce->customer could not enable it.
Here's my code:
$bilalId = get_current_user_id();
add_filter( 'woocommerce_cart_totals_order_total_html', 'wc_cart_totals_order_total_html_bn');
function wc_cart_totals_order_total_html_bn() {
global $woocommerce;
if( current_user_can('customer' || $bilalId == 0) ) {
$woocommerce->customer->set_is_vat_exempt(true);
}
}
At the end, I just want to disable any tax rates for non-logged in user.
even I tried "Zero rate" but doesn't worked for me.
Any kind of guidance would be appreciated.
Thanks.
So what you need is to use the wordpress conditional is_user_logged_in() in a custom function hooked in init action hook, this way:
add_action( 'init', 'wc_tax_exempt_unlogged' );
function wc_tax_exempt_unlogged() {
// Getting user data for logged users
if( is_user_logged_in() ){
$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;
$current_user_roles = $current_user->roles;
$bilal_id = 0;
}
// Exempting of VAT non logged users, customers and the main admin ID (you)
if( ! is_user_logged_in() || in_array( 'customer', $current_user_roles ) || $bilal_id == $current_user_id ){
WC()->customer->set_is_vat_exempt(true);
}
}
The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.
Hey guys I have a cash on delivery payment method on my wordpress/woocomerce website that I want to hide from the customer user role and non-logged in users.
I've been searching up and down and the only thing I found close was this bit of code.
function paypal_disable_manager( $available_gateways )
{global $woocommerce;
if ( isset( $available_gateways['paypal'] ) && current_user_can('customer') ) {
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways','paypal_disable_manager' );
Would someone be able to help me modify this code to make it work for my use. Thank you in advance!
Have mention the code which is tried and tested for you. It works well. Lemme know if the same works for you too.
function wdm_disable_cod( $available_gateways ) {
//check whether the avaiable payment gateways have Cash on delivery and user is not logged in or he is a user with role customer
if ( isset($available_gateways['cod']) && (current_user_can('customer') || ! is_user_logged_in()) ) {
//remove the cash on delivery payment gateway from the available gateways.
unset($available_gateways['cod']);
}
return $available_gateways;
}
add_filter('woocommerce_available_payment_gateways', 'wdm_disable_cod', 99, 1);
<?php
//--- Filter for remove any payment gateway as per the user role selected --
add_filter('woocommerce_available_payment_gateways','filter_gateways',1);
function filter_gateways($gateways){
global $woocommerce, $current_user;
if ( is_user_logged_in() ) {
$userRole = implode(',',$current_user->roles);
if($userRole == 'my_user_role'){
//-- Remove casho on delivery if following user have logged in
unset($gateways['cod']);
}
}else{
//-- Hide COD if user not logged in
unset($gateways['cod']);
}
return $gateways;
}
?>
//-- Try this one, I have already make use of this code against minimum order limit
I am trying to hide some payment options in Woocommerce in case of specific delivery option. I tried to put this to my functions.php but It´s not working and I don´t know why.
Can you help me please?
function payment_gateway_disable_country( $available_gateways, $available_methods )
{
global $woocommerce;
if ( isset( $available_methods['local_delivery'] ) ){
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );
My research:
link 1
link 2
link 3
link 4
The available delivery methods do not get passed as a parameter in the filter woocommerce_available_payment_gateways - you need to load them in and check them.
The code below should remove the paypal payment option one if the user selects local delivery. If your checkout page is the AJAX based one then as the user changes the delivery method, the available payment options should also change.
function payment_gateway_disable_country($available_gateways) {
global $woocommerce;
$packages = $woocommerce->shipping->get_packages();
foreach ( $packages as $i => $package ) {
$chosen_method = isset( $woocommerce->session->chosen_shipping_methods[ $i ] ) ?
$woocommerce->session->chosen_shipping_methods[ $i ] : '';
if ('local_delivery' == $chosen_method) {
unset($available_gateways['paypal']);
break;
}
}
return $available_gateways;
}
add_filter(
'woocommerce_available_payment_gateways',
'payment_gateway_disable_country'
);
Let me know if you have any issues with the code; I did not have a chance to test it with woocommerce.
$available_methods will not be accessible inside your function. So first define it globally & access as global variable inside function, somewhat like this:
global $available_methods;
$available_methods = array( 'local_delivery' => 'yes' );
function payment_gateway_disable_country( $available_gateways )
{
global $woocommerce, $available_methods;
if ( isset( $available_methods['local_delivery'] ) ){
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );