搜尋
首頁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 變量,以確保表單提交到我們的 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尊渡假赌尊渡假赌尊渡假赌

熱工具

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具