在OpenCart中创建自定义“订单总计”扩展

在本文中,我们将在OpenCart中创建自定义订单总计扩展。 订单总数扩展允许您在结帐期间操纵订单金额。 这是通过增加税收或通过不同方法提供折扣来更改价格的一种非常有用的方法。 我们将创建一个完整的自定义订单总额扩展,它将应用由后端配置表单定义的自定义税。

要创建自定义订单总计扩展,我们需要在后端和前端设置文件。 后端文件用于设置配置表单,前端文件用于定义扩展的逻辑。 当然,在没有前端文件的情况下,OpenCart不会在结帐时检测到您的扩展名。

我们将使用最新版本的OpenCart。 另外,我假设您熟悉OpenCart中的基本模块开发过程。 如果您不熟悉它,这是一篇很好的文章,介绍了自定义模块的开发。

让我们继续并立即开始!

后端文件设置

在本部分中,我们将创建与后端部分相关的文件。 在本节的最后,您将能够在列表中看到我们的自定义订单总计扩展以及其他订单总计扩展。 另外,您将能够使用自定义配置表单来安装和配置它。

继续并创建具有以下内容的控制器文件admin/controller/total/customot.php

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

  public function index() {
    $this->load->language('total/customot');

    $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('customot', $this->request->post);

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

      $this->response->redirect($this->url->link('extension/total', '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_none'] = $this->language->get('text_none');

    $data['entry_customtax'] = $this->language->get('entry_customtax');
    $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_total'),
      'href' => $this->url->link('extension/total', 'token=' . $this->session->data['token'], 'SSL')
    );

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

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

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

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

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

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

    $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('total/customot.tpl', $data));
  }

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

    return !$this->error;
  }
}

如您所见,这是OpenCart中设置的相当标准的后端控制器。 该控制器文件的主要目的是设置标签和其他元素,这些标签和其他元素将用于显示配置表单。 当然,它通过验证表单并将值保存到数据库来处理表单提交。

现在,让我们继续在admin/language/english/total/customot.php具有以下内容的语言文件。

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

// Text
$_['text_success']     = 'Success: You have modified custom order total!';
$_['text_edit']        = 'Edit Custom Order Total';

// Entry
$_['entry_customtax']  = 'Custom Tax (%)';
$_['entry_status']     = 'Status';
$_['entry_sort_order'] = 'Sort Order';

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

同样,我们在定义标签时应该很容易理解。

最后,我们将在admin/view/template/total/customot.tpl创建一个视图模板文件。

<?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-customot" 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-customot" class="form-horizontal">
          <div class="form-group">
            <label class="col-sm-2 control-label" for="input-customtax"><?php echo $entry_customtax; ?></label>
            <div class="col-sm-10">
              <input type="text" name="customot_customtax" value="<?php echo $customot_customtax; ?>" placeholder="<?php echo $entry_customtax; ?>" id="input-customtax" class="form-control" />
            </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="customot_status" id="input-status" class="form-control">
                <?php if ($customot_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="customot_sort_order" value="<?php echo $customot_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; ?>

该文件包含我们配置表单的XHTML代码。

就我们的后端设置而言就是这样。 转到扩展>订单总计 。 您应该能够看到我们的扩展名“ 自定义订单总计”以及其他扩展名。 让我们按照以下屏幕截图所示安装和配置它。

后端表格

我已将“ 自定义税”值设置为5 ,因此它将收取总订单金额的5%。 填写值并保存表格。

前端文件设置

在本节中,我们将为前端定义文件,以便在前端结帐流程中检测到我们的扩展名。

使用以下内容创建模型文件catalog/model/total/customot.php

<?php
class ModelTotalCustomot extends Model {
  public function getTotal(&$total_data, &$total, &$taxes) {
    $this->load->language('total/customot');
    
    // get customtax
    $customtax_percentage = $this->config->get('customot_customtax');
    
    if ($customtax_percentage > 0) {
      $customtax_value = round(($total * $customtax_percentage) / 100);
    

      $total_data[] = array(
        'code'       => 'customot',
        'title'      => $this->language->get('text_customot'),
        'value'      => $customtax_value,
        'sort_order' => $this->config->get('customot_sort_order')
      );

      $total += $customtax_value;
    }
  }
}

这是一个重要的文件,我们扩展的核心逻辑在这里。 按照惯例,OpenCart在结帐期间针对每个订单总计扩展调用getTotal方法。 您应该注意到重要的参数$total_data$total$taxes

$total_data变量表示所有订单总计扩展数据的数组。 $total变量是订单总金额, $taxes包含应用的税额。

在这种方法中,我们要获取一个自定义税值,该值是从后端配置表单中设置的。 此外,我们计算自定义税额并将其分配给$customtax_value变量。 接下来,我们将订单总计扩展信息插入$total_data数组,并将自定义税额添加到$total变量。

最后,我们需要在catalog/language/english/total/customot.php中定义以下内容的语言文件。

<?php
$_['text_customot'] = 'Custom Tax Value';

前端就是这样!

前端演示

继续,然后将产品添加到购物车,您将能够看到我们应用的自定义税项,如以下屏幕截图所示。

演示版

因此,您可以使用订单总数扩展来影响订单金额。

结论

在本文中,我们学习了如何创建自定义订单总额扩展并操纵订单金额。 这是附加任何自定义税项和折扣的一种非常有用的方法。

如果您正在寻找其他OpenCart工具,实用程序,扩展等,以便可以在自己的项目中使用或用于自己的教育,请不要忘记查看我们在市场上提供的产品

总是欢迎提出建议和疑问!

翻译自: https://code.tutsplus.com/tutorials/create-a-custom-order-total-extension-in-opencart--cms-25341

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值