OpenCart开发总结

获取购物车商品

$products = $this->cart->getProducts();

获取Customer登录状态

$data['logged'] = $this->customer->isLogged();

获取登录Customer信息

$this->load->model('account/customer');
$customer_info = array();
if ($this->customer->isLogged()) {
    $customer_info = $this->model_account_customer->getCustomer($this->customer->getId());
}

获取订单包含商品信息

$order_products = $this->db->query("SELECT `name`,`quantity` as `qty`,`price` FROM `" . DB_PREFIX . "order_product` WHERE order_id = $order_id");
$products = $order_products->rows;  //多条记录
$product = $order_products->row;  //单条记录

catalog/model/checkout/order.php

public function getOrder($order_id) {
    $order_products = $this->db->query("SELECT `name`,`quantity`,`price` FROM `" . DB_PREFIX . "order_product` WHERE order_id = $order_id");
    //$products = json_encode($order_products->row, JSON_UNESCAPED_SLASHES);
    return array(
        'order_id'                => $order_query->row['order_id'],
        'products'                => $order_products->rows,
        ...
    )
}

Checkout支付方式页提交参数

catalog/view/theme/default/template/checkout/payment_method.twig

<p><strong><span style="color: #FF0000;">* </span>Document Type</strong></p>
<p>
  <input name="doc_type" class="form-control" value="{{ doc_type }}"/>
</p>
<p><strong><span style="color: #FF0000;">* </span>Document No.</strong></p>
<p>
  <input name="doc_no" class="form-control" value="{{ doc_no }}"/>
</p>

...

$(document).delegate('#button-payment-method', 'click', function() {
    $.ajax({
        url: 'index.php?route=checkout/payment_method/save',
        type: 'post',
        data: $('#collapse-payment-method input[type=\'radio\']:checked, #collapse-payment-method input[type=\'checkbox\']:checked, #collapse-payment-method textarea, #collapse-payment-method input[name=\'doc_type\'], #collapse-payment-method input[name=\'doc_no\']'),

Checkout地址详情页校验地址

catalog/controller/checkout/payment_address.php

$this->load->model('account/address');
if (isset($this->session->data['payment_address']['address_id'])) {
    $address_id = $this->session->data['payment_address']['address_id'];
} else {
    $address_id = $this->customer->getAddressId();
}

$address = $this->model_account_address->getAddress($address_id);
if (empty($address['city']) || empty($address['district']) || empty($address['street_name']) || empty($address['house_number']) || empty($address['postcode'])) {
    $json['error']['warning'] = 'Warning: You must complete your address info to continue!';
}

Checkout支付方式页校验证件

catalog/controller/checkout/payment_method.php

if ($information_info && !isset($this->request->post['agree'])) {
    $json['error']['warning'] = sprintf($this->language->get('error_agree'), $information_info['title']);
} else {
    //证件号
    /*
    $this->load->model('account/customer');
    $document = '';
    if ($this->customer->isLogged()) {
        $customer_info = $this->model_account_customer->getCustomer($this->customer->getId());
        $document = $customer_info['doc_no'];
    }

    if (empty($document)) {
        $json['error']['warning'] = 'Warning: You must set the identity document (like CPF) info for your account!';
    }
    */

    if (!isset($this->request->post['doc_type']) || empty($this->request->post['doc_type'])
        || !isset($this->request->post['doc_no']) || empty($this->request->post['doc_no'])) {
        $json['error']['warning'] = 'Warning: You must set the identity document (like CPF) info for your account!';
    }
}

Checkout支付方式页添加证件信息Session

catalog/controller/checkout/payment_method.php

if (!$json) {
    $this->session->data['payment_method'] = $this->session->data['payment_methods'][$this->request->post['payment_method']];

    $this->session->data['comment'] = strip_tags($this->request->post['comment']);

    //证件信息Session
    if ($this->customer->isLogged()) {
        $this->session->data['doc_type'] = $this->request->post['doc_type'];
        $this->session->data['doc_no'] = $this->request->post['doc_no'];
    } else {
        $this->session->data['guest']['doc_type'] = $this->request->post['doc_type'];
        $this->session->data['guest']['doc_no'] = $this->request->post['doc_no'];
    }
}

改造Checkout注册页面

catalog/view/theme/default/template/checkout/register.twig

<div class="form-group required">
  <label class="control-label" for="input-payment-district">{{ entry_district }}</label>
  <input type="text" name="district" value="" placeholder="{{ entry_district }}" id="input-payment-district" class="form-control" />
</div>
<div class="form-group required">
  <label class="control-label" for="input-payment-street_name">{{ entry_street_name }}</label>
  <input type="text" name="street_name" value="" placeholder="{{ entry_street_name }}" id="input-payment-street_name" class="form-control" />
</div>
<div class="form-group required">
  <label class="control-label" for="input-payment-house_number">{{ entry_house_number }}</label>
  <input type="text" name="house_number" value="" placeholder="{{ entry_house_number }}" id="input-payment-house_number" class="form-control" />
</div>

Guest用户Checkout改造

catalog/controller/checkout/guest.php

public function index() {
    ...

    if (isset($this->session->data['payment_address']['district'])) {
        $data['district'] = $this->session->data['payment_address']['district'];
    } else {
        $data['district'] = '';
    }

    if (isset($this->session->data['payment_address']['street_name'])) {
        $data['street_name'] = $this->session->data['payment_address']['street_name'];
    } else {
        $data['street_name'] = '';
    }

    if (isset($this->session->data['payment_address']['house_number'])) {
        $data['house_number'] = $this->session->data['payment_address']['house_number'];
    } else {
        $data['house_number'] = '';
    }

    ...
}

public function save() {
    ...

    if (!$json) {
        $this->session->data['account'] = 'guest';

        $this->session->data['guest']['customer_group_id'] = $customer_group_id;
        $this->session->data['guest']['firstname'] = $this->request->post['firstname'];
        $this->session->data['guest']['lastname'] = $this->request->post['lastname'];
        $this->session->data['guest']['email'] = $this->request->post['email'];
        $this->session->data['guest']['telephone'] = $this->request->post['telephone'];
        ...

        $this->session->data['payment_address']['district'] = $this->request->post['district'];
        $this->session->data['payment_address']['street_name'] = $this->request->post['street_name'];
        $this->session->data['payment_address']['house_number'] = $this->request->post['house_number'];
        ...

        if ($this->session->data['guest']['shipping_address']) {
            ...

            $this->session->data['shipping_address']['district'] = $this->request->post['district'];
            $this->session->data['shipping_address']['street_name'] = $this->request->post['street_name'];
            $this->session->data['shipping_address']['house_number'] = $this->request->post['house_number'];
            ...
}

catalog/view/theme/default/template/checkout/guest.twig

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/26/opencart-development-summary/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
OpenCart开发总结
获取购物车商品 $products = $this->cart->getProducts(); 获取Customer登录状态 $data['logged'] = $this->customer->isLogged(); 获……
<<上一篇
下一篇>>
文章目录
关闭
目 录