• Resolved jimbo777

    (@jimbo777)


    Hi everyone,

    Huge WooCommerce fan and supporter here. It’s the only shopping solution I have been recommending for years now A+++ 5 stars!!

    Currently, I am building a new site for a client and just need a little help trying to get one of these code snippets below to work with the Members plugin which I am using to assign roles to customers. All I have is 1 Role which is the Dealer Role which I am trying to make tax exempt.

    1. This code did not work for me.
    https://gist.github.com/kloon/5948116

    // Place the following code in your theme’s functions.php file and replace tax_exempt_role with the name of the role to apply to
    
    add_action( ‘init’, ‘woocommerce_customer_tax_exempt’ );
    function woocommerce_customer_tax_exempt() {
    global $woocommerce;
    if ( is_user_logged_in() ) {
    $tax_exempt = current_user_can( ‘dealer’);
    $woocommerce->customer->set_is_vat_exempt( $tax_exempt );
    }
    }

    Next, I also created a Zero Tax rate in WooCommerce and tried this code:

    2. This code did not work for me either

    /* No tax for dealer */
    function zero_rate_for_custom_user_role( $tax_class, $product ) {
    // Getting the current user
    $current_user = wp_get_current_user();
    $current_user_data = get_userdata($current_user->ID);
    
    // <== <== <== <== <== <== <== Here you put your user role slug
    if ( in_array( ‘dealer’, $current_user_data->roles ) )
    $tax_class = ‘Zero Rate’;
    
    return $tax_class;
    }
    add_filter( ‘woocommerce_product_get_tax_class’, ‘wc_diff_rate_for_user’, 10, 2 );
    add_filter( ‘woocommerce_product_variation_get_tax_class’, ‘wc_diff_rate_for_user’, 10, 2 );

    3. I also tried this code and unfortunately that doesnt work either.

    /* Tax Exemption */
    add_action( 'init', 'woocommerce_tax_exempt' );
    global $woocommerce;
    function woocommerce_customer_tax_exempt() {
    if ( is_user_logged_in() ) {
    $tax_exempt = current_user_can( ‘dealer’ );
    $woocommerce->customer->set_is_vat_exempt( $tax_exempt );
        }
    }

    4. Tax exemption for non-logged in users, also doesnt work. What is going on???

    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);
        }
    }

    Did anything happen during one of the latest updates or maybe the Members plugin is using some sort of prefix when assigning user roles. It’s starting to get a little frustrating.

    Any chance you guys can tweak one of the code snippets above? This is the first time that I try 4 different snippets with 0 zero results. ??

    Please help!

    Thanks much in advance!

    Jimmy

    • This topic was modified 4 years, 5 months ago by jimbo777. Reason: forgot 4th non-working code snippet
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘tax exempt user role (members plugin + set_is_vat_exempt)’ is closed to new replies.