论坛风格切换
  • 7471阅读
  • 3回复

zencart订单查询函数query介绍 [复制链接]

上一主题 下一主题
 

发帖
6649
大洋
50101
贡献值
940
只看楼主 正序阅读 使用道具 楼主  发表于: 2012-08-14
zencart外贸建站外贸网站建站深圳外贸B2C建站外贸建站,信用卡通道集成,Amazon分类审核建站。请QQ联系
QQ:1023206115 QQ:2257583107 QQ:3038328260

zencart订单类 order 是zencart这个购物系统最重要的类之一。购物网站嘛,肯定是要对订单处理,所以这样的一个类是必不可少的。 
订单类order在includes\classes\order.php文件中定义,这个类只有在系统进入到下单的步骤才开始初始化,就是在用户点击确认之后开始生成订单,处理代码在includes\modules\checkout_process.php 文件中,首先要引入类的文件require(DIR_WS_CLASSES . 'order.php');然后初始化订单对象$order = new order; 生成这个对象后,就把订单的信息保存在这个对象里面,但是此时数据库里面还没有插入订单内容。只有当这个对象执行create()函数才开始插入数据库。这个流程大概就是这样。但是现在主要讲的主题不是怎么处理订单。我要讲的是怎么通过一个订单ID来获取这个订单里面的信息,比如,购买的用户名字,产品名字,付款方式,价钱...等等。而能完成这个功能的函数就是订单类的方法query($order_id)。 
这个功能可能更多的还是在后台开发的时候需要得到,因为前台主要的是处理订单,把订单的内容放到数据库。而这个方法主要就是读取数据库已经保存的订单的信息。 
这个方法的参数是订单ID,这个方法处理流程就是通过订单ID,查询数据库获取订单内容,然后把这些信息保存到类的成员变量里面。这样就可以通过读取成员变量里面的内容里了解订单的信息。 
首先是查询的SQL语句,看看如下代码 

  1. $order_query = "select customers_id, customers_name, customers_company,
  2.                          customers_street_address, customers_suburb, customers_city,
  3.                          customers_postcode, customers_state, customers_country,
  4.                          customers_telephone, customers_email_address, customers_address_format_id,
  5.                          delivery_name, delivery_company, delivery_street_address, delivery_suburb,
  6.                          delivery_city, delivery_postcode, delivery_state, delivery_country,
  7.                          delivery_address_format_id, billing_name, billing_company,
  8.                          billing_street_address, billing_suburb, billing_city, billing_postcode,
  9.                          billing_state, billing_country, billing_address_format_id,
  10.                          payment_method, payment_module_code, shipping_method, shipping_module_code,
  11.                          coupon_code, cc_type, cc_owner, cc_number, cc_expires, currency, currency_value,
  12.                          date_purchased, orders_status, last_modified, order_total, order_tax, ip_address
  13.                         from " . TABLE_ORDERS . "
  14.                         where orders_id = '" . (int)$order_id . "'";
  15.     $order = $db->Execute($order_query);
里面的SQL语句获取了orders表的很多个字段,具体字段意思,顾名思义就可以了。 
开始给成员变量$this->info赋值, 
如代码 
  1. $this->info = array('currency' => $order->fields['currency'],
  2.                         'currency_value' => $order->fields['currency_value'],
  3.                         'payment_method' => $order->fields['payment_method'],
  4.                         'payment_module_code' => $order->fields['payment_module_code'],
  5.                         'shipping_method' => $order->fields['shipping_method'],
  6.                         'shipping_module_code' => $order->fields['shipping_module_code'],
  7.                         'coupon_code' => $order->fields['coupon_code'],
  8.                         'cc_type' => $order->fields['cc_type'],
  9.                         'cc_owner' => $order->fields['cc_owner'],
  10.                         'cc_number' => $order->fields['cc_number'],
  11.                         'cc_expires' => $order->fields['cc_expires'],
  12.                         'date_purchased' => $order->fields['date_purchased'],
  13.                         'orders_status' => $order_status->fields['orders_status_name'],
  14.                         'last_modified' => $order->fields['last_modified'],
  15.                         'total' => $order->fields['order_total'],
  16.                         'tax' => $order->fields['order_tax'],
  17.                         'ip_address' => $order->fields['ip_address']
  18.                         );
