How to get product_id via ean via ajax and add to cart? Open cart 3
P粉176980522
P粉176980522 2024-03-31 16:16:30
0
1
382

I try to add a product to the cart by scanning the barcode. In common.js I added

$('body').on('keydown', '#form-for-saler-add-to-cart', function(e) {
            if (e.keyCode == 13) {
                
                    function getIdByEan(ean) {
                        $.ajax({
                            type: "POST",
                            url: 'index.php?route=checkout/cart/getIdByEan',
                            data: ean,
                            success: function(data) {
                                // Run the code here that needs
                                //    to access the data returned
                                return data;
                            }
                        });
                    }
                idbyean = getIdByEan($(this).val());
                
               console.log($(this).val());
               console.log(idbyean);
               cart.add(idbyean);
            //   cart.add($(this).val());
                $(this).val('');
                $(this).html('');
            }
        });

Add function in controller/checkout/cart.php:

public function getIdByEan() {
    $this->load->model('catalog/product');


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

and model/catalog/product.php

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

But I get idbyean as undefined. What did i do wrong?

P粉176980522
P粉176980522

reply all(1)
P粉968008175

I have solved it. In common.js

$('body').on('keydown', '#form-for-saler-add-to-cart', function(e) {
        if (e.keyCode == 13) {
            getIdByEan($(this).val());
            $(this).select();
        }
    });
function getIdByEan (ean) {
        console.log(ean);
        $.ajax({
            type: 'post',
            url: $('base').attr('href')+'index.php?route=checkout/cart/getIdByEan',
            data: {'ean': ean},
            success: function(response) {
                if (response == 0 || !response) {alert('Товар '+ean+' НЕ НАЙДЕН!')}
                console.log(response)
                cart.add(response);
            },
        });
}

In controller/checkout/cart.php

public function getIdByEan() {
    $this->load->model('catalog/product');


    if (isset($this->request->post['ean'])) {
        $product_id = (int)$this->model_catalog_product->productIDByEan($this->request->post['ean']);   
    } else {
        $product_id = 0;
    }
      $this->response->setOutput($product_id);
    
    
}

In model/catalog/product.php

public function productIDByEan($ean) {
    $query = $this->db->query("select product_id from " . DB_PREFIX . "product where ean = '" . $this->db->escape($ean) . "'");
    return $query->row['product_id'];
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!