Opencart: Query the database to get tax_rate data

我怕爱的太早我们不能终老 提交于 2019-12-08 05:11:35

问题


I'm trying to get a calculator helper in product admin page, near the price input I want to add a textbox that helps users to calculate the price with Tax VAT exluded to insert. It's something similar to this PLUGIN but I need to query the database to get an array of all Tax Rates and compare them with the tax_class_id selected by user just below the price.

I have found a sort of request in this page: admin/controller/localisation/tax_rate.php

if (isset($this->request->post['rate'])) {
    $this->data['rate'] = $this->request->post['rate'];
} elseif (!empty($tax_rate_info)) {
    $this->data['rate'] = $tax_rate_info['rate'];
} else {
    $this->data['rate'] = '';
}

But I have no idea how to query the database and get the data inside the page admin/view/template/catalog/product_form.tpl. I'm trying to get a result similar to Prestashop's price management.

Please give me some help!

EDIT:

I solved with this code added in admin/controller/catalog/product.php placed inside getForm() function:

$sql = 'SELECT tra.rate, tra.type, tru.tax_class_id FROM '.DB_PREFIX.'tax_rate tra LEFT JOIN '.DB_PREFIX.'tax_rule tru ON tru.tax_rate_id = tra.tax_rate_id WHERE tru.tax_class_id IS NOT NULL' ;
$query = $this->db->query($sql);
$rates = array();
foreach($query->rows as $result){
    $rates[] = $result;
}
$this->data['rates'] = $rates;

thanks to all the participants


回答1:


To get list of rates, use this code:

// get tax rates
$sql = 'SELECT * FROM '.DB_PREFIX.'tax_rate ';
$query = $this->db->query($sql);
$rates = array();
foreach($query->rows as $result){
    $rates[] = $result;
}

Output:

Array
(
[0] => Array
    (
        [tax_rate_id] => 86
        [geo_zone_id] => 3
        [name] => VAT (17.5%)
        [rate] => 17.5000
        [type] => P
        [date_added] => 2011-03-09 21:17:10
        [date_modified] => 2011-09-22 22:24:29
    )

[1] => Array
    (
        [tax_rate_id] => 87
        [geo_zone_id] => 3
        [name] => Eco Tax (-2.00)
        [rate] => 2.0000
        [type] => F
        [date_added] => 2011-09-21 21:49:23
        [date_modified] => 2011-09-23 00:40:19
    )

)

EDIT: The code above goes in the controller. To pass data to template use something like:

$this->data['rates'] = $rates;

Then this data will be accessible in view through $rates. Sorry for not clarifying this from the start. In other words, $this->data holds all variables for the view: $this->data['foo'] becomes $foo in the template file (view).




回答2:


I have also developed a price including VAT extension for OpenCart so I am able to give You some advice on how to move on:

  1. using jQuery handle the
    • Tax Class selectbox's change event
    • Price / Price Helper input keyup event (or any that You wish to)
    • by querying the database (within controller's action invoked by AJAX request) with the code like
      SELECT * FROM '.DB_PREFIX.'tax_rate tra LEFT JOIN '.DB_PREFIX.'tax_rule tru ON tru.tax_rate_id = tra.tax_rate_id WHERE tru.tax_class_id = ' . (int)$this->request->get['tax_class_id']
      where the tax_class_id index is retrieved from the GET from the AJAX call
  2. now that You have the tax_rate You can calculate the prices (I am doing this both-ways, even a person inputs price excluding VAT the price including VAT getd calculated and vice versa...)

I am sorry I cannot give You the concrete code but if I did that I could go directly to OpenCart extensions and delete my price including VAT extension ;-)



来源:https://stackoverflow.com/questions/16599779/opencart-query-the-database-to-get-tax-rate-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!