php - Opencart:如何根据复选框状态返回值?
问题描述
如果在 opencart 结帐页面中选中该复选框,则希望向 Total 添加一些费用
以下是我要更改的代码。如果选择付款方式“COD”,此代码会添加费用。
<?php
class ModelExtensionTotalCashonDeliveryFee extends Model {
public function getTotal($total) {
if ($this->config->get('cashon_delivery_fee_status') && isset($this->session->data['payment_method']) && $this->session->data['payment_method']['code'] == 'cod') {
$this->load->language('extension/total/cashon_delivery_fee');
$fee_amount = 0;
$sub_total = $this->cart->getSubTotal();
if($this->config->get('cashon_delivery_fee_type') == 'P') {
$fee_amount = round((($sub_total * $this->config->get('cashon_delivery_fee_fee')) / 100), 2);
} else {
$fee_amount = $this->config->get('cashon_delivery_fee_fee');
}
$tax_rates = $this->tax->getRates($fee_amount, $this->config->get('cashon_delivery_fee_tax_class_id'));
foreach ($tax_rates as $tax_rate) {
if (!isset($taxes[$tax_rate['tax_rate_id']])) {
$taxes[$tax_rate['tax_rate_id']] = $tax_rate['amount'];
} else {
$taxes[$tax_rate['tax_rate_id']] += $tax_rate['amount'];
}
}
$total['totals'][] = array(
'code' => 'cashon_delivery_fee',
'title' => $this->language->get('text_cashon_delivery_fee'),
'value' => $fee_amount,
'sort_order' => $this->config->get('cashon_delivery_fee_sort_order')
);
$total['total'] += $fee_amount;
}
}
}
我希望它在 .tpl 中选中输入复选框时添加费用,<input type="checkbox" name="checkbox">COD Charges
而不是在选择付款方式“cod”时添加费用。
解决方案
您不能通过单击复选框直接将数据添加到模型文件。首先,您需要像这样调用此数据:在将放置复选框的模板文件中,将复选框添加id="{{ order_id }}
到此模板文件或其他 javascript 文件中添加此脚本:
var checkbox = document.getElementById(product_id);
if (checkbox.checked == true) {
addFee(product_id);
//another stuff if you need
}
接下来,您需要在相应的控制器文件中创建函数,例如add_fee
,您可以根据发布的内容从 DB 调用数据,product_id
并将这些数据包含到当前会话中,如下所示$this->session->data['fee'] = $some_fee_from_DB
。在相应的模板文件或附加的 javascript 文件中添加 ajax 函数,例如addFee
:
function addFee(product_id) {
$.ajax({
type: 'post',
url: 'index.php?route=link_to_your_function/add_fee',
data: 'product_id=' + product_id,
dataType: 'json',
success: function(json) {
$('#fee').html(json['fee']); // and all other data which will be returned.
if (json['success']) {
//some stuff on success what you need to be displayed or filled up to the corresponding fields.
}
}
});
}
然后,您将能够使用$this->session->data['fee']
. 这里不是完整的东西,只是你需要做的事情。
推荐阅读
- bouncycastle - Bouncy Castle 1.60 中缺少 X509v3CertificateBuilder 和 JcaX509v3CertificateBuilder
- android - 如果我最小化我的应用程序或转动屏幕(谷歌地图),折线将无法正常工作
- ios - 从配对的 iPhone 确定 Apple Watch 电池电量
- awk - 对可变长度的循环应用 awk 过滤器
- java - 在 Spring Boot 中通过 Spring Security 中的角色授予或限制路由的权限
- azure-web-app-service - 从 azure-cli 向 WebApp 或 FunctionApp 添加 AppInsights 支持
- mysql - MySQL连接性能不使用索引
- karate - 如何在场景中的步骤之间添加静态等待?
- php - 当并行用户输入文本字段值更改下拉值时,应自动更改
- android - Kotlin 等效于检查是否为空