OpenCart — точки самовывоза. Часть 3

Добрый день, уважаемые читатели!

Сегодня мы перейдем к заключительному этапу написания модуля для OpenCart — точки самовывоза. Мы рассмотрим обработку полученной информации и запись в БД.

Начнем.

Первое, что нам нужно сделать — это добавить в контроллер информацию о магазине, в который выбрана доставка.

Открываем файл

/catalog/controller/checkout/simplecheckout_shipping.php

и после


if (isset($this->data['shipping_method'])) {
 setcookie('shipping_method', $this->data['shipping_method']['code'], time() + 60 * 60 * 24 * 30);
 }

вставляем следующий код:


// номер магазина
 $this->data['shipping_method']['shop_id'] = !empty($this->request->post['shop_id']) ? $this->request->post['shop_id'] : '';
 $this->data['shop_id'] = $this->data['shipping_method']['shop_id'];

теперь открываем файл:

/catalog/controller/checkout/simplecheckout.php

Находим функцию order() и в ней после


if (isset($shipping_method['code'])) {
 $data['shipping_code'] = $shipping_method['code'];
 } else {
 $data['shipping_code'] = '';
 }

добавляем


if(isset($shipping_method['shop_id'])) {
 $data['shop_id'] = $shipping_method['shop_id'];
 } else {
 $data['shop_id'] = '';
 }

Небольшое лирическое отступление для понимания того, что мы делаем.

Главная функция index() контроллера ControllerCheckoutSimpleCheckout собирает всю информацию о заказе вместе и передает ее в функцию order() через собственную библиотеку (/system/library/simple/simple.php), потом информация обрабатывается и передается в модель /catalog/model/checkout/order.php. Соответственно, следующим шагом будет обработка данных в модели.

Перед редактированием модели, нужно добавить одно поле в PhpMyAdmin. Заходим в таблицу «order» -> структура -> добавить одно поле (между какими ключами добавлять — решать вам, я добавил после «shipping_code») ->

Имя — Тип — Длина/значение

pickup_point — int — 11

Открываем модель:

/catalog/model/checkout/order.php

В функцию addOrder() первыми строчками пишем:


if($data['shipping_code'] == 'pickup_point.pickup_point') {
 $pickup_point_sql = "pickup_point = '" . (int)$data['shop_id'] . "', ";
 } else {
 $pickup_point_sql = '';
 }

Далее идем запрос:


