Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 20

Discussione: Aiuto su carrello

  1. #1

    Aiuto urgente!!

    Salve ragazzi, scusate il titolo ma ho un bel problema.

    E' da mesi che sto gestendo il sito aziendale e mi sono accorto di una cosa che va subito sistemata!!

    Il sito ha un carrello virtuale che di vendita dei prodotti aziendali, ma c'è un problema!

    Essendo che il sorgente per la parte del carrello virtuale le avevo prese da internet mi sono accorto solo ora che c'è un'anomalia da correggere al più presto.

    Praticamente se io dal mio pc aggiungo un prodotto nel carrello e poi chiudo e non faccio niente e poi un altro utente da un altra parte del mondo va nel carrello vede il prodotto che IO ho aggiunto!!
    Non è possibile, il carrello virtuale deve funzionare lato cliente non lato server.
    Come posso fare per risolvere subito questo problema?

    Vi faccio solo vedere il codice di cosa succede quando uno clicca su aggiungi:

    codice:
    public void AddItem(int productId, int scot) 
        {
    		// Create a new item to add to the cart
    		CartItem newItem = new CartItem(productId);
    
            // If this item already exists in our list of items, increase the quantity
    		// Otherwise, add the new item to the list
            if (Items.Contains(newItem)) {
    			foreach (CartItem item in Items) {
    				if (item.Equals(newItem)) {
    
                        //item.scot = scot;
    
    					item.Quantity++;
    					return;
    				}
    			}
    		} else {
    			newItem.Quantity = 1;
                newItem.scot = scot;
    			Items.Add(newItem);
    		}
    	}
    Credo che il fatto che crei un oggetto rimanga memorizzato sul server vero?
    Non c'è modo di farla funzionare come dovrebbe funzionare?

    Se volete vi passo tutto il codice del carrello.

    Vi prego aiutatemi

    Grazie in anticipo a tutti voi
    Response.Write("Tonyhhkx Programmer32");

  2. #2
    A occhio dipende da dove e come dichiari Items.

    probabilmente l'oggetto items è static o qualcosa del genere insomma è condiviso a livello applicazione mnetre dobvrebbe essere messo in sessione.

    in quel pezzo di codice non c'è nulla che non va.

  3. #3
    Originariamente inviato da Leonardo M.
    A occhio dipende da dove e come dichiari Items.

    probabilmente l'oggetto items è static o qualcosa del genere insomma è condiviso a livello applicazione mnetre dobvrebbe essere messo in sessione.

    in quel pezzo di codice non c'è nulla che non va.
    Ecco i 3 fiel che uso:

    CartItem.cs

    Codice PHP:
    using System;

    /**
     * The CartItem Class
     * 
     * Basically a structure for holding item data
     */
    public class CartItem IEquatable<CartItem> {
        
    #region Properties

        // A place to store the quantity in the cart
        // This property has an implicit getter and setter.
        
    public int Quantity getset; }
        public 
    int scot getset; }

        private 
    int _productId;
        public 
    int ProductId {
            
    get { return _productId; }
            
    set {
                
    // To ensure that the Prod object will be re-created
                
    _product null;
                
    _productId value;
            }
        }

        private 
    Product _product null;
        public 
    Product Prod {
            
    get {
                
    // Lazy initialization - the object won't be created until it is needed
                
    if (_product == null) {
                    
    _product = new Product(ProductId);
                }
                return 
    _product;
            }
        }

        public 
    string NomeCompleto {
            
    get { return Prod.NomeCompleto; }
        }

        public 
    string Codice
        
    {
            
    get { return Prod.Codice; }
        }

        public 
    decimal UnitPrice
        
    {
            
    get { return Prod.Price; }
        }

        public 
    decimal prezzoconIVA
        
    {
            
    get { return Prod.piuIVA; }
        }

        public 
    decimal TotalPrice {
            
    get { return prezzoconIVA Quantity; }
        }

        
    #endregion

        // CartItem constructor just needs a productId
        
    public CartItem(int productId) {
            
    this.ProductId productId;
        }

        
    /**
         * Equals() - Needed to implement the IEquatable interface
         *    Tests whether or not this item is equal to the parameter
         *    This method is called by the Contains() method in the List class
         *    We used this Contains() method in the ShoppingCart AddItem() method
         */
        
    public bool Equals(CartItem item) {
            return 
    item.ProductId == this.ProductId;
        }

    Product.cs

    Codice PHP:

    /**
     * The Product class
     * 
     * This is just to simulate some way of accessing data about  our products
     */
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.OleDb;



    public class 
    Product
    {

        public 
    int Id getset; }
        public 
    decimal Price getset; }
        public 
    decimal piuIVA getset; }
        public 
    string NomeCompleto getset; }


        public 
    string Codice getset; }

        
    OleDbConnection Accesdb = new OleDbConnection();

        
    string constring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|BDPSW.mdb;Persist Security Info=True";
        public 
    Product(int id)
        {
            
    this.Id id;
            
    Accesdb.ConnectionString constring;
            
    OleDbCommand comando = new OleDbCommand();
            
    comando.Connection Accesdb;
            
    comando.CommandText "SELECT * FROM SIS_LISTINO_WEB WHERE IdProdotto = " id "";
            try
            {
                
    Accesdb.Open();
                
    OleDbDataReader esecuzione comando.ExecuteReader();
                
    esecuzione.Read();
                
    string a esecuzione["PrezzoDef"].ToString();
                
    this.Price Convert.ToDecimal(esecuzione["PrezzoDef"].ToString());
                
    this.NomeCompleto esecuzione["Denominazione"].ToString();
                
    this.Codice esecuzione["Codice"].ToString();
            }
            catch
            {
              
            }
            finally
            {
                
    Accesdb.Close();
                
    CalcolaIva(id);
            }
        }

        public 
    int prendiIVA(int iva)
        {
            
    string a "";
            
    Accesdb.ConnectionString constring;
            
    OleDbCommand comando = new OleDbCommand();
            
    comando.Connection Accesdb;
            
    comando.CommandText "SELECT CodiceIva FROM SIS_PRODOTTI_IVA WHERE IdProdotto = " iva;
            try
            {
                
    Accesdb.Open();
                
    OleDbDataReader esecuzione comando.ExecuteReader();
                while (
    esecuzione.Read())
                {
                    
    esecuzione["CodiceIva"].ToString();
                }

            }
            catch
            {
              
            }
            finally
            {
                
    Accesdb.Close();
            }

            
    comando.CommandText "SELECT Aliquota FROM TEC_IVA WHERE Codice = '" "'";
            try
            {
                
    Accesdb.Open();
                
    OleDbDataReader esecuzione comando.ExecuteReader();
                while (
    esecuzione.Read())
                {
                    
    esecuzione["Aliquota"].ToString();
                }
            }
            catch
            {
        
            }
            finally
            {
                
    Accesdb.Close();
            }
            return 
    Convert.ToInt32(a);
        }

        public 
    void CalcolaIva(int id)
        {
            
    int  percentualeIVA prendiIVA(id);
            
    decimal iva this.Price + (( this.Price percentualeIVA) / 100 );
            
    this.piuIVA Convert.ToDecimal(String.Format("{0:N2}"iva));
        }


    Shoppingcart.cs

    Codice PHP:
    using System.Collections.Generic;
    using System.Web;


    using System;
    using System.Linq;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    /**
     * The ShoppingCart class
     * 
     * Holds the items that are in the cart and provides methods for their manipulation
     */
    public class ShoppingCart {
        
    #region Properties
        
        
    public List<CartItemItems get; private set; }
        
        
    #endregion

        #region Singleton Implementation

        // Readonly properties can only be set in initialization or in a constructor
        
    public static readonly ShoppingCart Instance;
        
    // The static constructor is called as soon as the class is loaded into memory
        
    static ShoppingCart() {
            
    // If the cart is not in the session, create one and put it there
            // Otherwise, get it from the session
            
    if (HttpContext.Current.Session["ASPNETShoppingCart"] == null) {
                
    Instance = new ShoppingCart();
                
    Instance.Items = new List<CartItem>();
                
    HttpContext.Current.Session["ASPNETShoppingCart"] = Instance;
            } else {
                
    Instance = (ShoppingCart)HttpContext.Current.Session["ASPNETShoppingCart"];
            }
        }

        
    // A protected constructor ensures that an object can't be created from outside
        
    protected ShoppingCart() { }

        
    #endregion

        #region Item Modification Methods
        /**
         * AddItem() - Adds an item to the shopping 
         */
        
    public void AddItem(int productIdint scot
        {
            
    // Create a new item to add to the cart
            
    CartItem newItem = new CartItem(productId);

            
    // If this item already exists in our list of items, increase the quantity
            // Otherwise, add the new item to the list
            
    if (Items.Contains(newItem)) {
                foreach (
    CartItem item in Items) {
                    if (
    item.Equals(newItem)) {

                        
    //item.scot = scot;

                        
    item.Quantity++;
                        return;
                    }
                }
            } else {
                
    newItem.Quantity 1;
                
    newItem.scot scot;
                
    Items.Add(newItem);
            }
        }

        
    /**
         * SetItemQuantity() - Changes the quantity of an item in the cart
         */
        
    public void SetItemQuantity(int productIdint quantity) {
            
    // If we are setting the quantity to 0, remove the item entirely
            
    if (quantity == 0) {
                
    RemoveItem(productId);
                return;
            }
            
    // Find the item and update the quantity
            
    CartItem updatedItem = new CartItem(productId);

            foreach (
    CartItem item in Items) {
                if (
    item.Equals(updatedItem)) {
                    
    item.Quantity quantity;
                    return;
                }
            }
        }

        
    /**
         * RemoveItem() - Removes an item from the shopping cart
         */
        
    public void RemoveItem(int productId) {
            
    CartItem removedItem = new CartItem(productId);
            
    Items.Remove(removedItem);
        }
        
    #endregion

        #region Reporting Methods
        /**
         * GetSubTotal() - returns the total price of all of the items
         *                 before tax, shipping, etc.
         */
        
    public decimal GetSubTotal() {
            
    decimal subTotal 0;
            foreach (
    CartItem item in Items)
                
    subTotal += item.TotalPrice;

            return 
    subTotal;
        }

        
    #endregion




    Se vuoi posto il codice lato client che richiama quelle classi, ma non credo serva. Sta tutto i quei 3 file
    Response.Write("Tonyhhkx Programmer32");

  4. #4
    quando aggiungo il prodotto chiamo:

    Codice PHP:
    protected void aggiungi(int id)
        {
            
    ShoppingCart.Instance.AddItem(id2);
        } 
    quando lo legge inceve:

    Codice PHP:
      protected void BindData()
        {
            
    // Let's give the data to the GridView and let it work!
            // The GridView will take our cart items one by one and use the properties
            // that we declared as column names (DataFields)
            
    try
            {
                
    gvShoppingCart.DataSource ShoppingCart.Instance.Items;
                
    gvShoppingCart.DataBind();
            }
            catch
            {
                        }

        } 
    Response.Write("Tonyhhkx Programmer32");

  5. #5
    Questa riga:
    public static readonly ShoppingCart Instance;

    è static quindi viene condivisa alivello applicazione (cioè in tutto il sito + o -)

    però togliere quel static non è banale.

  6. #6
    Originariamente inviato da Leonardo M.
    Questa riga:
    public static readonly ShoppingCart Instance;

    è static quindi viene condivisa alivello applicazione (cioè in tutto il sito + o -)

    però togliere quel static non è banale.
    in che senso non è banale? Che non basta togliere il static?
    Response.Write("Tonyhhkx Programmer32");

  7. #7
    Azz... come dovrei fare ragazzi?
    Response.Write("Tonyhhkx Programmer32");

  8. #8
    esato... puoi provare a toglierlo, ma tendenzialmente si usa static quando vuoi che la variabile sia visibile e condivisa in tutto il sito per tutti gli utenti. Quindi togliendolo l'istanza non sarà più visibile all'esterno di quella classe.

    Ho per caso appena trattato l'argomento in questo thread
    http://forum.html.it/forum/showthrea...readid=1482688

    Quindi penso che la soluzione sia complessa, cioè dovrai un po' di codice per risolverla.

    Il contenitore corretto dove emttere il carrello è questo: Session["carello"]

  9. #9
    Originariamente inviato da Leonardo M.
    esato... puoi provare a toglierlo, ma tendenzialmente si usa static quando vuoi che la variabile sia visibile e condivisa in tutto il sito per tutti gli utenti. Quindi togliendolo l'istanza non sarà più visibile all'esterno di quella classe.

    Ho per caso appena trattato l'argomento in questo thread
    http://forum.html.it/forum/showthrea...readid=1482688

    Quindi penso che la soluzione sia complessa, cioè dovrai un po' di codice per risolverla.

    Il contenitore corretto dove emttere il carrello è questo: Session["carello"]
    ho capito, purtroppo sono alle prime armi e non ho capito bene come adattare il mio codice alle cose scritte nel tuo altro thread.

    Response.Write("Tonyhhkx Programmer32");

  10. #10
    prova a togliere il static

    public static readonly ShoppingCart Instance;

    e vedi se risolvi (dubito ma prova) il passaggio che fa dell'istanza non mi è chiara purtroppo quindi non riesco ad aiutarti oltre. Alemno sai che il problema è togleire quel static in qualche modo...

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 © 2025 vBulletin Solutions, Inc. All rights reserved.