搜索
首页web前端html教程OpenCart教程:自定义配送方式(第一部分)

虽然 OpenCart 的核心本身提供了许多有用的运输方法,但您总有可能需要创建自己的运输方法。另一方面,作为一名 Web 开发人员,您将始终尝试探索您选择的框架,以了解如何创建您自己的自定义内容!

在本系列中,我们将在 OpenCart 中创建自定义运输方法模块。这将是一个由两部分组成的系列,在第一部分中,我们将为自定义运输方法创建一个后端配置表单。

要在 OpenCart 中创建新的自定义运输方法,需要按照 OpenCart 的约定实施文件。在后端,您需要提供一个配置表单,允许管理员配置价格、地理区域以及与运输方式相关的其他参数。在前端,您将实现所需的文件,以便在结账时选择您的自定义送货方式!

今天,我们将完成后端设置。我假设您使用的是最新版本的 OpenCart。在第二部分中,我们将探索前端对应部分,其中我们将看到前端文件设置和前端演示。

后端文件设置概览

让我们从后端所需的文件列表开始。我们将使用“custom”作为自定义送货方式的名称。

  • admin/controller/shipping/custom.php:这是一个控制器文件,我们将在其中设置配置表单所需的所有内容。
  • admin/language/english/shipping/custom.php:这是一个语言文件,我们将在其中定义表单的标签。
  • admin/view/template/shipping/custom.tpl:这是一个视图模板文件,其中包含我们的配置表单的 HTML 代码。

这就是后端设置的情况。

文件设置

让我们从控制器设置开始。

创建控制器文件

创建文件 admin/controller/shipping/custom.php 并将以下内容粘贴到该文件中。

<?php
class ControllerShippingCustom extends Controller {
  private $error = array();

  public function index() {
    $this->load->language('shipping/custom');

    $this->document->setTitle($this->language->get('heading_title'));

    $this->load->model('setting/setting');

    if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
      $this->model_setting_setting->editSetting('custom', $this->request->post);

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

      $this->response->redirect($this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL'));
    }

    $data['heading_title'] = $this->language->get('heading_title');
    
    $data['text_edit'] = $this->language->get('text_edit');
    $data['text_enabled'] = $this->language->get('text_enabled');
    $data['text_disabled'] = $this->language->get('text_disabled');
    $data['text_all_zones'] = $this->language->get('text_all_zones');
    $data['text_none'] = $this->language->get('text_none');

    $data['entry_cost'] = $this->language->get('entry_cost');
    $data['entry_tax_class'] = $this->language->get('entry_tax_class');
    $data['entry_geo_zone'] = $this->language->get('entry_geo_zone');
    $data['entry_status'] = $this->language->get('entry_status');
    $data['entry_sort_order'] = $this->language->get('entry_sort_order');

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

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

    $data['breadcrumbs'] = array();

    $data['breadcrumbs'][] = array(
      'text' => $this->language->get('text_home'),
      'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
    );

    $data['breadcrumbs'][] = array(
      'text' => $this->language->get('text_shipping'),
      'href' => $this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL')
    );

    $data['breadcrumbs'][] = array(
      'text' => $this->language->get('heading_title'),
      'href' => $this->url->link('shipping/custom', 'token=' . $this->session->data['token'], 'SSL')
    );

    $data['action'] = $this->url->link('shipping/custom', 'token=' . $this->session->data['token'], 'SSL');

    $data['cancel'] = $this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL');

    if (isset($this->request->post['custom_cost'])) {
      $data['custom_cost'] = $this->request->post['custom_cost'];
    } else {
      $data['custom_cost'] = $this->config->get('custom_cost');
    }

    if (isset($this->request->post['custom_tax_class_id'])) {
      $data['custom_tax_class_id'] = $this->request->post['custom_tax_class_id'];
    } else {
      $data['custom_tax_class_id'] = $this->config->get('custom_tax_class_id');
    }

    if (isset($this->request->post['custom_geo_zone_id'])) {
      $data['custom_geo_zone_id'] = $this->request->post['custom_geo_zone_id'];
    } else {
      $data['custom_geo_zone_id'] = $this->config->get('custom_geo_zone_id');
    }

    if (isset($this->request->post['custom_status'])) {
      $data['custom_status'] = $this->request->post['custom_status'];
    } else {
      $data['custom_status'] = $this->config->get('custom_status');
    }

    if (isset($this->request->post['custom_sort_order'])) {
      $data['custom_sort_order'] = $this->request->post['custom_sort_order'];
    } else {
      $data['custom_sort_order'] = $this->config->get('custom_sort_order');
    }

    $this->load->model('localisation/tax_class');
    $data['tax_classes'] = $this->model_localisation_tax_class->getTaxClasses();

    $this->load->model('localisation/geo_zone');
    $data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();

    $data['header'] = $this->load->controller('common/header');
    $data['column_left'] = $this->load->controller('common/column_left');
    $data['footer'] = $this->load->controller('common/footer');

    $this->response->setOutput($this->load->view('shipping/custom.tpl', $data));
  }

