Zen Cart Logo
Forums / General Questions / custom tax order total little help please

custom tax order total little help please

Views: 737

Results 1 to 4 of 4
19 Jul 2011, 1:46 PM
#1
balihr avatar

balihr

Totally Zenned

Join Date:
Oct 2008
Location:
Croatia
Posts:
1,778
Plugin Contributions:
21

custom tax order total little help please

I have an order_total module (which I built) but can't get one desired effect on it.

I need it to collect info from the product in cart and check it's main category ID.

I have tried the following code but it doesn't work:

  1. added anew function to includes/functions/functions_categories.php```
    function zen_get_main_category_by_product($products_id){
    global $db;
    $cPath = '';

    $category_query = "select p.products_id, p.master_categories_id
    from " . TABLE_PRODUCTS . " p
    where p.products_id = '" . (int)$products_id . "' limit 1";

    $category = $db->Execute($category_query);

    if ($category->RecordCount() > 0) {

    $categories = array();
    zen_get_parent_categories($categories, $category->fields['master_categories_id']);
    $categories = array_reverse($categories);

    return $categories[0];
    }
    }

2) next, I added this to functions_tax.php:

function zen_get_sales_tax(){
global $db;
$products = $_SESSION['cart']->get_products();
for ($i=0, $n=sizeof($products); $i<$n; $i++) {
$productArray[$i] = array(
'id'=>$products[$i]['id']);
}
$main_category_id = zen_get_main_category_by_product($productArray[0]['id']);

$sales_tax_select = "SELECT sales_tax from " . TABLE_CATEGORIES . " where categories_id = '" . (int)$main_category_id . "'";

}

3) finally, I added this to my new ot_sales_tax.php file:
<?php /** * ot_total order-total module * * @package orderTotal * @copyright Copyright 2003-2007 Zen Cart Development Team * @copyright Portions Copyright 2003 osCommerce * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0 * @version $Id: ot_loworderfee.php 6101 2007-04-01 10:30:22Z wilt $ */ class ot_sales_tax { var $title, $output; function ot_sales_tax() { $this->code = 'ot_sales_tax'; $this->title = MODULE_ORDER_TOTAL_SALES_TAX_TITLE; $this->description = MODULE_ORDER_TOTAL_SALES_TAX_DESCRIPTION; $this->sort_order = MODULE_ORDER_TOTAL_SALES_TAX_SORT_ORDER; $this->output = array(); } function process() { global $order, $currencies; if (MODULE_ORDER_TOTAL_SALES_TAX == 'true') { // calculate from flat fee or percentage $sales_tax_defined = zen_get_sales_tax($sales_tax); $sales_tax_value = ($order->info['subtotal'] * ($sales_tax_defined/100)); $order->info['total'] += $sales_tax_value; $this->output[] = array('title' => $this->title . ':', 'text' => $currencies->format($sales_tax_value, true, $order->info['currency'], $order->info['currency_value']), 'value' => $sales_tax_value); } } function check() { global $db; if (!isset($this->_check)) { $check_query = "select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_ORDER_TOTAL_SALES_TAX_STATUS'"; $check_query = $db->Execute($check_query); $this->_check = $check_query->RecordCount(); } return $this->_check; } function keys() { return array('MODULE_ORDER_TOTAL_SALES_TAX_STATUS', 'MODULE_ORDER_TOTAL_SALES_TAX_SORT_ORDER', 'MODULE_ORDER_TOTAL_SALES_TAX', 'MODULE_ORDER_TOTAL_SALES_TAX_FEE'); } function install() { global $db; $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('This module is installed', 'MODULE_ORDER_TOTAL_SALES_TAX_STATUS', 'true', '', '6', '1','zen_cfg_select_option(array(\'true\'), ', now())"); $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_ORDER_TOTAL_SALES_TAX_SORT_ORDER', '400', 'Sort order of display.', '6', '2', now())"); $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Allow Low Order Fee', 'MODULE_ORDER_TOTAL_SALES_TAX', 'false', 'Do you want to allow low order fees?', '6', '3', 'zen_cfg_select_option(array(\'true\', \'false\'), ', now())"); $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, date_added) values ('Order Fee', 'MODULE_ORDER_TOTAL_SALES_TAX_FEE', '5', 'Enter your percentage', '6', '5', '', now())"); } function remove() { global $db; $db->Execute("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')"); } } ?>

Basically, what I'm trying to achieve is to get the ot module to find out the following:
 - check cart for current product
 - check that product's top level category id
 - select a DB entry based on category id
 - use that entry as tax percentage

Can anyone help me out where I'm going wrong?

Ajeh, please? Will send another big pack of donuts if we work this out. :)
21 Jul 2011, 2:30 AM
#2
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: custom tax order total little help please

Do you need to know the master_categories_id of each Product in the cart?

Or, do you need to know the parent category (Top Level Category) of the Product?

21 Jul 2011, 3:26 AM
#3
balihr avatar

balihr

Totally Zenned

Join Date:
Oct 2008
Location:
Croatia
Posts:
1,778
Plugin Contributions:
21

Re: custom tax order total little help please

Well, there's already a limit there so I can have only one category in cart. I can't add products from different categories.
So, I'd need check the cart, see what product(s) is in, get the product's Top level category (if, for eg. cPath=11_36_54, I need 11). Then I need the query to select an entry from DB (which is already there for top level categories) and calculate the tax based on that entry. And it should all work with PayPal EC... :)

I really really hope you can help, Ajeh, you're my last hope! Thank you!

21 Jul 2011, 4:15 AM
#4
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: custom tax order total little help please

You can get a Products parent category with this function from the Admin ...

You can copy this function in to an extra functions file such as:
/includes/functions/extra_functions/my_sales_tax_functions.php

<?php
////
// return products master_categories_id
// TABLES: categories
  function zen_get_parent_category_id($product_id) {
    global $db;

    $categories_lookup = $db->Execute("select master_categories_id
                                from " . TABLE_PRODUCTS . "
                                where products_id = '" . (int)$product_id . "'");

    $parent_id = $categories_lookup->fields['master_categories_id'];

    return $parent_id;
  }

Now you can just use it to get the parent category ...

You function:
function zen_get_sales_tax()

is not returning any results ... so you are finding the
$sales_tax_select->fields['sales_tax'

but you are not returning it at the bottom of the function ...

You are passing the $sales_tax to the function:

// calculate from flat fee or percentage
				$sales_tax_defined = zen_get_sales_tax($sales_tax);

but you did not define what $sales_tax is ... is that suppose to be a categories_id or products_id ... :unsure: