如何通过ajax通过ean获取product_id并添加到购物车?开放式购物车 3
P粉176980522
P粉176980522 2024-03-31 16:16:30
0
1
440

我尝试通过扫描条形码将产品添加到购物车。 在 common.js 中我添加了

$('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('');
            }
        });

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

以及 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'];
    }

但我得到 idbyean 为未定义。我做错了什么?

P粉176980522
P粉176980522

全部回复(1)
P粉968008175

我已经解决了。在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);
            },
        });
}

在控制器/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);
    
    
}

在 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'];
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!