opencart一個電商應用的利器,PHP程序員 JAVA程序員 .NET程序員上手開發學習成本較低,良好的MVC模式,思路清晰的國際化處理,完美的商品管理方法,給開發廣大開發人員帶來了衆多的福音。美中不足有幾點: javascript
opencart的註冊須要完成註冊人的地址,公司,公司編碼,還有傳真這些奇葩的字段,這些字段讓親們多麼嘔吐啊!so... 殺之! style='display:none' 樣式表,這美工的橡皮才幫程序員解決註冊的難題啦~! php
打開opencart/catalog/view/theme/default/template/account/register.tpl html
讓咱們盡情的display:none 首先是幹掉fax! java
<tr style='display:none'> <td><?php echo $entry_fax; ?></td> <td><input type="text" name="fax" value="<?php echo $fax; ?>" /></td> </tr>
這個是方法1,方法2:用html的隱藏表單域解決問題 android
<input type="text" name="fax" value="<?php echo $fax; ?>" /> <!-- 幹掉tr td 替換成 --> <input type="hidden" name="fax" value="<?php echo $fax; ?>" />
接下來的公司 和公司id 國家 省份 地址1 等等一些列的都用這些辦法足以搞定了!我對待地址1的以爲辦法就是value='請編輯您的地址' 【有點噁心】 程序員
簡單看了看opencart的訂單流程源碼,大體的流程是下訂單之後,用ajax分步驟處理訂單,前5步都沒有更新order表,只有在最後的確認訂單裏更新了order表。若是一點一點修改它的各個步驟,太複雜了,特別是關於地址的問題,一個shoping_address 一個 payment_address ,搞的我亂七八糟的!全部我放棄了這個想法! ajax
因此複雜問題簡單化,其實訂單類的應用無非就是在訂單表裏新增數據,訂單明細表裏新增數據的事情。
我勇敢的選擇了重作!思路大體以下! cookie
本身作一個checkout的控制器,覆蓋原有的checkout控制器/opencart/catalog/controller/checkout/checkout.php session
<?php class ControllerCheckoutCheckout extends Controller { public function index() { //判斷用戶是否登陸 沒有登陸的去註冊登陸頁面 if (!$this->customer->isLogged()) { //登陸完畢後把親請回來吧 $this->session->data['redirect'] = $this->url->link('checkout/checkout', '', 'SSL'); $this->redirect($this->url->link('account/login', '', 'SSL')); } //設置前臺文字顯示內容 $this->language->load('checkout/mycheckout'); //標題內容 $this->document->setTitle($this->language->get('heading_title')); //麪包渣開始 $this->data['breadcrumbs'] = array(); $this->data['breadcrumbs'][] = array( 'text' => $this->language->get('text_home'), 'href' => $this->url->link('common/home'), 'separator' => false ); $this->data['breadcrumbs'][] = array( 'text' => $this->language->get('heading_title'), 'href' => $this->url->link('checkout/checkout', '', 'SSL'), 'separator' => $this->language->get('text_separator') ); //麪包渣結束 $this->data['heading_title'] = $this->language->get('heading_title'); if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/checkout/mycheckout.tpl')) { $this->template = $this->config->get('config_template') . '/template/checkout/mycheckout.tpl'; } else { $this->template = 'default/template/account/mycheckout.tpl'; } $this->children = array( 'common/content_top', 'common/content_bottom', 'common/footer', 'common/header' ); $this->response->setOutput($this->render()); } } ?>
在建立一個 mycheckout.tpl iphone
opencart/catalog/view/theme/default/template/checkout/mycheckout.tpl
在這裏面讓買家加些地址,配送信息,開-發票信息等!
<?php echo $header; ?> <div id="content"><?php echo $content_top; ?> <div class="breadcrumb"> <?php foreach ($breadcrumbs as $breadcrumb) { ?> <?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a> <?php } ?> </div> <form action='http://localhost/cake/index.php?route=checkout/confirmmy' method='post'> <div> <p>請填寫配送信息:</p> <p>送貨範圍:瀋陽市二環之內免費送貨,二環外加收不等費用,四環外暫不提供送貨。送貨收費標準:二環外,每千米加收2元。四環外暫不提供送貨。</p> <table align="center" width="100%"> <tbody><tr height="25"> <td align="right" width="8%">配送區域:</td> <td>遼寧省瀋陽市- <input type='radio' value='1' name='addressarea' checked="checked"> 二環內 <input type='radio' value='2' name='addressarea' checked=""> 二環外- <select> <option value="和平區">和平區</option> <option value="瀋河區">瀋河區</option> <option value="大東區">大東區</option> <option value="皇姑區">皇姑區</option> <option value="東陵區">東陵區</option> <option value="鐵西區">鐵西區</option> <option value="于洪區">于洪區</option> </select> </td> </tr> <tr height="25"> <td align="right">詳細地址:</td> <td><input name="address" type="text" id="address" value="" size="50"> (必填)</td> </tr> <tr height="25"> <td align="right">收貨人姓名:</td> <td><input name="consignee" type="text" id="consignee" value="" size="20"> (必填) </td> </tr> <tr height="25"> <td align="right">聯繫電話:</td> <td><input name="mobile" type="text" id="mobile" maxlength="11" value="">(必填)</td> </tr> <tr> <td align="right"></td><td><input type="button" class="bt" name="Submit" value="確認收貨人信息"> <input name="address_id" id="addreid" type="hidden" value=""> 信息修改請您點擊保存按鈕</td> </tr> </tbody></table> <p>配送方式及時間:</p> <table> <tr> <td>請選擇配送方式:</td> <!-- 注意 :此處須要javascript shopping_code 問題 配送方式互動 --> <input type='hiden' name="shopping_code" value='xxxx' /> <td><select name='shipping_method'><option value='商家爲您配送'>商家爲您配送</option value='客戶自提'><option>客戶自提</option></select></td> </tr> <tr> <td>配送或者自提時間:</td> <td><input type='text' name='sendtime'/></td> </tr> </table> <p>支付方式:</p> <div> <p> <span> <input type="radio" name="payment_method" value="貨到付款" checked="checked" /> <!-- 注意 :此處須要javascript編碼解決支付代碼問題 與支付方式互動 --> <input type="hidden" name="payment_code" value="20" /> 貨到付款 </span> <span> <input type="radio" name="payment_method" value="支付寶" /> 支付寶 </span> </p> </div> <p>訂單附言:</p> <textarea rows="10" cols="30" name='comment'></textarea> <input type='submit' value='提交'/> </from> </div> <?php echo $content_bottom; ?></div> <?php echo $footer; ?>
由於是測試程序,先後臺都沒有進行校驗。用的時候要當心!
這個表單提交到 /opencart/catalog/controller/checkout/confirmmy.php
因此咱們還要建立一個confirmmy.php
<?php class ControllerCheckoutConfirmmy extends Controller { public function index() { $redirect = ""; if (!$this->cart->hasShipping()) { echo "error"; exit(); } $products = $this->cart->getProducts(); foreach ($products as $product) { $product_total = 0; foreach ($products as $product_2) { if ($product_2['product_id'] == $product['product_id']) { $product_total += $product_2['quantity']; } } if ($product['minimum'] > $product_total) { $redirect = $this->url->link('checkout/cart'); break; } } $total_data = array(); $total = 0; $taxes = $this->cart->getTaxes(); $this->load->model('setting/extension'); ///// $sort_order = array(); $results = $this->model_setting_extension->getExtensions('total'); foreach ($results as $key => $value) { $sort_order[$key] = $this->config->get($value['code'] . '_sort_order'); } array_multisort($sort_order, SORT_ASC, $results); foreach ($results as $result) { if ($this->config->get($result['code'] . '_status')) { $this->load->model('total/' . $result['code']); $this->{'model_total_' . $result['code']}->getTotal($total_data, $total, $taxes); } } $sort_order = array(); foreach ($total_data as $key => $value) { $sort_order[$key] = $value['sort_order']; } array_multisort($sort_order, SORT_ASC, $total_data); $this->language->load('checkout/checkout'); $data = array(); $data['invoice_prefix'] = $this->config->get('config_invoice_prefix'); $data['store_id'] = $this->config->get('config_store_id'); $data['store_name'] = $this->config->get('config_name'); if ($data['store_id']) { $data['store_url'] = $this->config->get('config_url'); } else { $data['store_url'] = HTTP_SERVER; } if ($this->customer->isLogged()) { $data['customer_id'] = $this->customer->getId(); $data['customer_group_id'] = $this->customer->getCustomerGroupId(); $data['firstname'] = $_POST['consignee']; $data['lastname'] = $this->customer->getLastName(); $data['email'] = $this->customer->getEmail(); $data['telephone'] = $_POST['mobile']; $data['fax'] = $this->customer->getFax(); } $data['payment_firstname'] = $_POST['consignee']; //$data['payment_lastname'] = $payment_address['lastname']; $data['payment_company'] = ""; $data['payment_company_id'] = ""; $data['payment_tax_id'] = ""; $data['payment_address_1'] = $_POST['address']; $data['payment_address_2'] = ""; $data['payment_city'] = "瀋陽"; $data['payment_postcode'] = "110032"; $data['payment_zone'] = "遼寧"; $data['payment_zone_id'] = "703"; $data['payment_country'] = "中國"; $data['payment_country_id'] = "44"; $data['payment_address_format'] = ""; $data['payment_method'] = $_POST['payment_method']; $data['payment_code'] = $_POST['payment_code']; $data['shipping_firstname'] = $_POST['consignee']; $data['shipping_company'] = ""; $data['shipping_address_1'] = $_POST['address']; $data['shipping_address_2'] = ""; $data['shipping_city'] = "瀋陽"; $data['shipping_postcode']= "110032"; $data['shipping_zone'] = "遼寧"; $data['shipping_zone_id'] = "703"; $data['shipping_country'] = "中國"; $data['shipping_country_id'] = "44"; $data['shipping_address_format'] = ""; $data['shipping_method'] = $_POST['payment_method']; $data['shipping_code'] = $_POST['payment_code']; //////////////////////////////////////////////////////////////////////// $product_data = array(); foreach ($this->cart->getProducts() as $product) { $option_data = array(); foreach ($product['option'] as $option) { if ($option['type'] != 'file') { $value = $option['option_value']; } else { $value = $this->encryption->decrypt($option['option_value']); } $option_data[] = array( 'product_option_id' => $option['product_option_id'], 'product_option_value_id' => $option['product_option_value_id'], 'option_id' => $option['option_id'], 'option_value_id' => $option['option_value_id'], 'name' => $option['name'], 'value' => $value, 'type' => $option['type'] ); } $product_data[] = array( 'product_id' => $product['product_id'], 'name' => $product['name'], 'model' => $product['model'], 'option' => $option_data, 'download' => $product['download'], 'quantity' => $product['quantity'], 'subtract' => $product['subtract'], 'price' => $product['price'], 'total' => $product['total'], 'tax' => $this->tax->getTax($product['price'], $product['tax_class_id']), 'reward' => $product['reward'] ); } // Gift Voucher $voucher_data = array(); if (!empty($this->session->data['vouchers'])) { foreach ($this->session->data['vouchers'] as $voucher) { $voucher_data[] = array( 'description' => $voucher['description'], 'code' => substr(md5(mt_rand()), 0, 10), 'to_name' => $voucher['to_name'], 'to_email' => $voucher['to_email'], 'from_name' => $voucher['from_name'], 'from_email' => $voucher['from_email'], 'voucher_theme_id' => $voucher['voucher_theme_id'], 'message' => $voucher['message'], 'amount' => $voucher['amount'] ); } } $data['products'] = $product_data; $data['vouchers'] = $voucher_data; $data['totals'] = $total_data; $data['comment'] = $_POST['comment']; $data['total'] = $total; ////////////////////////////////////////// if (isset($this->request->cookie['tracking'])) { $this->load->model('affiliate/affiliate'); $affiliate_info = $this->model_affiliate_affiliate->getAffiliateByCode($this->request->cookie['tracking']); $subtotal = $this->cart->getSubTotal(); if ($affiliate_info) { $data['affiliate_id'] = $affiliate_info['affiliate_id']; $data['commission'] = ($subtotal / 100) * $affiliate_info['commission']; } else { $data['affiliate_id'] = 0; $data['commission'] = 0; } } else { $data['affiliate_id'] = 0; $data['commission'] = 0; } $data['language_id'] = $this->config->get('config_language_id'); $data['currency_id'] = $this->currency->getId(); $data['currency_code'] = $this->currency->getCode(); $data['currency_value'] = $this->currency->getValue($this->currency->getCode()); $data['ip'] = $this->request->server['REMOTE_ADDR']; if (!empty($this->request->server['HTTP_X_FORWARDED_FOR'])) { $data['forwarded_ip'] = $this->request->server['HTTP_X_FORWARDED_FOR']; } elseif(!empty($this->request->server['HTTP_CLIENT_IP'])) { $data['forwarded_ip'] = $this->request->server['HTTP_CLIENT_IP']; } else { $data['forwarded_ip'] = ''; } if (isset($this->request->server['HTTP_USER_AGENT'])) { $data['user_agent'] = $this->request->server['HTTP_USER_AGENT']; } else { $data['user_agent'] = ''; } if (isset($this->request->server['HTTP_ACCEPT_LANGUAGE'])) { $data['accept_language'] = $this->request->server['HTTP_ACCEPT_LANGUAGE']; } else { $data['accept_language'] = ''; } $this->load->model('checkout/order'); $this->session->data['order_id'] = $this->model_checkout_order->addOrder($data); $this->data['column_name'] = $this->language->get('column_name'); $this->data['column_model'] = $this->language->get('column_model'); $this->data['column_quantity'] = $this->language->get('column_quantity'); $this->data['column_price'] = $this->language->get('column_price'); $this->data['column_total'] = $this->language->get('column_total'); $this->data['products'] = array(); foreach ($this->cart->getProducts() as $product) { $option_data = array(); foreach ($product['option'] as $option) { if ($option['type'] != 'file') { $value = $option['option_value']; } else { $filename = $this->encryption->decrypt($option['option_value']); $value = utf8_substr($filename, 0, utf8_strrpos($filename, '.')); } $option_data[] = array( 'name' => $option['name'], 'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value) ); } $this->data['products'][] = array( 'product_id' => $product['product_id'], 'name' => $product['name'], 'model' => $product['model'], 'option' => $option_data, 'quantity' => $product['quantity'], 'subtract' => $product['subtract'], 'price' => $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax'))), 'total' => $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')) * $product['quantity']), 'href' => $this->url->link('product/product', 'product_id=' . $product['product_id']) ); } // Gift Voucher $this->data['vouchers'] = array(); if (!empty($this->session->data['vouchers'])) { foreach ($this->session->data['vouchers'] as $voucher) { $this->data['vouchers'][] = array( 'description' => $voucher['description'], 'amount' => $this->currency->format($voucher['amount']) ); } } $this->data['totals'] = $total_data; $this->data['payment'] = $_POST['payment_method']; $redirect = $this->url->link('checkout/success', '', 'SSL'); $this->data['redirect'] = $redirect; if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/checkout/confirm.tpl')) { $this->template = $this->config->get('config_template') . '/template/checkout/confirm.tpl'; } else { $this->template = 'default/template/checkout/confirm.tpl'; } $this->response->setOutput($this->render()); } } ?>這樣基本上大功告成了!
要發票嗎? 若是要發票的話須要在order表中加字段,而後修改/open/catalog/model/checkout/order.php中的addOrder方法 把發票字段賦值。此處省略一萬字!
其實這就是一個簡化的思路,並非簡化的成品,有不少地方存在不足,和漏洞,但願你們可以相互溝通來完善中國特點的opencart!
我期待和你們交流Opencart 二次開發,也期待作APP的朋友與我交流,如今正在改opencart 與APP交互【 iphone and android 】