$this->db->query("INSERT INTO `" . DB_PREFIX . "order` SET invoice_prefix = '" . $this->db->escape($data['invoice_prefix']) . "', store_id = '" . (int)$data['store_id'] . "', store_name = '" . $this->db->escape($data['store_name']) . "', store_url = '" . $this->db->escape($data['store_url']) . "', customer_id = '" . (int)$data['customer_id'] . "', customer_group_id = '" . (int)$data['customer_group_id'] . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "', shipping_firstname = '" . $this->db->escape($data['shipping_firstname']) . "', shipping_lastname = '" . $this->db->escape($data['shipping_lastname']) . "', shipping_company = '" . $this->db->escape($data['shipping_company']) . "', shipping_address_1 = '" . $this->db->escape($data['shipping_address_1']) . "', shipping_address_2 = '" . $this->db->escape($data['shipping_address_2']) . "', shipping_city = '" . $this->db->escape($data['shipping_city']) . "', shipping_postcode = '" . $this->db->escape($data['shipping_postcode']) . "', shipping_country = '" . $this->db->escape($data['shipping_country']) . "', shipping_country_id = '" . (int)$data['shipping_country_id'] . "', shipping_zone = '" . $this->db->escape($data['shipping_zone']) . "', shipping_zone_id = '" . (int)$data['shipping_zone_id'] . "', shipping_address_format = '" . $this->db->escape($data['shipping_address_format']) . "', shipping_method = '" . $this->db->escape($data['shipping_method']) . "', shipping_code = '" . $this->db->escape($data['shipping_code']) . "', payment_firstname = '" . $this->db->escape($data['payment_firstname']) . "', payment_lastname = '" . $this->db->escape($data['payment_lastname']) . "', payment_company = '" . $this->db->escape($data['payment_company']) . "', payment_address_1 = '" . $this->db->escape($data['payment_address_1']) . "', payment_address_2 = '" . $this->db->escape($data['payment_address_2']) . "', payment_city = '" . $this->db->escape($data['payment_city']) . "', payment_postcode = '" . $this->db->escape($data['payment_postcode']) . "', payment_country = '" . $this->db->escape($data['payment_country']) . "', payment_country_id = '" . (int)$data['payment_country_id'] . "', payment_zone = '" . $this->db->escape($data['payment_zone']) . "', payment_zone_id = '" . (int)$data['payment_zone_id'] . "', payment_address_format = '" . $this->db->escape($data['payment_address_format']) . "', payment_method = '" . $this->db->escape($data['payment_method']) . "', payment_code = '" . $this->db->escape($data['payment_code']) . "', comment = '" . $this->db->escape($data['comment']) . "', total = '" . (float)$data['total'] . "', affiliate_id = '" . (int)$data['affiliate_id'] . "', commission = '" . (float)$data['commission'] . "', language_id = '" . (int)$data['language_id'] . "', currency_id = '" . (int)$data['currency_id'] . "', currency_code = '" . $this->db->escape($data['currency_code']) . "', currency_value = '" . (float)$data['currency_value'] . "', ip = '" . $this->db->escape($data['ip']) . "', forwarded_ip = '" .  $this->db->escape($data['forwarded_ip']) . "', user_agent = '" . $this->db->escape($data['user_agent']) . "', accept_language = '" . $this->db->escape($data['accept_language']) . "', date_added = NOW(), date_modified = NOW()");

Его нужно немного модернизировать:

между запросов shipping_code и payment_firstname добавляем нашу переменную $pickup_point_sql:


$this->db->query("INSERT INTO `" . DB_PREFIX . "order` SET invoice_prefix = '" . $this->db->escape($data['invoice_prefix']) . "', store_id = '" . (int)$data['store_id'] . "', store_name = '" . $this->db->escape($data['store_name']) . "', store_url = '" . $this->db->escape($data['store_url']) . "', customer_id = '" . (int)$data['customer_id'] . "', customer_group_id = '" . (int)$data['customer_group_id'] . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "', shipping_firstname = '" . $this->db->escape($data['shipping_firstname']) . "', shipping_lastname = '" . $this->db->escape($data['shipping_lastname']) . "', shipping_company = '" . $this->db->escape($data['shipping_company']) . "', shipping_address_1 = '" . $this->db->escape($data['shipping_address_1']) . "', shipping_address_2 = '" . $this->db->escape($data['shipping_address_2']) . "', shipping_city = '" . $this->db->escape($data['shipping_city']) . "', shipping_postcode = '" . $this->db->escape($data['shipping_postcode']) . "', shipping_country = '" . $this->db->escape($data['shipping_country']) . "', shipping_country_id = '" . (int)$data['shipping_country_id'] . "', shipping_zone = '" . $this->db->escape($data['shipping_zone']) . "', shipping_zone_id = '" . (int)$data['shipping_zone_id'] . "', shipping_address_format = '" . $this->db->escape($data['shipping_address_format']) . "', shipping_method = '" . $this->db->escape($data['shipping_method']) . "', <strong>shipping_code = '" . $this->db->escape($data['shipping_code']) . "', ". $pickup_point_sql ." payment_firstname = '" . $this->db->escape($data['payment_firstname']) . "'</strong>, payment_lastname = '" . $this->db->escape($data['payment_lastname']) . "', payment_company = '" . $this->db->escape($data['payment_company']) . "', payment_address_1 = '" . $this->db->escape($data['payment_address_1']) . "', payment_address_2 = '" . $this->db->escape($data['payment_address_2']) . "', payment_city = '" . $this->db->escape($data['payment_city']) . "', payment_postcode = '" . $this->db->escape($data['payment_postcode']) . "', payment_country = '" . $this->db->escape($data['payment_country']) . "', payment_country_id = '" . (int)$data['payment_country_id'] . "', payment_zone = '" . $this->db->escape($data['payment_zone']) . "', payment_zone_id = '" . (int)$data['payment_zone_id'] . "', payment_address_format = '" . $this->db->escape($data['payment_address_format']) . "', payment_method = '" . $this->db->escape($data['payment_method']) . "', payment_code = '" . $this->db->escape($data['payment_code']) . "', comment = '" . $this->db->escape($data['comment']) . "', total = '" . (float)$data['total'] . "', affiliate_id = '" . (int)$data['affiliate_id'] . "', commission = '" . (float)$data['commission'] . "', language_id = '" . (int)$data['language_id'] . "', currency_id = '" . (int)$data['currency_id'] . "', currency_code = '" . $this->db->escape($data['currency_code']) . "', currency_value = '" . (float)$data['currency_value'] . "', ip = '" . $this->db->escape($data['ip']) . "', forwarded_ip = '" .  $this->db->escape($data['forwarded_ip']) . "', user_agent = '" . $this->db->escape($data['user_agent']) . "', accept_language = '" . $this->db->escape($data['accept_language']) . "', date_added = NOW(), date_modified = NOW()");

