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.
Related
Following the woocommerce documentation, I added an endpoit to my-account page in woocommerce.
I want to make this endpoint visible only to a specific user role, lets say shop_manager.
Is there a way to redirect to a 404 page users who try to access directly that endpoint?
Thanks in advance.
Assuming that you have already created a custom endpoint to my account section (see this related answer), you can redirect all non allowed user roles to a specific page using template_redirect hook in this simple way:
add_action( 'template_redirect', 'custom_endpoint_redirection' );
function custom_endpoint_redirection() {
$allowed_user_role = 'administrator';
$custom_endpoint = 'my-custom-endpoint';
if ( is_wc_endpoint_url($custom_endpoint) && ! current_user_can($allowed_user_role) ) {
$redirection_url = home_url( '/404/' );
wp_redirect( $redirection_url );
exit;
}
}
You need to specify your custom end point, your allowed user role and the url redirection.
Code goes in functions.php file of your active child theme (or active theme). It could works.
Related:
WooCommerce - Assign endpoints to multiple custom templates in my-account page
WooCommerce: Assigning an endpoint to a custom template in my account pages
WooCommerce: Adding custom template to customer account pages
Custom my account new menu item for a specific user role in Woocommerce
Just add the follows code snippet in your active theme's functions.php and this is only for administrator user role, you can change it as per you -
function add_custom_my_account_endpoint() {
add_rewrite_endpoint( 'shop_manager', EP_PAGES );
}
add_action( 'init', 'add_custom_my_account_endpoint' );
function add_custom_wc_menu_items( $items ) {
$user = wp_get_current_user();
if( $user && in_array( 'administrator', $user->roles ) ){
$items[ 'shop_manager' ] = __( 'Shop Manager', 'text-domain' );
}
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'add_custom_wc_menu_items' );
function add_shop_manager_endpoint_content(){
$user = wp_get_current_user();
if( $user && !in_array( 'administrator', $user->roles ) ) return;
echo "Your content goes here";
}
add_action( 'woocommerce_account_shop_manager_endpoint', 'add_shop_manager_endpoint_content' );
After this just flush_rewrite_rules from Backend Settings > Permalinks. Thats it.
I am able to update status using this code
In this image highlighted text is username of currently logged in user, when I changed status from dashboard it shows me name, but when I change status using code it won't show any name.
I want username should be display like in this screenshot:
add_filter('woocommerce_new_order_note_data', 'modify_added_by');
function modify_added_by($args) {
$user = get_user_by('id', get_current_user_id());
$comment_author = $user->display_name;
$comment_author_email = $user->user_email;
$args['comment_author'] = $comment_author;
$args['comment_author_email'] = $comment_author_email;
}
Try this code
You can use the following hooked function to get the shop manager user name in the order note:
add_filter( 'woocommerce_new_order_note_data', 'filter_woocommerce_new_order_note_data', 10, 2 );
function filter_woocommerce_new_order_note_data( $args, $args2 ) {
if( ! $args2['is_customer_note'] && is_user_logged_in() && current_user_can( 'edit_shop_order', $args2['order_id'] ) ){
$user = get_user_by( 'id', get_current_user_id() );
$args['comment_author'] = $user->display_name;
$args['comment_author_email'] = $user->user_email;
}
return $args;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Related thread: Add the Shop Manager username to Woocommerce Admin Order notes
There is a site on WordPress and WooCommerce. At the moment, I already have functionality for several conditions when buying products by users:
If the user is not registered and he buys any product other than the subscription, he calmly draws up the order and the system automatically creates an account for him. (Done)
If the user is registered but forgot to enter under his login, he also quietly draws up an order simply by logging in under his login on the checkout page. (Done)
If the user is not registered and wants to buy a subscription, the system redirects it to the registration page, from there to the edit-account page and he goes back to the checkout page. (Done)
If the user is registered but forgot to log in with his login, and he wants to buy a subscription. The user redirects to the registration page. He enters login and password. The system redirects back him to the checkout page. (A task)
The first two conditions I created are standard WooCommerce settings. For the third condition, I have code that can be seen:
add_action('template_redirect', 'woo_custom_redirect');
function woo_custom_redirect($redirect) {
// HERE set your product category (can be term IDs, slugs or names)
$category = 'subscriptions';
$found = false;
// CHECK CART ITEMS: search for items from our product category
foreach(WC()->cart->get_cart() as $cart_item) {
if (has_term($category, 'product_cat', $cart_item['product_id'])) {
$found = true;
break;
}
}
if (!is_user_logged_in() && is_checkout() && $found) {
wp_redirect('/my-account/edit-account/');
exit();
}
if (is_user_logged_in() && !WC()->cart->is_empty() && is_account_page()) {
wp_redirect( get_permalink( get_option('woocommerce_checkout_page_id') ) );
}
}
For the fourth condition, I added the code:
if (is_user_logged_in() &&! WC()->cart->is_empty() && is_account_page ()) {
wp_redirect (get_permalink (get_option ('woocomm erce_checkout_page_id'))));
}
But it does not work as it should. The third condition stops working because of this code.
How can this be remedied? Thanks!
Update: I save it here. This can help other users. The code is fully working.
add_action( 'template_redirect', 'custom_redirections' );
function custom_redirections( $redirect ) {
// HERE set your product category (can be term IDs, slugs or names)
$category = 'subscriptions';
$found = false;
// CHECK CART ITEMS: search for items from our product category
foreach( WC()->cart->get_cart() as $cart_item ) {
if (has_term( $category, 'product_cat', $cart_item['product_id'] ) ) {
$found = true;
break;
}
}
if ( ! is_user_logged_in() && is_checkout() && $found ) {
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
exit();
} elseif ( is_user_logged_in() && is_account_page() && $found ) {
wp_redirect( get_permalink( get_option('woocommerce_checkout_page_id') ) );
exit();
}
}
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.
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