Asif Shaikh
Asif Shaikh

Reputation: 11

Opencart: How to return the value based on checkbox state?

Want to add some fees to the Total if the checkbox is checked in opencart checkout page

Following is the code that I want to change. This code adds the fees if the Payment method "COD" is selected.

<?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;
        }
    }
}

I want it to add fees when input checkbox is checked <input type="checkbox" name="checkbox">COD Charges in .tpl and not when the Payment method "cod" is selected.

Upvotes: 0

Views: 230

Answers (1)

K. B.
K. B.

Reputation: 1430

you can not directly add data to model file by clicking checkbox. First you need invoke this data something like this: In template file where checkbox will be placed to checkbox tag add id="{{ order_id }} Than in to this template file or additional javascript file add this script:

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

Next you need in your corresponding controller file create function for example add_fee where you can invoke data from DB according posted product_id and also include this data to the current session like this $this->session->data['fee'] = $some_fee_from_DB. In corresponding template file or additional javascript file add ajax function for example 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.

                }

        }
    });
}

Than you will be able retrieve data in your mentioned model file, using $this->session->data['fee']. Here is not complete stuff, just only the way what you need to do.

Upvotes: 0

Related Questions