具体这个变量是保存什么内容,可以看看这个数组里面的键值就知道了,大概内容就是这个订单的一些基本信息,比如付款方式,运费方式,下订单的IP地址.... 
接下来就是给$this->customer变量赋值了,这个变量顾名思义就知道是顾客的信息了。看看这个数字的字段就知道保存了顾客什么信息。 
 
  1. $this->customer = array('id' => $order->fields['customers_id'],
  2.                             'name' => $order->fields['customers_name'],
  3.                             'company' => $order->fields['customers_company'],
  4.                             'street_address' => $order->fields['customers_street_address'],
  5.                             'suburb' => $order->fields['customers_suburb'],
  6.                             'city' => $order->fields['customers_city'],
  7.                             'postcode' => $order->fields['customers_postcode'],
  8.                             'state' => $order->fields['customers_state'],
  9.                             'country' => $order->fields['customers_country'],
  10.                             'format_id' => $order->fields['customers_address_format_id'],
  11.                             'telephone' => $order->fields['customers_telephone'],
  12.                             'email_address' => $order->fields['customers_email_address']);
在下来就是$this->delivery变量,这个就是送货地址了。 
  1. $this->delivery = array('name' => $order->fields['delivery_name'],
  2.                             'company' => $order->fields['delivery_company'],
  3.                             'street_address' => $order->fields['delivery_street_address'],
  4.                             'suburb' => $order->fields['delivery_suburb'],
  5.                             'city' => $order->fields['delivery_city'],
  6.                             'postcode' => $order->fields['delivery_postcode'],
  7.                             'state' => $order->fields['delivery_state'],
  8.                             'country' => $order->fields['delivery_country'],
  9.                             'format_id' => $order->fields['delivery_address_format_id']);
下来是$this->billing 账单地址了,一般情况下送货地址跟账单地址都一样 
  1. $this->billing = array('name' => $order->fields['billing_name'],
  2.                            'company' => $order->fields['billing_company'],
  3.                            'street_address' => $order->fields['billing_street_address'],
  4.                            'suburb' => $order->fields['billing_suburb'],
  5.                            'city' => $order->fields['billing_city'],
  6.                            'postcode' => $order->fields['billing_postcode'],
  7.                            'state' => $order->fields['billing_state'],
  8.                            'country' => $order->fields['billing_country'],
  9.                            'format_id' => $order->fields['billing_address_format_id']);
最后呢就是订单里面的产品信息了$this->products 数组,因为订单里面可能有多个产品,所以当然是保存在一个数组里面了 
  1. $this->products[$index] = array('qty' => $new_qty,
  2.                                       'id' => $orders_products->fields['products_id'],
  3.                                       'name' => $orders_products->fields['products_name'],
  4.                                       'model' => $orders_products->fields['products_model'],
  5.                                       'tax' => $orders_products->fields['products_tax'],
  6.                                       'price' => $orders_products->fields['products_price'],
  7.                                       'final_price' => $orders_products->fields['final_price'],
  8.                                       'onetime_charges' => $orders_products->fields['onetime_charges'],
  9.                                       'products_priced_by_attribute' => $orders_products->fields['products_priced_by_attribute'],
  10.                                       'product_is_free' => $orders_products->fields['product_is_free'],
  11.                                       'products_discount_type' => $orders_products->fields['products_discount_type'],
  12.                                       'products_discount_type_from' => $orders_products->fields['products_discount_type_from']);
这个方法执行完了之后,这个订单的全部信息都可以拿到了,只要引用里面的变量就可以获取订单内容了。 
比如要获取订单里面的产品,就可以这样写,假设订单id是10. 
require(DIR_WS_CLASSES . 'order.php');  
$order = new order(10); 
可以直接给构造函数输入订单信息,看来构造函数的定义就知道了为什么可以这样 
  1. function order($order_id = '') {
  2.     $this->info = array();
  3.     $this->totals = array();
  4.     $this->products = array();
  5.     $this->customer = array();
  6.     $this->delivery = array();
  7.     if (zen_not_null($order_id)) {
  8.       $this->query($order_id);
  9.     } else {
  10.       $this->cart();
  11.     }
  12.   }
