Domanda

Nel carrello predefinito di Magento, quando il cliente modifica la quantità, ha bisogno di premere il pulsante per aggiornare la quantità.

C'è un modo in cui il carrello aggiorna automaticamente la quantità quando l'utente inserisce un altro numero nel campo della quantità?

È stato utile?

Soluzione

Prima modifica il modello di carrello /app/design/frontend/{package}/{theme}/template/checkout/cart.phtml e aggiungere un ID sull'elemento del modulo per un accesso più facile.Diciamo che aggiungi 'id="cart-form"';

Ora modifica i modelli che rendono gli elementi del carrello:

    .
  • app / design / frontend / {package} / {tema} /template/checkout/cart/itm/default.phtml
  • app / design / frontend / {package} / {tema} /template/downloadable/checkout/cart/item/default.phtml

    E sull'elemento <input> con il nome cart[<?php echo $_item->getId() ?>][qty] Aggiungi questo:

    onchange="$('cart-form').submit()"
    
    .

    Ma non lo consiglio di farlo.È davvero fastidioso per gli utenti.(almeno per me).

Altri suggerimenti

Supponendo che il tuo sito abbia jquery incluso nella modalità senza conflitti, ecco un modo per fare questo asincrono (molto meno fastidioso!).

jQuery(document).ready(function(){
jQuery('#shopping-cart-table')
    .on(
        'change',
        'input[name$="[qty]"]',
        function(){
            var form = jQuery(jQuery(this).closest('form'));

            // we'll extract the action and method attributes out of the form

            // kick off an ajax request using the form's action and method,
            // with the form data as payload
            jQuery.ajax({
                url: form.attr('action'),
                method: form.attr('method'),
                data: form.serializeArray()
            });
        }
    );
});
.

Dovrei sottolineare che questo rende le seguenti assunzioni:

    .
  • Il tuo carrello della spesa vive all'interno di un elemento con l'ID di Shopping-cart-table
  • I campi di input per Quantità Attributo Attributo che termina con [Qtà]

    Dovrebbe essere facile regolare i selettori nel codice sulle righe 2 e 5 rispettivamente per abbinare le circostanze.

Modifica questi due file

app/design/frontend/{package}/{theme}/template/checkout/cart/item/default.phtml

app/design/frontend/{package}/{theme}/template/downloadable/checkout/cart/item/default.phtml
.

E sull'elemento con il nome cart[<?php echo $_item->getId() ?>][qty] Aggiungi questo:

onchange="this.form.submit()"
.

Se la tua versione jQuery è vecchia, non ci riuscirai.Ho trovato un modo che è il seguente, segue come le istruzioni del nostro amico Marius per inserire

/app/design/frontend/{package}/{theme}/template/checkout/cart.phtml e aggiungi un ID sull'elemento modulo per un accesso più facile.Diciamo che aggiungi id="cart-form"

Ora Apri il file

app/design/frontend/{package}/{theme}/template/downloadable/checkout/cart/item/default.phtml
.

e scorrere fino alla fine del file e troverai il JavaScript che fa incremento e decremento della quantità.La funzione sarà simile a questa:

function plusQty(itemId){
    qty = $('qty'+itemId).value;
    qty = parseInt(qty);        
    qty++;
    $('qty'+itemId).value = qty;
}

function minusQty(itemId){
    qty = $('qty'+itemId).value;
    qty = parseInt(qty);        
    if(qty>0){
        qty--;
        $('qty'+itemId).value = qty;
    }
}
.

Cambia per questo:

function plusQty(itemId){
    qty = $('qty'+itemId).value;
    qty = parseInt(qty);        
    qty++;
    $('qty'+itemId).value = qty;
    document.getElementById("cart-form").submit();  
}

function minusQty(itemId){
    qty = $('qty'+itemId).value;
    qty = parseInt(qty);

    if(qty>0){
        qty--;
        $('qty'+itemId).value = qty;
         document.getElementById("cart-form").submit();   
    }
}
.

Nel caso in cui non hai caricato jquery (ancora), puoi anche trovare il <input> Elemento (o nel mio caso un elemento <select> da quando ho creato un campo a discesa per selezionare la quantità) con il nome name="cart[<?php echo $_item->getId() ?>][qty]" e aggiungere questo:

onchange="this.form.submit()"
.

Il file PHTML che devi modificare è situato qui:

app/design/frontend/{package}/{theme}/template/checkout/cart/item/default.phtml
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top