  protected function validate() {
    if (!$this->user->hasPermission('modify', 'shipping/custom')) {
      $this->error['warning'] = $this->language->get('error_permission');
    }

    return !$this->error;
  }
}

这是一个重要的文件,定义了后端配置表单的大部分逻辑。我们将浏览控制器的 index 方法中的重要片段。根据约定,您需要定义类名 ControllerShippingCustom

index 方法中,我们首先加载语言文件并设置页面标题。

接下来,我们加载 setting 模型并将设置保存到数据库中,作为配置表单的 POST 数据。在保存数据之前,我们使用该文件中定义的 validate 方法验证表单。

$this->load->model('setting/setting');

if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
  $this->model_setting_setting->editSetting('custom', $this->request->post);
  $this->session->data['success'] = $this->language->get('text_success');
  $this->response->redirect($this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL'));
}

之后,我们将语言标签分配到 $data 数组中,以便我们可以在视图模板文件中访问这些标签。

接下来,有一个标准片段可以设置正确的面包屑链接。

$data['breadcrumbs'] = array();

$data['breadcrumbs'][] = array(
  'text' => $this->language->get('text_home'),
  'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
);

$data['breadcrumbs'][] = array(
  'text' => $this->language->get('text_shipping'),
  'href' => $this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL')
);

$data['breadcrumbs'][] = array(
  'text' => $this->language->get('heading_title'),
  'href' => $this->url->link('shipping/custom', 'token=' . $this->session->data['token'], 'SSL')
);

接下来,我们设置 action 变量,以确保表单提交到我们的 action 变量,以确保表单提交到我们的 index 方法。同样,如果用户点击 取消 方法。同样,如果用户点击 取消 按钮,就会返回送货方式列表。

$data['action'] = $this->url->link('shipping/custom', 'token=' . $this->session->data['token'], 'SSL');
$data['cancel'] = $this->url->link('extension/shipping', 'token=' . $this->session->data['token'], 'SSL');

此外,还有代码可以在添加或编辑模式下填充配置表单字段的默认值。

if (isset($this->request->post['custom_cost'])) {
  $data['custom_cost'] = $this->request->post['custom_cost'];
} else {
  $data['custom_cost'] = $this->config->get('custom_cost');
}

if (isset($this->request->post['custom_tax_class_id'])) {
  $data['custom_tax_class_id'] = $this->request->post['custom_tax_class_id'];
} else {
  $data['custom_tax_class_id'] = $this->config->get('custom_tax_class_id');
}

if (isset($this->request->post['custom_geo_zone_id'])) {
  $data['custom_geo_zone_id'] = $this->request->post['custom_geo_zone_id'];
} else {
  $data['custom_geo_zone_id'] = $this->config->get('custom_geo_zone_id');
}

if (isset($this->request->post['custom_status'])) {
  $data['custom_status'] = $this->request->post['custom_status'];
} else {
  $data['custom_status'] = $this->config->get('custom_status');
}

if (isset($this->request->post['custom_sort_order'])) {
  $data['custom_sort_order'] = $this->request->post['custom_sort_order'];
} else {
  $data['custom_sort_order'] = $this->config->get('custom_sort_order');
}

在下一部分中,我们从数据库加载税级和地理区域,这些数据将用作配置表单中的下拉选项。

$this->load->model('localisation/tax_class');
$data['tax_classes'] = $this->model_localisation_tax_class->getTaxClasses();

$this->load->model('localisation/geo_zone');
$data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();

最后,我们分配视图的子模板和主模板。

$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');

$this->response->setOutput($this->load->view('shipping/custom.tpl', $data));

创建语言文件

创建文件 admin/language/english/shipping/custom.php 并将以下内容粘贴到该文件中。

<?php
// Heading
$_['heading_title']    = 'Custom Rate';

// Text
$_['text_shipping']    = 'Shipping';
$_['text_success']     = 'Success: You have modified custom rate shipping!';
$_['text_edit']        = 'Edit Custom Rate Shipping';

