Heim  >  Fragen und Antworten  >  Hauptteil

Wie erhalte ich die Produkt-ID per Ean über Ajax und füge sie in den Warenkorb? Warenkorb öffnen 3

Ich habe versucht, das Produkt durch Scannen des Barcodes in den Warenkorb zu legen. In common.js habe ich

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

Funktionalität in controller/checkout/cart.php hinzufügen:

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

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

Aber ich erhalte idbyean als undefiniert. Was habe ich falsch gemacht?

P粉176980522P粉176980522175 Tage vor385

Antworte allen(1)Ich werde antworten

  • P粉968008175

    P粉9680081752024-04-01 13:04:54

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

    Antwort
    0
  • StornierenAntwort