Visualizzazione dei risultati da 1 a 8 su 8
  1. #1

    sommare subtotali con sconto quantitá in carrello compra

    Salve a tutti.
    Dopo varie ricerche e prove, ho trovato un carrello di compra in internet che fa quello che mi serve, pero ho bisogno di utilizzare sconti per quantitá.
    Ho modificato il codice e posso vedere il subtotale con lo sconto, pero il totale rimane con i valori senza sconto perche moltiplica il prezzo per la quantità e no i subtotali.
    Colpa la mia ignoranza non riesco a sommare i subtotali.
    allego il codice.
    Un grazie a tutti per leggere questo post e per un eventuale aiuto.
    Massimo


    // totale
    $this->cart_contents['cart_total'] += ($val['price'] * $val['qty']);



    $this->cart_contents['total_items'] += $val['qty'];


    //codice modificato per sconti

    if ($val['qty']<=3) {
    $this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']);
    }
    if (($val['qty']>=4) && ($item['discount'] = 1)) {
    $this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']/1.10);
    }
    if (($val['qty']>=8) && ($item['discount'] = 1)){
    $this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']/1.15);
    }
    if (($val['qty']>=12) && ($item['discount'] = 1)){
    $this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']/1.20);
    }
    if (($val['qty']>15) && ($item['discount'] = 1)) {
    $this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']/1.30);
    }
    Immagini allegate Immagini allegate

  2. #2
    Utente di HTML.it L'avatar di clasku
    Registrato dal
    Aug 2006
    Messaggi
    3,197
    Nei totali, non sommare $val ma tutti i valori dei subtotali che calcoli tu

  3. #3
    Quote Originariamente inviata da clasku Visualizza il messaggio
    Nei totali, non sommare $val ma tutti i valori dei subtotali che calcoli tu
    Grazie per rispondere, pero non so come si fa questo, se puoi aiutarmi, spiegando come, sarebbe fantastico.
    Grazie
    Massimo

  4. #4
    Utente di HTML.it L'avatar di clasku
    Registrato dal
    Aug 2006
    Messaggi
    3,197
    $cart_contents è un array, per ogni $key hai un subtotale
    Cicla con un foreach per fare la somma, oppure usa array_map()
    Secondo me, dove è definito ora il totale hai già la soluzione a portata di mano

  5. #5
    Quote Originariamente inviata da clasku Visualizza il messaggio
    $cart_contents è un array, per ogni $key hai un subtotale
    Cicla con un foreach per fare la somma, oppure usa array_map()
    Secondo me, dove è definito ora il totale hai già la soluzione a portata di mano
    Ok, proveró piu tardi quando rientro, anche se con i limiti che ho con la programmazione sara dura hahahah
    grazie mille

  6. #6
    non riesco a capire come fare,
    mi sono accorto che non avevo inserito tutto il codice della funzione, forse magari è più chiaro.
    P.S. non ci capisco una mazza... hahahaha

    protected function save_cart(){


    $this->cart_contents['total_items'] = $this->cart_contents['cart_total'] = 0;


    foreach ($this->cart_contents as $key => $val){


    // make sure the array contains the proper indexes


    if(!is_array($val) OR !isset($val['price'], $val['qty'])){


    continue;


    }




    $this->cart_contents['cart_total'] += ($val['price'] * $val['qty']);



    $this->cart_contents['total_items'] += $val['qty'];


    if ($val['qty']<=3) {
    $this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']);
    }
    if (($val['qty']>=4) && ($item['discount'] = 1)) {
    $this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']/1.10);
    }
    if (($val['qty']>=8) && ($item['discount'] = 1)){
    $this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']/1.15);
    }
    if (($val['qty']>=12) && ($item['discount'] = 1)){
    $this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']/1.20);
    }
    if (($val['qty']>15) && ($item['discount'] = 1)) {
    $this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']/1.30);
    }

    }

  7. #7
    Utente di HTML.it L'avatar di boots
    Registrato dal
    Oct 2012
    Messaggi
    1,626
    Dovrebbe bastare fare così:

    Codice PHP:
    ....
    foreach (
    $this->cart_contents as $key => $val){
       ...

       
    // $this->cart_contents['cart_total'] += ($val['price'] * $val['qty']);   Cancellalo

       
    $this->cart_contents['total_items'] += $val['qty'];
       if (
    $val['qty']<=3) {
            
    $this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']);
       }
       if ((
    $val['qty']>=4) && ($item['discount'] == 1)) {
            
    $this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']/1.10);
       }
       if ((
    $val['qty']>=8) && ($item['discount'] == 1)){
            
    $this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']/1.15);
       }
       if ((
    $val['qty']>=12) && ($item['discount'] == 1)){
            
    $this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']/1.20); 
      }
       if ((
    $val['qty']>15) && ($item['discount'] == 1)) {
            
    $this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']/1.30);
       }

       
    $this->cart_contents['cart_total'] += $this->cart_contents[$key]['subtotal']; // Aggiungi questo

    PS: negli if devi mettere $item['discount'] == 1, altrimenti fai un assegnazione
    PS2: che succede se $item['discount'] == 0 e $val['qty'] >3 ? non ti perdi il sub_totale?
    Dovresti mettere nel primo if:
    Codice PHP:
      if($val['qty']<=|| $item['discount'] == 0
    Ultima modifica di boots; 15-11-2016 a 12:02

  8. #8
    Grazie mille sembra che adesso funziona tutto alla perfezione, ho solo modiciato il codice nella parte

    ($item['discount'] == 1) sostituito con ($val['discount'] == 1)

    foreach ($this->cart_contents as $key => $val){
    // make sure the array contains the proper indexes
    if(!is_array($val) OR !isset($val['price'], $val['qty'])){
    continue;
    }

    $this->cart_contents['total_items'] += $val['qty'];
    if($val['qty']<=3 || $item['discount'] == 0)
    {
    $this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']);
    }
    if (($val['qty']>=4) && ($val['discount'] == 1)) {
    $this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']/1.10);
    }
    if (($val['qty']>=8) && ($val['discount'] == 1)){
    $this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']/1.15);
    }
    if (($val['qty']>=12) && ($val['discount'] == 1)){
    $this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']/1.20);
    }
    if (($val['qty']>15) && ($val['discount'] == 1)) {
    $this->cart_contents[$key]['subtotal'] = ($this->cart_contents[$key]['price'] * $this->cart_contents[$key]['qty']/1.30);
    }

    $this->cart_contents['cart_total'] += $this->cart_contents[$key]['subtotal']; // Aggiungi questo
    }

    un cosa bella sarebbe che cambiasse anche il valore prezzo unitario in base all sconto, esempio quando si arriva con il combo a 4 unitá il prezzo cambiasse invece di 300$ ponesse 273$
    ceil(prezzo / 1.10), avevo fatto varie prove pero non funzionavano


    ti posto il link del carrello:

    http://cietesting.com/

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.