在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变量以确保将表单提交给我们的index方法。 同样,如果用户单击“ Cancel按钮,则会将他们带回到运输方式列表中。

$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并将以下内容粘贴到该文件中。

<?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; ?>

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

因此,就我们的自定义传送方式而言,就涉及到后端文件设置。 在下一节中,我们将看到如何启用自定义发送方式以及自定义配置表单的外观!

启用自定义送货方式

转到管理部分,然后转到扩展>运送 。 您应该看到我们的自定义送货方式列为“ 自定义费率” 。 单击+号以安装我们的自定义送货方式。 安装后,您应该可以看到“ 编辑”链接以打开配置表单。 单击“ 编辑”链接,表格应如下图所示。

配置表

上表中的重要字段是“ 税种”和“ 地域”

如果您需要除“ 成本”字段中定义的金额之外还强加任何其他税款,则“ 税种”字段允许您选择适当的选项。 让我们现在选择应税商品

地理区域”字段允许您选择此方法适用的区域; 为简单起见,选择“ 所有区域” 。 另外,请确保将状态设置为Enabled ,否则它不会在前端结帐中列出。

填写必要的数据后,请单击“ 保存”按钮,这应该没问题。 今天的文章就是这样,下一部分我将尽快与您联系,这将解释前端文件的设置。

结论

今天,我们开始了有关如何在OpenCart中创建自定义送货方式的系列文章。 在第一部分中,我们浏览了后端部分,并探讨了如何设置配置表单。 发表您的查询和建议(如果有)!

翻译自: https://code.tutsplus.com/tutorials/create-a-custom-shipping-method-in-opencart-part-one--cms-25076

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值