【问题标题】:Opencart: How to return the value based on checkbox state?Opencart:如何根据复选框状态返回值?
【发布时间】:2019-09-15 02:37:39
【问题描述】:

如果在 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 中选中输入复选框 &lt;input type="checkbox" name="checkbox"&gt;COD Charges 时添加费用 而不是在选择付款方式“cod”时。

【问题讨论】:

    标签: php opencart


    【解决方案1】:

    您不能通过单击复选框直接将数据添加到模型文件。首先,您需要像这样调用此数据: 在复选框将被放置到复选框标签的模板文件中添加id="{{ order_id }} 比在此模板文件或其他 javascript 文件中添加此脚本:

    var checkbox = document.getElementById(product_id);
            if (checkbox.checked == true) {
                addFee(product_id);
        //another stuff if you need     
    }
    

    接下来,您需要在相应的控制器文件中创建函数,例如 add_fee,您可以在其中根据发布的 product_id 从 DB 调用数据,并将此数据包含到当前会话中,例如 $this-&gt;session-&gt;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-&gt;session-&gt;data['fee'] 在您提到的模型文件中检索数据。这里不是完整的东西,只是你需要做的事情。

    【讨论】:

    • 实际上我在管理面板中使用了整个模块。费用是在那里预定义的。选择 COD 选项时,它可以完美运行。我只想通过条件` if ($this->config->get('cashon_delivery_fee_status') && isset($this->session->data['payment_method']) && $this->session->data[ '付款_method'] ['code'] =='cod'){`单击复选框时为true。 span>
    猜你喜欢
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 2012-06-28
    • 1970-01-01
    • 2021-10-27
    • 2014-11-24
    相关资源
    最近更新 更多