2

I want to add products into the cart by SKU or MODEL instead of Product id. here is the code of how it adds in the category.twig file

<button type="button" onclick="cart.add('{{ product.product_id }}', '{{ product.minimum }}');"><i class="fa fa-shopping-cart"></i> <span class="hidden-xs hidden-sm hidden-md">{{ button_cart }}</span></button>

And here is js

// Cart add remove functions
var cart = {
    'add': function(product_id, quantity) {
        $.ajax({
            url: 'index.php?route=checkout/cart/add',
            type: 'post',
            data: 'product_id=' + product_id + '&quantity=' + (typeof(quantity) != 'undefined' ? quantity : 1),
            dataType: 'json',
            beforeSend: function() {
                $('#cart > button').button('loading');
            },
            complete: function() {
                $('#cart > button').button('reset');
            },
            success: function(json) {
                $('.alert-dismissible, .text-danger').remove();

                if (json['redirect']) {
                    location = json['redirect'];
                }

                if (json['success']) {
                    $('#content').parent().before('<div class="alert alert-success alert-dismissible"><i class="fa fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');

                    // Need to set timeout otherwise it wont update the total
                    setTimeout(function () {
                        $('#cart > button').html('<span id="cart-total"><i class="fa fa-shopping-cart"></i> ' + json['total'] + '</span>');
                    }, 100);

                    $('html, body').animate({ scrollTop: 0 }, 'slow');

                    $('#cart > ul').load('index.php?route=common/cart/info ul li');
                }
            },
            error: function(xhr, ajaxOptions, thrownError) {
                alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
        });
    },
}

in above code opencart uses product_id to add product into cart but instead i want to add it by sku or model

Ziauz
  • 773
  • 4
  • 22
  • you can add model by changeing this product_id to model BUT you will get issues after that – Ivan Jan 31 '20 at 07:31
  • 1
    @Ivan yes I can change but it won't add product to cart – Ziauz Jan 31 '20 at 08:08
  • yes here is come the issue becasue in index.php?route=checkout/cart/add you are not sending anymore product_id so u need to modificate everthing there to work with product model, also in system\library\cart\cart.php and many other places to make the whole system to work with product model instead of product_id, BTW why you want to make it to work with models ? – Ivan Jan 31 '20 at 09:53
  • @Ivan yeah I modified all the files but getting errors, I will update the question with modified code, I have a client requirement where he wants to add products by barcode reader with SKU or model – Ziauz Jan 31 '20 at 10:08
  • show the error and what else you modificated – Ivan Jan 31 '20 at 10:36
  • @Ivan I can't update questions the code limit exceeds, I Changed ``public function getProduct($model) {`` in model/catalog/product.php and controller/checkout/cart.php – Ziauz Jan 31 '20 at 10:42
  • than post the errors – Ivan Jan 31 '20 at 12:30
  • I have cleared all errors those are simply my mistakes, but product is not adding to cart. – Ziauz Jan 31 '20 at 13:04
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206968/discussion-between-ziauz-and-ivan). – Ziauz Jan 31 '20 at 13:04
  • 1
    @Ivan Can you please share the answer so that it can be helpful for others like me. – asimdev Jan 31 '20 at 13:16

1 Answers1

2

In common.js new method derived from add (basically product_id changed to sku and addBySku is called)

    'addBySku': function(sku, quantity) {
        $.ajax({
            url: 'index.php?route=checkout/cart/addBySku',
            type: 'post',
            data: 'sku=' + sku + '&quantity=' + (typeof(quantity) != 'undefined' ? quantity : 1),
            //nothing else changed from 'add' method

In catalog/controller/checkout/cart.php derived from add method. Just get product_id by sku, put it into post and keep everything else unchanged.

    public function addBySku() {
        $this->load->language('checkout/cart');
        $this->load->model('catalog/product');

        $json = array();

        if (isset($this->request->post['sku'])) {
            $product_id = (int)$this->model_catalog_product->productIDBySku($this->request->post['sku']);   
        } else {
            $product_id = 0;
        }
        $this->request->post['product_id'] = $product_id;


        //nothing else changed from 'add' method


In catalog/model/catalog/product.php. Add this method which retrieves sku by product_id

    public function productIDBySku($sku) {
        $query = $this->db->query("select product_id from " . DB_PREFIX . "product where sku = '" . $this->db->escape($sku) . "'");
        return $query->row['product_id'];
    }

Hope that helps. I didn't test it though. Don't forget to call correct method and pass sku

<!--<button type="button" onclick="cart.add('{{ product.product_id }}',-->
<button type="button" onclick="cart.addBySku('{{ product.sku }}',
Hebe
  • 661
  • 1
  • 7
  • 13
  • Modified: retrieve product_id in controller and proceed as is. – Hebe Jan 31 '20 at 15:01
  • Fatal error: Call to a member function productIDBySku() on null in D:\wamp\www\opencart\catalog\controller\checkout\cart.php on line 268 – Ziauz Jan 31 '20 at 15:12
  • Modified: load catalog/product before calling its function – Hebe Jan 31 '20 at 15:18
  • great..... its working... thanks for your kind help... but when there is no sku value even at that point it ads a random product. how can we avoid that – Ziauz Jan 31 '20 at 15:33
  • I didn't get it. if no such sku it's now tries to add product with product_id 0. – Hebe Jan 31 '20 at 15:42