Теперь в БД будет добавляться ID магазина, который вы добавляли в админ-панели в поле «Номер магазина». Далее мы это используем, чтобы вывести название магазина (или его адрес) в админ-панели в просмотре информации о заказе.

Собственно, этим сейчас и займемся.

Открываем контроллер:

/admin/controller/sale/order.php

в функцию info() ищем


$this->data['text_shipping_method'] = $this->language->get('text_shipping_method');

и добавляем после:


$this->data['text_shipping_method_shop_address'] = $this->language->get('text_shipping_shop_address');

Ищем далее:


$this->data['shipping_method'] = $order_info['shipping_method'];

и после вставляем:


$this->data['shipping_method_shop_address'] = '';
 if(!empty($order_info['pickup_point'])) {

$shops = $this->config->get('pickup_point_shops');

foreach($shops as $shop) {

if($shop['shop_number'] == $order_info['pickup_point']) {
 $this->data['shipping_method_shop_address'] = $shop['shop_address'];
 }

}

}

Открываем языковой файл:

/admin/language/russian/sale/order.php

И вставляем строчку:


$_['text_shipping_shop_address']          = 'Адрес магазина:';

Открываем модель:

/admin/model/sale/order.php

Ищем функцию getOrder(), в ней ищем


return array(
 'order_id' => $order_query->row['order_id'],
 'order_id_1c' => $order_query->row['order_id_1c'],
 'invoice_no' => $order_query->row['invoice_no'],
 'invoice_prefix' => $order_query->row['invoice_prefix'],
 'store_id' => $order_query->row['store_id'],
 'store_name' => $order_query->row['store_name'],
 'store_url' => $order_query->row['store_url'], // <-- большой массив, который возвращает информацию о заказе

и в этом массиве после строчки

'shipping_method'         => $order_query->row['shipping_method'],

добавляем строчку

'pickup_point'            => $order_query->row['pickup_point'],

Открываем файл вида

/admin/view/template/sale/order_info.tpl

Ищем там:


<?php if ($shipping_method) { ?>
 <tr>
 <td><?php echo $text_shipping_method; ?></td>
 <td><?php echo $shipping_method; ?></td>
 </tr>
 <?php } ?>

и после вставляем:


<?php if ($shipping_method_shop_address) { ?>
 <tr>
 <td><?php echo $text_shipping_method_shop_address; ?></td>
 <td><?php echo $shipping_method_shop_address; ?></td>
 </tr>
 <?php } ?>

Вот и все! Наш модуль «Точки самовывоза для OpenCart» готов! Задавайте вопросы в комментариях.

До новых встреч!

6 комментариев

Leave a Reply