0

I have regular customers and regular and tier-level wholesalers. I live in Canada and with have two taxes to apply (GST & PST). We also have customers/wholesalers that get exempted from one tax (PST) only or both taxes.

I have one regular wholesale role with two tax exemption roles: 'wholesale_customer', 'wholesale_pst_exempt', 'wholesale_tax_exempt'.

I have 4 tier levels wholesale roles with each their own tax exemption roles:

'wholesale_silvia_silver', 'wholesale_silvia_gold', 'wholesale_silvia_premium', 'wholesale_silvia_union'

'wholesale_silvia_silver_pst_exempt', 'wholesale_silvia_gold_pst_exempt', 'wholesale_silvia_premium_pst_exempt', 'wholesale_silvia_union_pst_exempt'

'wholesale_silvia_silver_tax_exempt', 'wholesale_silvia_gold_tax_exempt', 'wholesale_silvia_premium_tax_exempt', 'wholesale_silvia_union_tax_exempt'

The tier levels are new and I'm trying to update my existing function that applies different tax rates based on customer user roles. I also have filters to alter the shipping tax for the different tax class based on the customer role.

Here are the function and filter that I have updated to add the additional tier level wholesale roles. The changes I've made are not working because I don't see the tax exemptions. Both taxes are always being applied.

Can someone help me figure out what I've done wrong to the code that stopped it from working? I'm not proficient in PHP, so was trying my best to make this work.

    /*
    * APPLY DIFFERENT TAX RATE BASED ON CUSTOMER USER ROLE
    * (Code compacted in one unique hook instead of 5 functions with the same              hook)
    */
    function all_custom_tax_classes( $tax_class, $product ) {
    global $current_user;

    // Getting the current user
    $curr_user = wp_get_current_user();
    $curr_user_data = get_userdata($current_user->ID);

    // 1 customer_tax_exempt 
    /* special tax rate: zero if role: Customer Tax Exempt */
    /*if ( in_array( 'customer_tax_exempt', $curr_user_data->roles ) )
        $tax_class = 'CustomerTaxExemptClass';

    // 2 customer_pst_exempt
    // special tax rate: charge only GST if role: Customer PST Exempt
    if ( in_array( 'customer_pst_exempt', $curr_user_data->roles ) )
        $tax_class = 'CustomerPSTExemptClass';      
    */
    // 3, 4 & 5 WHOLESLE SUITE SPECIAL WHOLESALE TAX RATES
    if (isset($current_user) && class_exists('WWP_Wholesale_Roles')) {
        $wwp_wholesale_roles = WWP_Wholesale_Roles::getInstance();
        $wwp_wholesale_role = $wwp_wholesale_roles->getUserWholesaleRole();

        // special tax rate: charge both PST and GST if roles: Wholesale    Customer,  Wholesale Silvia Silver, Wholesale Silvia Gold, Wholesale Silvia Premium, Wholesale Silvia Union
        if (!empty($wwp_wholesale_role) && in_array('wholesale_customer', $wwp_wholesale_role) && in_array('wholesale_silvia_silver', $wwp_wholesale_role) && in_array('wholesale_silvia_gold', $wwp_wholesale_role) && in_array('wholesale_silvia_premimum', $wwp_wholesale_role) && in_array('wholesale_silvia_union', $wwp_wholesale_role)) {
        // Where 'wholesale_customer, wholesale_silvia_silver, wholesale_silvia_gold, wholesale_silvia_premium, wholesale_silvia_union' are the names of the wholesale roles to target
            $tax_class = 'WholesalePSTGST';

        }

        // special tax rate: charge only GST if roles: Wholesale PST Exempt, Wholesale Silvia Silver PST Exempt, Wholesale Silvia Gold PST Exempt, Wholesale Silvia Premium PST Exempt, Wholesale Silvia Union PST Exempt
                if (!empty($wwp_wholesale_role) &&   in_array('wholesale_pst_exempt', $wwp_wholesale_role) && in_array('wholesale_silvia_silver_pst_exempt', $wwp_wholesale_role) && in_array('wholesale_silvia_gold_pst_exempt', $wwp_wholesale_role) && in_array('wholesale_silvia_premium_pst_exempt', $wwp_wholesale_role) && in_array('wholesale_silvia_union_pst_exempt', $wwp_wholesale_role)) {
        // Where 'wholesale_pst_exempt, wholesale_silvia_silver_pst_exempt, wholesale_silvia_gold_pst_exempt, wholesale_silvia_premium_pst_exempt, wholesale_silvia_union_pst_exempt' are the names of the wholesale roles to target
            $tax_class = 'WholesalePSTExempt';

        }

        // special tax rate: zero if roles: Wholesale Tax Exempt, Wholesale Silvia Silver Tax Exempt, Wholesale Silvia Gold Tax Exempt, Wholesale Silvia Premium Tax Exempt, Wholesale Silvia Union Tax Exempt
         if (!empty($wwp_wholesale_role) && in_array('wholesale_tax_exempt', $wwp_wholesale_role) && in_array('wholesale_silvia_silver_tax_exempt', $wwp_wholesale_role)&& in_array('wholesale_silvia_gold_tax_exempt', $wwp_wholesale_role) && in_array('wholesale_silvia_premium_tax_exempt', $wwp_wholesale_role) && in_array('wholesale_silvia_union_tax_exempt', $wwp_wholesale_role)) {
        // Where 'wholesale_tax_exempt, wholesale_silvia_silver_tax_exempt, wholesale_silvia_gold_tax_exempt, wholesale_silvia_premium_tax_exempt, wholesale_silvia_union_tax_exempt' are the names of the wholesale role to target
            $tax_class = 'WholesaleZeroTax';
                }
    }

  return $tax_class;

}

    /* ADDITIONAL FILTERS TO ALTER THE SHIPPING TAX FOR DIFFERENT TAX CLASSES BASED ON CUSTOMER USER ROLE */
    add_filter( 'woocommerce_product_get_tax_class', 'all_custom_tax_classes', 1, 2 );
    add_filter( 'woocommerce_product_variation_get_tax_class', 'all_custom_tax_classes', 1, 2 );

    add_filter( 'option_woocommerce_shipping_tax_class' , function( $option_value ) {

    global $wc_wholesale_prices;

    if ( $wc_wholesale_prices && is_a( $wc_wholesale_prices , 'WooCommerceWholeSalePrices' ) ) {

        $current_user_wholesale_roles = $wc_wholesale_prices->wwp_wholesale_roles->getUserWholesaleRole();

        if ( in_array( 'wholesale_customer', $current_user_wholesale_roles ) ){
            return 'wholesalepstgst';
        } elseif (in_array( 'wholesale_silvia_silver', $current_user_wholesale_roles) ){
            return 'wholesalepstgst';
        } elseif (in_array( 'wholesale_silvia_gold', $current_user_wholesale_roles) ){
            return 'wholesalepstgst';
        } elseif (in_array( 'wholesale_silvia_premium', $current_user_wholesale_roles) ){
            return 'wholesalepstgst';
        } elseif (in_array( 'wholesale_silvia_union', $current_user_wholesale_roles) ){
            return 'wholesalepstgst';
        } elseif (in_array( 'wholesale_pst_exempt', $current_user_wholesale_roles) ){
            return 'wholesalepstexempt';
        } elseif (in_array( 'wholesale_silvia_silver_pst_exempt', $current_user_wholesale_roles) ){
            return 'wholesalepstexempt';
        }  elseif (in_array( 'wholesale_silvia_gold_pst_exempt', $current_user_wholesale_roles) ){
            return 'wholesalepstexempt';
        }  elseif (in_array( 'wholesale_silvia_premium_pst_exempt', $current_user_wholesale_roles) ){
            return 'wholesalepstexempt';
        }  elseif (in_array( 'wholesale_silvia_union_pst_exempt', $current_user_wholesale_roles) ){
            return 'wholesalepstexempt';
        }  elseif (in_array( 'wholesale_tax_exempt', $current_user_wholesale_roles) ){
            return 'wholesalezerotax';
        }  elseif (in_array( 'wholesale_silvia_silver_tax_exempt', $current_user_wholesale_roles) ){
            return 'wholesalezerotax';
        }  elseif (in_array( 'wholesale_silvia_gold_tax_exempt', $current_user_wholesale_roles) ){
            return 'wholesalezerotax';
        }  elseif (in_array( 'wholesale_silvia_premium_tax_exempt', $current_user_wholesale_roles) ){
            return 'wholesalezerotax';
        }  elseif (in_array( 'wholesale_silvia_union_tax_exempt', $current_user_wholesale_roles) ){
            return 'wholesalezerotax';
        }

    }

    return $option_value;

} , 10 , 1 );

Thank You Lyse

Siong Thye Goh
  • 3,518
  • 10
  • 23
  • 31
Lyse
  • 53
  • 1
  • 8
  • Thank you Siong, but I'm not sure I see the change you've made. The code appears to be the same. – Lyse Jun 16 '19 at 13:47
  • I remember that LoicTheAztec was the user on Stackoverlow that helped me with by providing the initial code solution. Is anybody out there able to help me with this? – Lyse Jun 18 '19 at 13:09
  • Had help on a different forum and had successful resolution. – Lyse Jun 20 '19 at 14:18

0 Answers0