OpenCart数据字段改造总结

添加数据库表字段

oc_address 添加字段

  • district
  • street_name
  • house_number

oc_order 添加字段

  • doc_type
  • doc_no
  • payment_district
  • payment_street_name
  • payment_house_number
  • shipping_district
  • shipping_street_name
  • shipping_house_number

oc_customer 添加字段

  • doc_type
  • doc_no

修改Model

catalog/model/account/address.php

public function addAddress($customer_id, $data) {
    $this->db->query("INSERT INTO " . DB_PREFIX . "address SET customer_id = '" . (int)$customer_id . "', firstname = '" . $this->db->escape($data['firstname']) ... . "', district = '" . $this->db->escape($data['district']) . "', street_name = '" . $this->db->escape($data['street_name']) . "', house_number = '" . $this->db->escape($data['house_number']) ...);

    ...
}

public function editAddress($address_id, $data) {
    $this->db->query("UPDATE " . DB_PREFIX . "address SET firstname = '" . $this->db->escape($data['firstname']) ... . "', district = '" . $this->db->escape($data['district']) . "', street_name = '" . $this->db->escape($data['street_name']) . "', house_number = '" . $this->db->escape($data['house_number']) ...);

    ...
}

public function getAddress($address_id) {
    ...

    if ($address_query->num_rows) {
        ...

        $address_data = array(
            ...
            'district'       => $address_query->row['district'],
            'street_name'    => $address_query->row['street_name'],
            'house_number'   => $address_query->row['house_number'],
            ...
}

public function getAddresses() {
    ...

    foreach ($query->rows as $result) {
        ...

        $address_data[$result['address_id']] = array(
            ...
            'district'       => $result['district'],
            'street_name'    => $result['street_name'],
            'house_number'   => $result['house_number'],
            ...
}

catalog/model/account/order.php

public function getOrder($order_id) {
    ...

    if ($order_query->num_rows) {
        ...

        return array(
            ...
            'doc_type'                => $order_query->row['doc_type'],
            'doc_no'                  => $order_query->row['doc_no'],
            ...
            'payment_district'        => $order_query->row['payment_district'],
            'payment_street_name'     => $order_query->row['payment_street_name'],
            'payment_house_number'    => $order_query->row['payment_house_number'],
            ...
            'shipping_district'       => $order_query->row['shipping_district'],
            'shipping_street_name'    => $order_query->row['shipping_street_name'],
            'shipping_house_number'   => $order_query->row['shipping_house_number'],
            ...
        );

catalog/model/account/customer.php

public function editCustomer($customer_id, $data) {
    $this->db->query("UPDATE " . DB_PREFIX . "customer SET firstname = '" . $this->db->escape($data['firstname']) ... . "', doc_type = '" . $this->db->escape($data['doc_type']) . "', doc_no = '" . $this->db->escape($data['doc_no']) ...);
}

opencart/catalog/model/checkout/order.php

public function addOrder($data) {
    $this->db->query("INSERT INTO `" . DB_PREFIX . "order` SET invoice_prefix = '" . $this->db->escape($data['invoice_prefix']) ... . "', doc_type = '" . $this->db->escape($data['doc_type']) . "', doc_no = '" . $this->db->escape($data['doc_no']) ... . "', payment_district = '" . $this->db->escape($data['payment_district']) . "', payment_street_name = '" . $this->db->escape($data['payment_street_name']) . "', payment_house_number = '" . $this->db->escape($data['payment_house_number']) ... . "', shipping_district = '" . $this->db->escape($data['shipping_district']) . "', shipping_street_name = '" . $this->db->escape($data['shipping_street_name']) . "', shipping_house_number = '" . $this->db->escape($data['shipping_house_number']) ....);

    ...
}

public function editOrder($order_id, $data) {
    ...
    $this->db->query("UPDATE `" . DB_PREFIX . "order` SET invoice_prefix = '" . $this->db->escape($data['invoice_prefix']) ... . "', doc_type = '" . $this->db->escape($data['doc_type']) . "', doc_no = '" . $this->db->escape($data['doc_no']) ... . "', payment_district = '" . $this->db->escape($data['payment_district']) . "', payment_street_name = '" . $this->db->escape($data['payment_street_name']) . "', payment_house_number = '" . $this->db->escape($data['payment_house_number']) ... . "', shipping_district = '" . $this->db->escape($data['shipping_district']) . "', shipping_street_name = '" . $this->db->escape($data['shipping_street_name']) . "', shipping_house_number = '" . $this->db->escape($data['shipping_house_number']) ...);

    ...
}

public function getOrder($order_id) {
    ...

    if ($order_query->num_rows) {
        ...

        $order_products = $this->db->query("SELECT `name`,`quantity` as `qty`,`price` FROM `" . DB_PREFIX . "order_product` WHERE order_id = $order_id");

        ...

        return array(
            'order_id'                => $order_query->row['order_id'],
            'products'                => $order_products->rows,
            ...
            'doc_type'                => $order_query->row['doc_type'],
            'doc_no'                  => $order_query->row['doc_no'],
            ...
            'payment_district'        => $order_query->row['payment_district'],
            'payment_street_name'     => $order_query->row['payment_street_name'],
            'payment_house_number'    => $order_query->row['payment_house_number'],
            ...
            'shipping_district'       => $order_query->row['shipping_district'],
            'shipping_street_name'    => $order_query->row['shipping_street_name'],
            'shipping_house_number'   => $order_query->row['shipping_house_number'],
            ...
}

修改Language

catalog/language/en-gb/account/address.php
catalog/language/en-gb/checkout/checkout.php

修改Controller

catalog/controller/account/address.php

protected function getForm() {
    ...

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

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

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

    ...
}

catalog/controller/account/edit.php

public function index() {
    ...

    if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
        $this->model_account_customer->editCustomer($this->customer->getId(), $this->request->post);

        $this->session->data['success'] = $this->language->get('text_success');

        $this->response->redirect($this->url->link('account/account', '', true));
    }

    ...

    if (isset($this->error['doc_type'])) {
        $data['error_doc_type'] = $this->error['doc_type'];
    } else {
        $data['error_doc_type'] = '';
    }

    if (isset($this->error['doc_no'])) {
        $data['error_doc_no'] = $this->error['doc_no'];
    } else {
        $data['error_doc_no'] = '';
    }

    ...

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

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

    ...
}

catalog/controller/checkout/confirm.php

public function index() {
    ...

    if (!$redirect) {
        ...

        $this->load->model('account/customer');

        if ($this->customer->isLogged()) {
            $customer_info = $this->model_account_customer->getCustomer($this->customer->getId());

            $order_data['customer_id'] = $this->customer->getId();
            ...

            $order_data['doc_type'] = empty($customer_info['doc_type']) ? $this->session->data['doc_type'] : $customer_info['doc_type'];
            $order_data['doc_no'] = empty($customer_info['doc_type']) ? $this->session->data['doc_no'] : $customer_info['doc_no'];
            ...

        } elseif (isset($this->session->data['guest'])) {
            $order_data['customer_id'] = 0;
            ...

            $order_data['doc_type'] = $this->session->data['guest']['doc_type'];
            $order_data['doc_no'] = $this->session->data['guest']['doc_no'];
            ...

        }

        ...

        $order_data['payment_district'] = $this->session->data['payment_address']['district'];
        $order_data['payment_street_name'] = $this->session->data['payment_address']['street_name'];
        $order_data['payment_house_number'] = $this->session->data['payment_address']['house_number'];

        ...

        if ($this->cart->hasShipping()) {
            ...

            $order_data['shipping_district'] = $this->session->data['shipping_address']['district'];
            $order_data['shipping_street_name'] = $this->session->data['shipping_address']['street_name'];
            $order_data['shipping_house_number'] = $this->session->data['shipping_address']['house_number'];
            ...

        } else {
            ...

            $order_data['shipping_district'] = '';
            $order_data['shipping_street_name'] = '';
            $order_data['shipping_house_number'] = '';
            ...
}

修改Twig模板

catalog/view/theme/default/template/account/address_form.twig
catalog/view/theme/default/template/account/edit.twig
catalog/view/theme/default/template/checkout/payment_address.twig

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

THE END
分享
二维码
打赏
海报
OpenCart数据字段改造总结
添加数据库表字段 oc_address 添加字段 district street_name house_number oc_order 添加字段 doc_type doc_no payment_district payment_street_name pa……
<<上一篇
下一篇>>
文章目录
关闭
目 录