默认构造函数参数是空值,也就是生成一个订单,所以它会调用$this->cart();这个函数,而如果这个参数是一个数值的话,那就是调用$this->query($order_id);函数了。 
这个函数执行了之后,这个ID的订单内容就已经获取了。 
然后就可以循环$this->products这个数组获得里面的值 
如:
  1. foreach($order->products as products) {
  2.      echo products['id'];//产品id
  3. echo products['name'];//产品名字
  4. }
大概就是这样,做这个介绍是想给一些想做开发的朋友提供一些参考,希望对zencart开发感兴趣的朋友有帮助 
 
 
 
函数原型代码 
  1. function query($order_id) {
  2.     global $db;
  3.     $order_id = zen_db_prepare_input($order_id);
  4.     $order_query = "select customers_id, customers_name, customers_company,
  5.                          customers_street_address, customers_suburb, customers_city,
  6.                          customers_postcode, customers_state, customers_country,
  7.                          customers_telephone, customers_email_address, customers_address_format_id,
  8.                          delivery_name, delivery_company, delivery_street_address, delivery_suburb,
  9.                          delivery_city, delivery_postcode, delivery_state, delivery_country,
  10.                          delivery_address_format_id, billing_name, billing_company,
  11.                          billing_street_address, billing_suburb, billing_city, billing_postcode,
  12.                          billing_state, billing_country, billing_address_format_id,
  13.                          payment_method, payment_module_code, shipping_method, shipping_module_code,
  14.                          coupon_code, cc_type, cc_owner, cc_number, cc_expires, currency, currency_value,
  15.                          date_purchased, orders_status, last_modified, order_total, order_tax, ip_address
  16.                         from " . TABLE_ORDERS . "
  17.                         where orders_id = '" . (int)$order_id . "'";
  18.     $order = $db->Execute($order_query);
  19.     $totals_query = "select title, text, class
  20.                          from " . TABLE_ORDERS_TOTAL . "
  21.                          where orders_id = '" . (int)$order_id . "'
  22.                          order by sort_order";
  23.     $totals = $db->Execute($totals_query);
  24.     while (!$totals->EOF) {
  25.       if ($totals->fields['class'] == 'ot_coupon') {
  26.         $coupon_link_query = "SELECT coupon_id
  27.                 from " . TABLE_COUPONS . "
  28.                 where coupon_code ='" . $order->fields['coupon_code'] . "'";
  29.         $coupon_link = $db->Execute($coupon_link_query);
  30.         $zc_coupon_link = '<a href="javascript:couponpopupWindow(\'' . zen_href_link(FILENAME_POPUP_COUPON_HELP, 'cID=' . $coupon_link->fields['coupon_id']) . '\')">';
  31.       }
  32.       $this->totals[] = array('title' => ($totals->fields['class'] == 'ot_coupon' ? $zc_coupon_link . $totals->fields['title'] . '</a>' : $totals->fields['title']),
  33.                               'text' => $totals->fields['text'],
  34.                               'class' => $totals->fields['class']);
  35.       $totals->MoveNext();
  36.     }
  37.     $order_total_query = "select text, value
  38.                              from " . TABLE_ORDERS_TOTAL . "
  39.                              where orders_id = '" . (int)$order_id . "'
  40.                              and class = 'ot_total'";
  41.     $order_total = $db->Execute($order_total_query);
  42.     $shipping_method_query = "select title, value
  43.                                 from " . TABLE_ORDERS_TOTAL . "
  44.                                 where orders_id = '" . (int)$order_id . "'
  45.                                 and class = 'ot_shipping'";
  46.     $shipping_method = $db->Execute($shipping_method_query);
  47.     $order_status_query = "select orders_status_name
  48.                              from " . TABLE_ORDERS_STATUS . "
  49.                              where orders_status_id = '" . $order->fields['orders_status'] . "'
  50.                              and language_id = '" . (int)$_SESSION['languages_id'] . "'";
  51.     $order_status = $db->Execute($order_status_query);
  52.     $this->info = array('currency' => $order->fields['currency'],
  53.                         'currency_value' => $order->fields['currency_value'],
  54.                         'payment_method' => $order->fields['payment_method'],
  55.                         'payment_module_code' => $order->fields['payment_module_code'],
  56.                         'shipping_method' => $order->fields['shipping_method'],
  57.                         'shipping_module_code' => $order->fields['shipping_module_code'],
  58.                         'coupon_code' => $order->fields['coupon_code'],
  59.                         'cc_type' => $order->fields['cc_type'],
  60.                         'cc_owner' => $order->fields['cc_owner'],
  61.                         'cc_number' => $order->fields['cc_number'],
  62.                         'cc_expires' => $order->fields['cc_expires'],
  63.                         'date_purchased' => $order->fields['date_purchased'],
  64.                         'orders_status' => $order_status->fields['orders_status_name'],
  65.                         'last_modified' => $order->fields['last_modified'],
  66.                         'total' => $order->fields['order_total'],
  67.                         'tax' => $order->fields['order_tax'],
  68.                         'ip_address' => $order->fields['ip_address']
  69.                         );
  70.     $this->customer = array('id' => $order->fields['customers_id'],
  71.                             'name' => $order->fields['customers_name'],
  72.                             'company' => $order->fields['customers_company'],
  73.                             'street_address' => $order->fields['customers_street_address'],
  74.                             'suburb' => $order->fields['customers_suburb'],
  75.                             'city' => $order->fields['customers_city'],
  76.                             'postcode' => $order->fields['customers_postcode'],
  77.                             'state' => $order->fields['customers_state'],
  78.                             'country' => $order->fields['customers_country'],
  79.                             'format_id' => $order->fields['customers_address_format_id'],
  80.                             'telephone' => $order->fields['customers_telephone'],
  81.                             'email_address' => $order->fields['customers_email_address']);
  82.     $this->delivery = array('name' => $order->fields['delivery_name'],
  83.                             'company' => $order->fields['delivery_company'],
  84.                             'street_address' => $order->fields['delivery_street_address'],
  85.                             'suburb' => $order->fields['delivery_suburb'],
  86.                             'city' => $order->fields['delivery_city'],
  87.                             'postcode' => $order->fields['delivery_postcode'],
  88.                             'state' => $order->fields['delivery_state'],
  89.                             'country' => $order->fields['delivery_country'],
  90.                             'format_id' => $order->fields['delivery_address_format_id']);
  91.     if (empty($this->delivery['name']) && empty($this->delivery['street_address'])) {
  92.       $this->delivery = false;
  93.     }
  94.     $this->billing = array('name' => $order->fields['billing_name'],
  95.                            'company' => $order->fields['billing_company'],
  96.                            'street_address' => $order->fields['billing_street_address'],
  97.                            'suburb' => $order->fields['billing_suburb'],
  98.                            'city' => $order->fields['billing_city'],
  99.                            'postcode' => $order->fields['billing_postcode'],
  100.                            'state' => $order->fields['billing_state'],
  101.                            'country' => $order->fields['billing_country'],
  102.                            'format_id' => $order->fields['billing_address_format_id']);
  103.     $index = 0;
  104.     $orders_products_query = "select orders_products_id, products_id, products_name,
  105.                                  products_model, products_price, products_tax,
  106.                                  products_quantity, final_price,
  107.                                  onetime_charges,
  108.                                  products_priced_by_attribute, product_is_free, products_discount_type,
  109.                                  products_discount_type_from
  110.                                   from " . TABLE_ORDERS_PRODUCTS . "
  111.                                   where orders_id = '" . (int)$order_id . "'
  112.                                   order by orders_products_id";
  113.     $orders_products = $db->Execute($orders_products_query);
  114.     while (!$orders_products->EOF) {
  115.       // convert quantity to proper decimals - account history
  116.       if (QUANTITY_DECIMALS != 0) {
  117.         $fix_qty = $orders_products->fields['products_quantity'];
  118.         switch (true) {
  119.           case (!strstr($fix_qty, '.')):
  120.           $new_qty = $fix_qty;
  121.           break;
  122.           default:
  123.           $new_qty = preg_replace('/[0]+$/', '', $orders_products->fields['products_quantity']);
  124.           break;
  125.         }
  126.       } else {
  127.         $new_qty = $orders_products->fields['products_quantity'];
  128.       }
  129.       $new_qty = round($new_qty, QUANTITY_DECIMALS);
  130.       if ($new_qty == (int)$new_qty) {
  131.         $new_qty = (int)$new_qty;
  132.       }
  133.       $this->products[$index] = array('qty' => $new_qty,
  134.                                       'id' => $orders_products->fields['products_id'],
  135.                                       'name' => $orders_products->fields['products_name'],
  136.                                       'model' => $orders_products->fields['products_model'],
  137.                                       'tax' => $orders_products->fields['products_tax'],
  138.                                       'price' => $orders_products->fields['products_price'],
  139.                                       'final_price' => $orders_products->fields['final_price'],
  140.                                       'onetime_charges' => $orders_products->fields['onetime_charges'],
  141.                                       'products_priced_by_attribute' => $orders_products->fields['products_priced_by_attribute'],
  142.                                       'product_is_free' => $orders_products->fields['product_is_free'],
  143.                                       'products_discount_type' => $orders_products->fields['products_discount_type'],
  144.                                       'products_discount_type_from' => $orders_products->fields['products_discount_type_from']);
  145.       $subindex = 0;
  146.       $attributes_query = "select products_options_id, products_options_values_id, products_options, products_options_values,
  147.                               options_values_price, price_prefix from " . TABLE_ORDERS_PRODUCTS_ATTRIBUTES . "
  148.                                where orders_id = '" . (int)$order_id . "'
  149.                                and orders_products_id = '" . (int)$orders_products->fields['orders_products_id'] . "'";
  150.       $attributes = $db->Execute($attributes_query);
  151.       if ($attributes->RecordCount()) {
  152.         while (!$attributes->EOF) {
  153.           $this->products[$index]['attributes'][$subindex] = array('option' => $attributes->fields['products_options'],
  154.                                                                    'value' => $attributes->fields['products_options_values'],
  155.                                                                    'prefix' => $attributes->fields['price_prefix'],
  156.                                                                    'price' => $attributes->fields['options_values_price']);
  157.           $subindex++;
  158.           $attributes->MoveNext();
  159.         }
  160.       }
  161.       $this->info['tax_groups']["{$this->products[$index]['tax']}"] = '1';
  162.       $index++;
  163.       $orders_products->MoveNext();
  164.     }
  165.     if (FEC_GIFT_WRAPPING_SWITCH == 'true') {
  166.       // Gift wrap
  167.       $wrap_query = "select orders_products_id, wrap from " .
  168.                            TABLE_ORDERS_GIFTWRAP .
  169.                            " where orders_id = '" . (int)$order_id . "'";
  170.       $wraplist = $db->Execute($wrap_query);
  171.       while (!$wraplist->EOF) {
  172.         $this->wrapped [] = array('orders_products_id' => $wraplist->fields['orders_products_id'],
  173.                                 'wrapped' => $wraplist->fields['wrapped']);
  174.         $wraplist->MoveNext();
  175.       }
  176.     }
  177.   }
 
 