// Entry
$_['entry_cost']       = 'Cost';
$_['entry_tax_class']  = 'Tax Class';
$_['entry_geo_zone']   = 'Geo Zone';
$_['entry_status']     = 'Status';
$_['entry_sort_order'] = 'Sort Order';

// Error
$_['error_permission'] = 'Warning: You do not have permission to modify custom rate shipping!';

文件的内容应该是不言自明的!

创建视图文件

创建文件 admin/view/template/shipping/custom. 并将以下内容粘贴到该文件中。

<?php echo $header; ?><?php echo $column_left; ?>
<div id="content">
  <div class="page-header">
    <div class="container-fluid">
      <div class="pull-right">
        <button type="submit" form="form-custom" data-toggle="tooltip" title="<?php echo $button_save; ?>" class="btn btn-primary"><i class="fa fa-save"></i></button>
        <a href="<?php echo $cancel; ?>" data-toggle="tooltip" title="<?php echo $button_cancel; ?>" class="btn btn-default"><i class="fa fa-reply"></i></a></div>
      <h1><?php echo $heading_title; ?></h1>
      <ul class="breadcrumb">
        <?php foreach ($breadcrumbs as $breadcrumb) { ?>
        <li><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a></li>
        <?php } ?>
      </ul>
    </div>
  </div>
  <div class="container-fluid">
    <?php if ($error_warning) { ?>
    <div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
      <button type="button" class="close" data-dismiss="alert">&times;</button>
    </div>
    <?php } ?>
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title"><i class="fa fa-pencil"></i> <?php echo $text_edit; ?></h3>
      </div>
      <div class="panel-body">
        <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form-custom" class="form-horizontal">
          <div class="form-group">
            <label class="col-sm-2 control-label" for="input-cost"><?php echo $entry_cost; ?></label>
            <div class="col-sm-10">
              <input type="text" name="custom_cost" value="<?php echo $custom_cost; ?>" placeholder="<?php echo $entry_cost; ?>" id="input-cost" class="form-control" />
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-2 control-label" for="input-tax-class"><?php echo $entry_tax_class; ?></label>
            <div class="col-sm-10">
              <select name="custom_tax_class_id" id="input-tax-class" class="form-control">
                <option value="0"><?php echo $text_none; ?></option>
                <?php foreach ($tax_classes as $tax_class) { ?>
                <?php if ($tax_class['tax_class_id'] == $custom_tax_class_id) { ?>
                <option value="<?php echo $tax_class['tax_class_id']; ?>" selected="selected"><?php echo $tax_class['title']; ?></option>
                <?php } else { ?>
                <option value="<?php echo $tax_class['tax_class_id']; ?>"><?php echo $tax_class['title']; ?></option>
                <?php } ?>
                <?php } ?>
              </select>
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-2 control-label" for="input-geo-zone"><?php echo $entry_geo_zone; ?></label>
            <div class="col-sm-10">
              <select name="custom_geo_zone_id" id="input-geo-zone" class="form-control">
                <option value="0"><?php echo $text_all_zones; ?></option>
                <?php foreach ($geo_zones as $geo_zone) { ?>
                <?php if ($geo_zone['geo_zone_id'] == $custom_geo_zone_id) { ?>
                <option value="<?php echo $geo_zone['geo_zone_id']; ?>" selected="selected"><?php echo $geo_zone['name']; ?></option>
                <?php } else { ?>
                <option value="<?php echo $geo_zone['geo_zone_id']; ?>"><?php echo $geo_zone['name']; ?></option>
                <?php } ?>
                <?php } ?>
              </select>
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-2 control-label" for="input-status"><?php echo $entry_status; ?></label>
            <div class="col-sm-10">
              <select name="custom_status" id="input-status" class="form-control">
                <?php if ($custom_status) { ?>
                <option value="1" selected="selected"><?php echo $text_enabled; ?></option>
                <option value="0"><?php echo $text_disabled; ?></option>
                <?php } else { ?>
                <option value="1"><?php echo $text_enabled; ?></option>
                <option value="0" selected="selected"><?php echo $text_disabled; ?></option>
                <?php } ?>
              </select>
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-2 control-label" for="input-sort-order"><?php echo $entry_sort_order; ?></label>
            <div class="col-sm-10">
              <input type="text" name="custom_sort_order" value="<?php echo $custom_sort_order; ?>" placeholder="<?php echo $entry_sort_order; ?>" id="input-sort-order" class="form-control" />
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>
<?php echo $footer; ?>

