Lyse Posted June 18, 2019 Share Posted June 18, 2019 (edited) Hi My website is a Wordpress WooCommerce. I modified the PHP function that worked well before I added the multiple customer roles. 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 Edited June 18, 2019 by Lyse Specify website platforms Quote Link to comment Share on other sites More sharing options...
gw1500se Posted June 18, 2019 Share Posted June 18, 2019 (edited) We need to know what error message you are getting on which line or which line is not producing the results you expect? Edited June 18, 2019 by gw1500se Quote Link to comment Share on other sites More sharing options...
Lyse Posted June 18, 2019 Author Share Posted June 18, 2019 I don't get any error. If the code works I should not see two taxes applied in my cart. I should see one tax (PST) for PST exempt roles or no tax at all for TAX exempt roles. I always see two taxes, so I know the strings of roles I added to the code do not work. Here's the original code that works perfectly: /*  * 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 role: Wholesale Customer        if (!empty($wwp_wholesale_role) && in_array('wholesale_customer', $wwp_wholesale_role)) {        // Where 'wholesale_customer' is the name of the wholesale role you want to target            $tax_class = 'WholesalePSTGST';                 }        // special tax rate: charge only GST if role: Wholesale PST Exempt                if (!empty($wwp_wholesale_role) && in_array('wholesale_pst_exempt', $wwp_wholesale_role)) {        // Where 'wholesale_customer' is the name of the wholesale role you want to target            $tax_class = 'WholesalePSTExempt';                 }        // special tax rate: zero if role: Wholesale Tax Exempt         if (!empty($wwp_wholesale_role) && in_array('wholesale_tax_exempt', $wwp_wholesale_role)) {        // Where 'wholesale_customer' is the name of the wholesale role you want 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_pst_exempt' , $current_user_wholesale_roles) ){          return 'wholesalepstexempt';       } elseif (in_array( 'wholesale_tax_exempt' , $current_user_wholesale_roles) ){          return 'wholesalezerotax';       }              }    return $option_value; } , 10 , 1 );  Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 18, 2019 Share Posted June 18, 2019 Why don't you compare the two sets of code (cause I am not going to!) and see what you changed. You do know you have a big chunk commented out? Quote Link to comment Share on other sites More sharing options...
Lyse Posted June 18, 2019 Author Share Posted June 18, 2019 I am aware of the commented chunk. My changes were in the arrays where added the new roles. I did not change anything else.'// 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 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'; } } and here: $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'; } }  Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 18, 2019 Share Posted June 18, 2019 Do you have php error checking on to catch any typos you may made in all of this new (and tedious) code? Quote Link to comment Share on other sites More sharing options...
Lyse Posted June 18, 2019 Author Share Posted June 18, 2019 I added the error reporting code you suggested. Quote Link to comment Share on other sites More sharing options...
Lyse Posted June 18, 2019 Author Share Posted June 18, 2019 The only reported error relates a plugin that is not related to my specific code. So my code is not returning any error. Quote Link to comment Share on other sites More sharing options...
gw1500se Posted June 18, 2019 Share Posted June 18, 2019 Did you printout each variable to make sure it contains what you expect? As ginerjm said, your code is really too tedious to compare. You may need to spend some time rethinking all your 'if's to simplify them somehow. Quote Link to comment Share on other sites More sharing options...
Lyse Posted June 18, 2019 Author Share Posted June 18, 2019 I'm not an experienced PHP coder, so I don't know how to simply my if statements, I simply repeated them from the first one. I don't know how to print out each variable either. Â Quote Link to comment Share on other sites More sharing options...
gw1500se Posted June 18, 2019 Share Posted June 18, 2019 (edited) Use 'echo' for individual variables. Use '<pre><?php var_dump($arrayorobject); ?></pre>' for arrays or objects. Edited June 18, 2019 by gw1500se Quote Link to comment Share on other sites More sharing options...
Lyse Posted June 18, 2019 Author Share Posted June 18, 2019 I'm sorry but I don't know where or how to use these statements within my code. I don't know PHP enough just getting by with help from experts, such as yourself. Quote Link to comment Share on other sites More sharing options...
gw1500se Posted June 18, 2019 Share Posted June 18, 2019 (edited) Put them wherever you want to check the values of variables. Like maybe just before the 'if' statements. Perhaps you will find this useful. Edited June 18, 2019 by gw1500se Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted June 18, 2019 Share Posted June 18, 2019 first, a couple of questions - 1. can a user have more than one role value at a time? i ask this because in the all_custom_tax_classes function, the last role found is what is returned, but in the in-line function, the first role found is what is returned. if there is only a single value in the user's role array, all this code can be greatly simplified. 2. the values being returned from the all_custom_tax_classes function have camel-case letter-case, but the values being returned from the in-line function are all lower-case. does the code at some point convert all values to lower-case? in any case, this code needs to be converted to be a general purpose data-driven design, where a data structure defines what simple code should do, since all it is doing is mapping found input values to output values. once this is done, all you need to do is edit the data structure to add new sets of values. the following (untested, could contain typos) is what the original/working code would look like - /* * APPLY DIFFERENT TAX RATE BASED ON CUSTOMER USER ROLE * (Code compacted in one unique hook instead of 5 functions with the same hook) */ // in the original version, the $tax_class input is conditionally modified and returned. $product is not used. function all_custom_tax_classes( $tax_class, $product ) { global $current_user; // used in isset() only // Getting the current user $curr_user = wp_get_current_user(); // not used in 'active' code $curr_user_data = get_userdata($current_user->ID); // not used in 'active' code // 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(); // get an array of the user roles - is there ever more than one element? // define an array that maps input values to output values - note: if the user role values were defined to be the same as the expected return values, this step wouldn't be needed $map = []; $map['wholesale_customer'] = 'WholesalePSTGST'; // charge both PST and GST $map['wholesale_pst_exempt'] = 'WholesalePSTExempt'; // charge only GST $map['wholesale_tax_exempt'] = 'WholesaleZeroTax'; // charge neither if (!empty($wwp_wholesale_role) { // the following assumes that the 1st role value found is what is returned. if a user can have more than one role, this differs from the original logic, in that the last role found is what is returned. // loop over the map array foreach($map as $key=>$value) { // test if the key is in the role array if(in_array($key,$wwp_wholesale_role)) { // if so, return the value corresponding to the key return $value; } } } } // if none of the role values was found in the user role(s), return the original $tax_class value to the calling code 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 ); // calls the above function add_filter( 'woocommerce_product_variation_get_tax_class', 'all_custom_tax_classes', 1, 2 ); // calls the above function // calls the in-line function. the $option_value input is returned if none of the logic returns first. add_filter( 'option_woocommerce_shipping_tax_class' , function( $option_value ) { global $wc_wholesale_prices; if ( $wc_wholesale_prices && is_a( $wc_wholesale_prices , 'WooCommerceWholeSalePrices' ) ) { $wwp_wholesale_role = $wc_wholesale_prices->wwp_wholesale_roles->getUserWholesaleRole(); // get an array of the user roles - is there ever more than one element? // define an array that maps input values to output values - note: if the user role values were defined to be the same as the expected return values, this step wouldn't be needed // use the same definition as above (you would actually do this through configuration data so as to not repeat it) // i'm assuming that the same letter-case values used above will work here. if not, alter this data as needed. $map = []; $map['wholesale_customer'] = 'WholesalePSTGST'; // charge both PST and GST $map['wholesale_pst_exempt'] = 'WholesalePSTExempt'; // charge only GST $map['wholesale_tax_exempt'] = 'WholesaleZeroTax'; // charge neither if (!empty($wwp_wholesale_role) { // the following assumes that the 1st role value found is what is returned. this is the same operation as the original code here. // loop over the map array foreach($map as $key=>$value) { // test if the key is in the role array if(in_array($key,$wwp_wholesale_role)) { // if so, return the value corresponding to the key return $value; } } } } return $option_value; } , 10 , 1 ); Â assuming no mistakes in the above code and it works correctly, all you would need to do is modify the $map array(s) to add your new definitions. Â Â Â Quote Link to comment Share on other sites More sharing options...
Lyse Posted June 18, 2019 Author Share Posted June 18, 2019 Answer to Q1. The wholesale customer can only have one role. Answer to Q2. The camel case I used for the how I created the tax class (sample provided by the wholesale plugin) and all of the wholesale roles lower case are my creations in this function and are used by the wholesale plugin as role "keys" . No conversion is done on the values. There seems to be a syntax error with the array that I can't seem to figure out how to fix. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted June 18, 2019 Share Posted June 18, 2019 10 minutes ago, Lyse said: Answer to Q1. The wholesale customer can only have one role. then, all the in_array() statement(s) can be removed. just get the role value (either using current() or the zero'th element of the array) and use it directly in comparisons. 11 minutes ago, Lyse said: Answer to Q2. The camel case I used for the how I created the tax class (sample provided by the wholesale plugin) and all of the wholesale roles lower case are my creations in this function and are used by the wholesale plugin as role "keys" . No conversion is done on the values. i suspect the code is using the returned values to dynamically/indirectly call user classes/methods, in which case the letter-case doesn't matter. 12 minutes ago, Lyse said: There seems to be a syntax error with the array that I can't seem to figure out how to fix. if the error is on the $map = []; line, it is because you are using a very old and obsolete php version. the short array syntax [] was introduced in php 5.4. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted June 18, 2019 Share Posted June 18, 2019 1 hour ago, mac_gyver said: if (!empty($wwp_wholesale_role) if the error is on the above line, it's missing a )Â repeated in two places in the code. Quote Link to comment Share on other sites More sharing options...
Lyse Posted June 18, 2019 Author Share Posted June 18, 2019 The error is on the $map = [ ];. I have not uploaded the function to the server yet. I'm only editing the PHP code using Dreamweaver. DW is reporting the syntax error. If I remove the array, do I just go back to my old original code? Â Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted June 18, 2019 Share Posted June 18, 2019 dreamweaver was never any good. the $map = []; line is valid for php 5.4 and higher. 24 minutes ago, Lyse said: If I remove the array, do I just go back to my old original code? no. don't use the original logic. you should NOT find yourself adding program logic each time you add a new data value. this is a sign you are doing something wrong. in fact, wherever the roles are being defined, should have a column for the tax type, then just retrieve and use the tax type for the current user and all the code in this thread would go away. Quote Link to comment Share on other sites More sharing options...
Lyse Posted June 18, 2019 Author Share Posted June 18, 2019 If I understand correctly, you suggest that a field be create/made available in the wholesale plugin so that when I create a wholesaler role, that field would allow me to assign the tax class for that particular user role? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted June 18, 2019 Share Posted June 18, 2019 yes. that is the ideal way of implementing this. but before you start making changes like this, make sure this is within your programming skill level. currently, the mapping of role (name) to tax class is hard-coded in the program logic you have shown in this thread, and it would appear that in an earlier version there were 5 different functions to do this, one for each possible input/output combination. the use of the $map array that i suggested, simplifies the current method, so that you are not writing out and fixing program logic for each possible combination. you just have an array that defines the input/output combinations. to change to having the tax class defined with the role, would require that you add a column to the database table (or perhaps just add a new key/value if the table is using the wordpress metadata style of storage), write a new ->getUserWholesaleClass() method to retrieve the value, then change the code you have posted in this thread so that it just gets and returns the tax class for the user.  Quote Link to comment Share on other sites More sharing options...
Lyse Posted June 19, 2019 Author Share Posted June 19, 2019 My programming skills is basic and by no means capable to change program logic of that magnitude. I do like the idea of the $map array you suggested that looks much simpler to use. What exactly should the function end up looking like with the array? Â Quote Link to comment Share on other sites More sharing options...
Lyse Posted June 19, 2019 Author Share Posted June 19, 2019 Would my array look like this: $map = []; $map['wholesale_customer','wholesale_silvia_silver','wholesale_silvia_gold','wholesale_silvia_premium','wholesale_silvia_union'] = 'WholesalePSTGST'; // charge both PST and GST $map['wholesale_pst_exempt','wholesale_silvia_silver_pst_exempt','wholesale_silvia_gold_pst_exempt','wholesale_silvia_premium_pst_exempt','wholesale_silvia_union_pst_exempt'] = 'WholesalePSTExempt'; // charge only GST $map['wholesale_tax_exempt','wholesale_silvia_silver_tax_exempt','wholesale_silvia_gold_tax_exempt','wholesale_silvia_premium_tax_exempt','wholesale_silvia_union_tax_exempt'] = 'WholesaleZeroTax'; // charge neither  Quote Link to comment Share on other sites More sharing options...
Barand Posted June 19, 2019 Share Posted June 19, 2019 I would opt for a slightly different structure, viz $taxes = ['Wholesale Customer' => ['PST' => 1, 'GST' => 1], 'Wholesale Silvia Silver' => ['PST' => 1, 'GST' => 1], 'Wholesale Silvia Gold' => ['PST' => 1, 'GST' => 1], 'Wholesale Silvia Premium' => ['PST' => 1, 'GST' => 1], 'Wholesale Silvia Union' => ['PST' => 1, 'GST' => 1], 'wholesale_pst_exempt' => ['PST' => 0, 'GST' => 1], 'wholesale_silvia_silver_pst_exempt' => ['PST' => 0, 'GST' => 1], 'wholesale_silvia_gold_pst_exempt' => ['PST' => 0, 'GST' => 1], 'wholesale_silvia_premium_pst_exempt' => ['PST' => 0, 'GST' => 1], 'wholesale_silvia_union_pst_exempt' => ['PST' => 0, 'GST' => 1], 'wholesale_tax_exempt' => ['PST' => 0, 'GST' => 0], 'wholesale_silvia_silver_tax_exempt' => ['PST' => 0, 'GST' => 0], 'wholesale_silvia_gold_tax_exempt' => ['PST' => 0, 'GST' => 0], 'wholesale_silvia_premium_tax_exempt' => ['PST' => 0, 'GST' => 0], 'wholesale_silvia_union_tax_exempt' => ['PST' => 0, 'GST' => 0] ]; Then $PST_payable = $taxes[$customer_role]['PST']; $GST_payable = $taxes[$customer_role]['GST']; Â Quote Link to comment Share on other sites More sharing options...
Lyse Posted June 19, 2019 Author Share Posted June 19, 2019 (edited) How does this fit into the remainder of my function? Â Edited June 19, 2019 by Lyse Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.