快速提示:在OpenCart 2.0中接受多个凭证

OpenCart是基于PHP的开源在线电子商务解决方案,可让您在线接受订单。 它允许客户使用代金券购物,但默认情况下一次只能使用一张。 如果要允许客户一次使用多个凭单,则需要修改代码。 在本教程中,我将确切地向您展示如何做到这一点。

我们如何创建礼品券?

只有管​​理员和允许的后端用户才能生成礼品券。 OpenCart带有用于执行此操作的默认模块。 以下屏幕截图显示了后端用户可以生成凭证的地方。

仪表板

问题是什么?

如果默认情况下我们在OpenCart中实现了凭证系统,那是什么问题? 让我们借助示例了解它:

最近,我的一个朋友想通过OpenCart购买几件商品,价值800美元,但她只有500美元,还有两张价值250美元的礼券。 于是她打电话给我,要求解决。 由于OpenCart仅允许您一次使用一张优惠券购物,因此她无法购买商品。

一个办法

要解决此问题,我们需要添加一个使用多个凭单的选项,以便客户可以轻松购物而不会遇到任何困难。 这是解决方案,其中我们将修改几行代码,这将有助于解决此问题。

本教程分为三个主要步骤,分别是:

  1. 控制器变更
  2. 型号变更
  3. 改变视角

1.控制器文件

  1. 导航到catalog/controller/checkout/cart.php
  2. 查找以下代码:
If(isset($this->request->post['voucher']) && $this-> valid voucher())
{
$this->session->data['voucher']=$this->request->post['voucher'];
}

将上面的代码更改为:

If(isset($this->request->post['voucher']) && $this-> valid voucher())
{
$this->session->data['voucher'][$this->request->post['voucher']]=$this->request->post['voucher']; // creates an array for multiple vouchers
}

2.模型文件

  1. 导航到catalog/model/total/voucher.php
  2. 查找以下代码行:
$this->load->model('checkout/voucher');
          
$voucher_info = $this->model_checkout_voucher->getVoucher($this->session->data['voucher']);
         
         if ($voucher_info) {
            if ($voucher_info['amount'] > $total) {
               $amount = $total;   
            } else {
               $amount = $voucher_info['amount'];   
            }            
               
            $total_data[] = array(
               'code'       => 'voucher',
                 'title'      => sprintf($this->language->get('text_voucher'), $this->session-      >data['voucher']),
                'text'       => $this->currency->format(-$amount),
                 'value'      => -$amount,
               'sort_order' => $this->config->get('voucher_sort_order')
               );

            $total -= $amount;
    }

我们需要运行一个外部循环以获取在步骤1中作为数组维护的所有凭证。因此,我们将在$this->load->model('checkout/voucher');之后放置一个外部循环$this->load->model('checkout/voucher');

因此,我们将循环使用“会话凭证阵列”以获取我们应用的所有凭证,代码如下所示。 请注意,对以下代码进行了注释,以使其更易于理解。

foreach (array_unique($this->session->data['voucher']) as $voucher)
 { // foreach loop will select each array and extract the unique voucher

     $voucher_info = $this->model_checkout_voucher->getVoucher($voucher); // fetch the order details
     
     // Check 1: If Voucher Exists  
     if ($voucher_info) {
     // Check 2: If the voucher amount is greater than our order amount, voucher balance will be maintained
     if ($voucher_info['amount'] > $total) {
     $amount = $total;   
     } else {
     $amount = $voucher_info['amount'];   
     }
    // End Check 2
  
  // Array to return Updated Totals
     $total_data[] = array( 'code'       => 'voucher', 'title'      => sprintf($this->language->  get('text_voucher'), $voucher),
     'text'       => $this->currency->format(-$amount),
     'value'      => -$amount,
     'sort_order' => $this->config->get('voucher_sort_order')
     );
  // End Array
  
    $total -= $amount; // Substracts the amount with our order totals
     
     } // End Check 1
     
 } // End Foreach Loop

3.查看文件

  1. 导航到catalog/view/theme/default/template/checkout/voucher.tpl
  2. 查找以下代码行:
<input type="text" name="voucher" value="<?php echo $voucher; ?>" placeholder="<?php echo $entry_voucher; ?>" id="input-voucher" class="form-control" />

替换为:

<input type="text" name="voucher" value="" placeholder="<?php echo $entry_voucher; ?>" id="input-voucher" class="form-control" />

我们已经解决了问题! 实际上,我们只是做了一些简单的代码黑客来解决这个大问题,因此我们不必开发新的模块或扩展。 我们只是修改了几行代码来完成它。

结论

在本文中,我们提供了一个成功的解决方案,可以在我们的电子商店中添加多个凭单。 由于OpenCart默认情况下不允许购物者使用多个凭单,因此我们修改了代码,以便现在他们可以根据需要使用任意数量的凭单。 这将帮助客户轻松购物,没有任何问题。

在我们的下一篇文章中,我们将在OpenCart系统中实现一些实际的业务工具,因此请继续订阅并发表您的宝贵意见。 谢谢!

翻译自: https://code.tutsplus.com/articles/quick-tip-accept-multiple-vouchers-in-opencart-20--cms-24405

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值