首页 > 解决方案 > 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”时添加费用。

标签: phpopencart

解决方案


您不能通过单击复选框直接将数据添加到模型文件。首先,您需要像这样调用此数据:在将放置复选框的模板文件中,将复选框添加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']. 这里不是完整的东西,只是你需要做的事情。


推荐阅读