同样,这应该很容易理解。此模板文件的目的是为我们的自定义运输方法提供配置表单。它使用我们之前在控制器文件中设置的变量。

因此,就我们的自定义运输方法而言,后端文件设置就是这样。在下一节中,我们将了解如何启用自定义运输方式以及自定义配置表单的外观!

启用自定义运送方式

前往管理部分,然后转至 扩展 > 运送。您应该会看到我们的自定义送货方式被列为自定义费率。点击+符号安装我们的自定义送货方式。安装后,您应该能够看到编辑链接来打开配置表单。点击编辑链接,表单应如以下屏幕截图所示。

OpenCart教程:自定义配送方式(第一部分)

上述表单中的重要字段是税级地理区域强>。

如果您除了成本字段中定义的金额之外还需要征收任何其他税款,则可以通过税级字段选择适当的选项。我们现在选择应税商品

通过地理区域字段,您可以选择此方法适用的区域;为了简单起见,选择所有区域。另外,请确保将状态设置为已启用,否则不会在前端结账中列出。

填写完必要的数据后,点击保存按钮就可以了。今天的文章就到此为止,我将很快在下一部分中给您回复,其中将解释前端文件设置。

结论

今天,我们开始了一系列关于如何在 OpenCart 中创建自定义送货方式的系列。在第一部分中,我们浏览了后端部分并探讨了如何设置配置表单。如果有任何疑问和建议,请留言!

以上是OpenCart教程:自定义配送方式(第一部分)的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
HTML的角色:构建Web内容HTML的角色:构建Web内容Apr 11, 2025 am 12:12 AM

HTML的作用是通过标签和属性定义网页的结构和内容。1.HTML通过到、等标签组织内容,使其易于阅读和理解。2.使用语义化标签如、等增强可访问性和SEO。3.优化HTML代码可以提高网页加载速度和用户体验。

HTML和代码:仔细观察术语HTML和代码:仔细观察术语Apr 10, 2025 am 09:28 AM

htmlisaspecifictypefodyfocusedonstructuringwebcontent,而“代码” badlyLyCludEslanguagesLikeLikejavascriptandPytyPythonForFunctionality.1)htmldefineswebpagertuctureduseTags.2)“代码”代码“ code” code code code codeSpassSesseseseseseseseAwiderRangeLangeLangeforLageforLogageforLogicIctInterract

HTML,CSS和JavaScript:Web开发人员的基本工具HTML,CSS和JavaScript:Web开发人员的基本工具Apr 09, 2025 am 12:12 AM

HTML、CSS和JavaScript是Web开发的三大支柱。1.HTML定义网页结构,使用标签如、等。2.CSS控制网页样式,使用选择器和属性如color、font-size等。3.JavaScript实现动态效果和交互,通过事件监听和DOM操作。

HTML,CSS和JavaScript的角色:核心职责HTML,CSS和JavaScript的角色:核心职责Apr 08, 2025 pm 07:05 PM

HTML定义网页结构,CSS负责样式和布局,JavaScript赋予动态交互。三者在网页开发中各司其职,共同构建丰富多彩的网站。

HTML容易为初学者学习吗?HTML容易为初学者学习吗?Apr 07, 2025 am 12:11 AM

HTML适合初学者学习,因为它简单易学且能快速看到成果。1)HTML的学习曲线平缓,易于上手。2)只需掌握基本标签即可开始创建网页。3)灵活性高,可与CSS和JavaScript结合使用。4)丰富的学习资源和现代工具支持学习过程。

HTML中起始标签的示例是什么?HTML中起始标签的示例是什么?Apr 06, 2025 am 12:04 AM

AnexampleOfAstartingTaginHtmlis,beginSaparagraph.startingTagSareEssentialInhtmlastheyInitiateEllements,defiteTheeTheErtypes,andarecrucialforsstructuringwebpages wepages webpages andConstructingthedom。

如何利用CSS的Flexbox布局实现菜单中虚线分割效果的居中对齐?如何利用CSS的Flexbox布局实现菜单中虚线分割效果的居中对齐?Apr 05, 2025 pm 01:24 PM

如何设计菜单中的虚线分割效果?在设计菜单时,菜名和价格的左右对齐通常不难实现,但中间的虚线或点如何...

在线代码编辑器究竟用什么HTML元素实现代码输入?在线代码编辑器究竟用什么HTML元素实现代码输入?Apr 05, 2025 pm 01:21 PM

网页代码编辑器中的HTML元素分析许多在线代码编辑器允许用户输入HTML、CSS和JavaScript代码。最近,有人提出了一...

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具