zencart外贸网站案例请看http://www.zencar-bbs.com/ 如果QQ在线联系没有反应,请直接加QQ:1023206115 或者 2257583107
关于zen cart的问题请发帖说明,不要发站内信,也不要QQ问,就是问了也没有人回复的。可以发好帖子,发帖子地址给我,叫我回复
!!!我要免费模板!!!
发帖
142
大洋
1047
贡献值
58
只看该作者 地板  发表于: 2012-08-31
zencart外贸建站外贸网站建站深圳外贸B2C建站外贸建站,信用卡通道集成,Amazon分类审核建站。请QQ联系
QQ:1023206115 QQ:2257583107 QQ:3038328260

神啊 学习了

http://www.ahotop.com/

发帖
179
大洋
1010
贡献值
34
只看该作者 板凳  发表于: 2012-08-14
zencart外贸建站外贸网站建站深圳外贸B2C建站外贸建站,信用卡通道集成,Amazon分类审核建站。请QQ联系
QQ:1023206115 QQ:2257583107 QQ:3038328260

解析的很详细,很好很强大

QQ374879357

发帖
112
大洋
1553
贡献值
12
只看该作者 沙发  发表于: 2012-08-14
zencart外贸建站外贸网站建站深圳外贸B2C建站外贸建站,信用卡通道集成,Amazon分类审核建站。请QQ联系
QQ:1023206115 QQ:2257583107 QQ:3038328260

很全面, ~great

快速回复
限80 字节
批量上传需要先选择文件,再选择上传(---删除纯表情和无意义内容,扣1分---)
 
上一个 下一个