Add 'plus' 'minus' in place of 'add to cart' in opencart

前端 未结 2 1678
离开以前
离开以前 2021-01-16 18:36

I want to replace add to cart with 2 buttons that are plus and minus in OpenCart 2.0.1.1, Now i am unable to code for minus button properly. I have added plus and mius butto

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-16 19:12

    In order to decrement your product quantity you need product_id and its quantity. To decrement we need to check whether qty is greater than 1 or not if it is then we can decrement else we need to remove entire product if qty is only 1.

    Things you need to change is your view page add plus , minus icons there, then controller then library then send ajax result back. I will try to make this as easier as possible.

    Lets start with view page in my case it is products.tpl the code i writteb to get plus minus buttons is

      

    Here I have made javascript ajax call onclick. So lets see what that call does. I have written in same page you can write in any .js file if you wish. script.js

    'decrement': function(key) {
    $.ajax({
    url: 'index.php?route=checkout/cart/decrement',
    type: 'post',
    data: 'key=' + key,
    dataType: 'json',
    beforeSend: function() {
    $('#cart > button').button('loading');
    },
    complete: function() {
    $('#cart > button').button('reset');
    },
    success: function(json) {
    // Need to set timeout otherwise it wont update the total
    setTimeout(function () {
    $('#cart > button').html(' ' + json['total'] + '');
    }, 100);
    
    if (getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') {
    location = 'index.php?route=checkout/cart';
    } else {
    $('#cart > ul').load('index.php?route=common/cart/info ul li');
    }
    }
    });
    }
    

    Now the url we calling from above ajax call is the path for our controller checkout and function decrement. Here is that

    controller.php

        public function decrement() {
        $this->load->language('checkout/cart');
    
        $json = array();
    
        // Remove
        if (isset($this->request->post['key'])) {
        $this->cart->decrement_product_quantity($this->request->post['key'],1);
    
        unset($this->session->data['vouchers'][$this->request->post['key']]);
    
        $this->session->data['success'] = $this->language->get('text_remove');
      // rest of the code keep same}
    

    Now did you noticed we are calling library function decrement_product_quantity by passing qty and 1. Here key is nothing but ajax parameter which is product_id.

    Now final function in library

    public function  decrement_product_quantity($product_id, $qty = 1){
    $this->data = array();
    $product['product_id'] = (int)$product_id;
    $key = base64_encode(serialize($product));
    
    if ((int)$qty && ((int)$qty > 0)) {
    if ($this->session->data['cart'][$key]>1) {
    $this->session->data['cart'][$key] -= (int)$qty;
    } else {
    $this->remove($key);
    }
    }
    }
    

    This one checks cart if qty is greater than 1 it will decrement else remove the entire product. Hope you understood please let me know if any queries you have. Also hope you can do for increment too. good luck

    提交回复
    热议问题