WebsiteBaker 2.13.8 is now available!
R.I.P Dietmar (luisehahne) and thank you for all your valuable work for WBhttps://forum.websitebaker.org/index.php/topic,32355.0.html
My question is, is it possible to "do something" based on the setting of an Item Option being added to an item. More specifically, if "Canada Tax is added to an item, could we check for the shipping address for the order, and then calculate the item and shipping tax based on that provinces tax rates?
if ($setting_tax_included == 'included') { // Calculate tax amount for prices including tax (brutto) if(your province == "AAAAA") {$sItemTaxRate = 19; } elseif(your province == "BBBB") {$sItemTaxRate = 17; } elseif(your province == "CCCC") {$sItemTaxRate = 15; } else {$sItemTaxRate = $items[$i]['tax_rate']} $sales_tax = $sales_tax + $items[$i]['price'] * $items[$i]['quantity'] * $items[$i]['tax_rate'] / (100 + $sItemTaxRate); }
// Retain shipping state code for Canadian sales tax calculation $shipping_state_code = $ship_state;
// CALCULATE ITEMS SALES TAX// *************************// Initialize vars$sales_tax = 0;$f_tax_rate_array = array();// Calculate tax for Canadian Shop Shipping to Canadian Province/Territory if (!empty($shipping_state_code)) { $shipping_prov_code = $shipping_state_code; } else { $shipping_prov_code = $cust_state_code; } $can_gst = array("BC","AB","SK","MB","QC","YT","NT","NU"); $can_hst = array("ON","NL","NB");// Calculate items sales taxfor ($i = 1; $i <= sizeof($items); $i++) {if ($setting_tax_included == 'included') { // Calculate tax amount for prices including tax (brutto) $sales_tax = $sales_tax + $items[$i]['price'] * $items[$i]['quantity'] * $items[$i]['tax_rate'] / (100 + $items[$i]['tax_rate']); } else { // Calculate tax amount for prices excluding tax (netto) if(array_search($shipping_prov_code, $can_gst)) {$sItemTaxRate = 5; } elseif(array_search($shipping_prov_code, $can_hst)) {$sItemTaxRate = 13; } elseif($shipping_prov_code == "PE") {$sItemTaxRate = 14; } elseif($shipping_prov_code == "NS") {$sItemTaxRate = 15; } else { $sItemTaxRate = $items[$i]['tax_rate']; } $sales_tax = $sales_tax + $items[$i]['price'] * $items[$i]['quantity'] / 100 * $sItemTaxRate;// $sales_tax = $sales_tax + $items[$i]['price'] * $items[$i]['quantity'] / 100 * $items[$i]['tax_rate']; } // Get tax rate(s) for display $f_tax_rate_array[] = number_format($items[$i]['tax_rate'], 1);}
// CALCULATE ITEMS SALES TAX// *************************// Initialize vars$sales_tax = 0;$f_tax_rate_array = array();// Calculate tax for Canadian Shop Shipping to Canadian Province/Territory if (!empty($shipping_state_code)) { $shipping_prov_code = $shipping_state_code; } else { $shipping_prov_code = $cust_state_code; } $can_gst = array("AB","BC","SK","MB","QC","YT","NT","NU"); $can_hst = array("ON","NL","NB");// Calculate items sales taxfor ($i = 1; $i <= sizeof($items); $i++) { if(in_array($shipping_prov_code, $can_gst)) {$sItemTaxRate[$i] = 5; } elseif(in_array($shipping_prov_code, $can_hst)) {$sItemTaxRate[$i] = 13; } elseif($shipping_prov_code == "PE") {$sItemTaxRate[$i] = 14; } elseif($shipping_prov_code == "NS") {$sItemTaxRate[$i] = 15; } else { $sItemTaxRate = $items[$i]['tax_rate']; }if ($setting_tax_included == 'included') { // Calculate tax amount for prices including tax (brutto) $sales_tax = $sales_tax + $items[$i]['price'] * $items[$i]['quantity'] * $sItemTaxRate[$i] / (100 + $sItemTaxRate[$i]); } else { // Calculate tax amount for prices excluding tax (netto) $sales_tax = $sales_tax + $items[$i]['price'] * $items[$i]['quantity'] / 100 * $sItemTaxRate[$i]; } // Get tax rate(s) for display $f_tax_rate_array[] = number_format($sItemTaxRate[$i], 1);}
// CALCULATE ITEMS SALES TAX// *************************// Initialize vars$sales_tax = 0;$f_tax_rate_array = array();// Calculate Canadian tax based on shipping destination if (!is_null($shipping_state_code)) { $shipping_prov_code = $shipping_state_code; } else { $shipping_prov_code = $cust_state_code; } $ca_gst = array("BC","AB","SK","MB","QC","YT","NT","NU"); $ca_hst = array("ON","NL","NB"); if(array_search($shipping_prov_code, $ca_gst)) {$sItemTaxRate = 5; } elseif(array_search($shipping_prov_code, $ca_hst)) {$sItemTaxRate = 13; } elseif($shipping_prov_code == "PE") {$sItemTaxRate = 14; } elseif($shipping_prov_code == "NS") {$sItemTaxRate = 15; } else { $sItemTaxRate = $items[$i]['tax_rate']; }// Calculate items sales taxfor ($i = 1; $i <= sizeof($items); $i++) {if ($setting_tax_included == 'included') { // Calculate tax amount for prices including tax (brutto) $sales_tax = $sales_tax + $items[$i]['price'] * $items[$i]['quantity'] * $items[$i]['tax_rate'] / (100 + $sItemTaxRate);// $sales_tax = $sales_tax + $items[$i]['price'] * $items[$i]['quantity'] * $items[$i]['tax_rate'] / (100 + $items[$i]['tax_rate']); } else { // Calculate tax amount for prices excluding tax (netto) $sales_tax = $sales_tax + $items[$i]['price'] * $items[$i]['quantity'] / 100 * $sItemTaxRate;// $sales_tax = $sales_tax + $items[$i]['price'] * $items[$i]['quantity'] / 100 * $items[$i]['tax_rate']; } // Get tax rate(s) for display $f_tax_rate_array[] = number_format($sItemTaxRate, 1);// $f_tax_rate_array[] = number_format($items[$i]['tax_rate'], 1);}
// CALCULATE SHIPPING SALES TAX// ****************************// Calculate Canadian shipping tax based on shipping destination $ship_tax_prov_code = $shipping_prov_code; // Calculate shipping Canadian sales tax if(in_array($ship_tax_prov_code, $gst_ca)) {$sShipTaxRate[$i] = 5; } elseif(in_array($ship_tax_prov_code, $hst_ca)) {$sShipTaxRate[$i] = 13; } elseif($ship_tax_prov_code == "PE") {$sShipTaxRate[$i] = 14; } elseif($ship_tax_prov_code == "NS") {$sShipTaxRate[$i] = 15; } else { $sShipTaxRate = $setting_tax_rate_shipping; }// Calculate tax amount for shipping including tax (brutto)if ($setting_tax_included == 'included') { $sales_tax_shipping = $shipping * $sShipTaxRate[$i] / (100 + $sShipTaxRate[$i]); //$sales_tax_shipping = $shipping * $setting_tax_rate_shipping / (100 + $setting_tax_rate_shipping);}else { // Calculate tax amount for shipping excluding tax (netto) $sales_tax_shipping = $shipping / 100 * $sShipTaxRate[$i];// $sales_tax_shipping = $shipping / 100 * $setting_tax_rate_shipping;}
Everything worked except when the customer's province was Ontario (ON), then no tax was added (it should have been 13%). I thought it might be because it matched the shop_state, but then I changed the shop_state to another province and still Ontario did not show the proper tax of 13%. Can't figure that out.
Is the key the "i" variable in $sItemTaxRate[$i]?
// Calculate items sales taxfor ($i = 1; $i <= sizeof($items); $i++) {
Now I just have to make the tax on shipping act the same way (tax on shipping based on shipping province). Can I do this in the view_summary.php file as well, or do I have to go further?
i use in_array(), array_search doesnt give the key No == 0, it sends false for that
Tip: make a renamed copy from this view_summary.php (maybe view_summary_canada .php) and let it stay in the bakery-folder as a backup. so that you dont overwrite your work in the next upgrade.
// Convert state code to state name
// Retain shipping state code for Canadian sales tax calculation$shipping_state_code = $ship_state;
// CALCULATE ITEMS SALES TAX// *************************// Initialize vars$sales_tax = 0;$f_tax_rate_array = array();// Calculate items sales taxfor ($i = 1; $i <= sizeof($items); $i++) { if ($setting_tax_included == 'included') { // Calculate tax amount for prices including tax (brutto) $sales_tax = $sales_tax + $items[$i]['price'] * $items[$i]['quantity'] * $items[$i]['tax_rate'] / (100 + $items[$i]['tax_rate']); } else { // Calculate tax amount for prices excluding tax (netto) $sales_tax = $sales_tax + $items[$i]['price'] * $items[$i]['quantity'] / 100 * $items[$i]['tax_rate']; } // Get tax rate(s) for display $f_tax_rate_array[] = number_format($items[$i]['tax_rate'], 1);}
// CALCULATE ITEMS SALES TAX// *************************// Initialize vars$sales_tax = 0;$f_tax_rate_array = array();// Check if Shop Country is Canadaif ($setting_shop_country=='CA') { // Canadian Shop - Check if there is a shipping address if (!is_null($shipping_state_code)) { $shipping_prov_code = $shipping_state_code; } else { $shipping_prov_code = $cust_state_code; } $gst_ca = array("AB","BC","SK","MB","QC","YT","NT","NU"); $hst_ca = array("ON","NL","NB"); // Calculate Canadian tax on total order based on shipping destination for ($i = 1; $i <= sizeof($items); $i++) {// Calculate items Canadian sales tax if(in_array($shipping_prov_code, $gst_ca)) {$sItemTaxRate[$i] = 5;} elseif(in_array($shipping_prov_code, $hst_ca)) {$sItemTaxRate[$i] = 13;} elseif($shipping_prov_code == "PE") {$sItemTaxRate[$i] = 14;} elseif($shipping_prov_code == "NS") {$sItemTaxRate[$i] = 15;} else { $sItemTaxRate = $items[$i]['tax_rate']; }if ($setting_tax_included == 'included') { // Calculate tax amount for prices including tax (brutto) $sales_tax = $sales_tax + $items[$i]['price'] * $items[$i]['quantity'] * $sItemTaxRate[$i] / (100 + $sItemTaxRate[$i]); } else { // Calculate tax amount for prices excluding tax (netto) $sales_tax = $sales_tax + $items[$i]['price'] * $items[$i]['quantity'] / 100 * $sItemTaxRate[$i]; } // Get tax rate(s) for display $f_tax_rate_array[] = number_format($sItemTaxRate[$i], 1); }}else{ // Calculate items sales tax for ($i = 1; $i <= sizeof($items); $i++) { if ($setting_tax_included == 'included') { // Calculate tax amount for prices including tax (brutto) $sales_tax = $sales_tax + $items[$i]['price'] * $items[$i]['quantity'] * $items[$i]['tax_rate'] / (100 + $items[$i]['tax_rate']); } else { // Calculate tax amount for prices excluding tax (netto) $sales_tax = $sales_tax + $items[$i]['price'] * $items[$i]['quantity'] / 100 * $items[$i]['tax_rate']; } // Get tax rate(s) for display $f_tax_rate_array[] = number_format($items[$i]['tax_rate'], 1); }}
// CALCULATE SHIPPING SALES TAX// ****************************// Calculate tax amount for shipping including tax (brutto)if ($setting_tax_included == 'included') { $sales_tax_shipping = $shipping * $setting_tax_rate_shipping / (100 + $setting_tax_rate_shipping);}else { // Calculate tax amount for shipping excluding tax (netto) $sales_tax_shipping = $shipping / 100 * $setting_tax_rate_shipping;}
// CALCULATE SHIPPING SALES TAX// ****************************// Check if Shop Country is Canadaif ($setting_shop_country=='CA') {// Calculate Canadian shipping tax based on shipping destination $ship_tax_prov_code = $shipping_prov_code; // Calculate shipping Canadian sales tax if(in_array($ship_tax_prov_code, $gst_ca)) {$sShipTaxRate = 5; } elseif(in_array($ship_tax_prov_code, $hst_ca)) {$sShipTaxRate = 13; } elseif($ship_tax_prov_code == "PE") {$sShipTaxRate = 14; } elseif($ship_tax_prov_code == "NS") {$sShipTaxRate = 15; } else { $sShipTaxRate = $setting_tax_rate_shipping; }// Calculate tax amount for shipping including tax (brutto)if ($setting_tax_included == 'included') { $sales_tax_shipping = $shipping * $sShipTaxRate / (100 + $sShipTaxRate);}else { // Calculate tax amount for shipping excluding tax (netto) $sales_tax_shipping = $shipping / 100 * $sShipTaxRate;}}else {// Calculate tax amount for shipping including tax (brutto)if ($setting_tax_included == 'included') { $sales_tax_shipping = $shipping * $setting_tax_rate_shipping / (100 + $setting_tax_rate_shipping);}else { // Calculate tax amount for shipping excluding tax (netto) $sales_tax_shipping = $shipping / 100 * $setting_tax_rate_shipping;}}