add to cart button

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
I try to add button "add to cart" to related product box but get this error
DataBinding: 'System.Data.Entity.DynamicProxies .Product_41AF00E728CA9C12156EFA331953CC0F4B76565227D385B09148EA61519D03B2' does not contain a property with the name 'ProductVariantId'.
What I'm doing wrong?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NopSolutions.NopCommerce.BusinessLogic;
using NopSolutions.NopCommerce.BusinessLogic.Audit;
using NopSolutions.NopCommerce.BusinessLogic.Configuration.Settings;
using NopSolutions.NopCommerce.BusinessLogic.Directory;
using NopSolutions.NopCommerce.BusinessLogic.Media;
using NopSolutions.NopCommerce.BusinessLogic.Orders;
using NopSolutions.NopCommerce.BusinessLogic.Products;
using NopSolutions.NopCommerce.BusinessLogic.Products.Attributes;
using NopSolutions.NopCommerce.BusinessLogic.SEO;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.Web.Modules;
using NopSolutions.NopCommerce.BusinessLogic.Manufacturers;
using NopSolutions.NopCommerce.BusinessLogic.Infrastructure;

namespace NopSolutions.NopCommerce.Web.Modules
{
    public partial class AArelated : BaseNopFrontendUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            var product = this.ProductService.GetProductById(this.ProductId);
            if (product == null || product.ProductVariants.Count == 0)
            {
                Response.Redirect(CommonHelper.GetStoreLocation());
            }
            BindProductVariantInfo(ProductVariant);
            BindProductInfo(product);
        }

        protected void BindProductInfo(Product product)
        {
            lShortDescription.Text = product.LocalizedShortDescription;
        }

        protected void BindProductVariantInfo(ProductVariant productVariant)
        {

            ctrlProductAttributes.ProductVariantId = productVariant.ProductVariantId;

            ctrlProductPrice.ProductVariantId = productVariant.ProductVariantId;

            //buttons
            if (!productVariant.DisableBuyButton)
            {
                txtQuantity.ValidationGroup = string.Format("ProductVariant{0}", productVariant.ProductVariantId);
                btnAddToCart.ValidationGroup = string.Format("ProductVariant{0}", productVariant.ProductVariantId);

                txtQuantity.Value = productVariant.OrderMinimumQuantity;
            }
            else
            {
                txtQuantity.Visible = false;
                btnAddToCart.Visible = false;
            }

            //final check - hide prices for non-registered customers
            if (!this.SettingManager.GetSettingValueBoolean("Common.HidePricesForNonRegistered") ||
                    (NopContext.Current.User != null &&
                    !NopContext.Current.User.IsGuest))
            {
                //
            }
            else
            {
                txtQuantity.Visible = false;
                btnAddToCart.Visible = false;
            }
        }
        public int ProductId
        {
            get
            {
                return CommonHelper.QueryStringInt("ProductId");
            }
        }

        public ProductVariant ProductVariant
        {
            get
            {
                Product product = this.ProductService.GetProductById(this.ProductId);
                if (product == null && product.ProductVariants.Count == 0)
                {
                    return null;
                }
                return product.ProductVariants[0];
            }
        }

        protected void OnCommand(object source, CommandEventArgs e)
        {
            var pv = ProductVariant;
            if (pv == null)
            {
                return;
            }

            string attributes = ctrlProductAttributes.SelectedAttributes;
            int quantity = txtQuantity.Value;

            try
            {
                if (e.CommandName == "AddToCart")
                {
                    string sep = "<br />";
                    var addToCartWarnings = this.ShoppingCartService.AddToCart(
                        ShoppingCartTypeEnum.ShoppingCart,
                        pv.ProductVariantId,
                        attributes,
                        quantity);
                    if (addToCartWarnings.Count == 0)
                    {
                        if (this.SettingManager.GetSettingValueBoolean("Display.Products.DisplayCartAfterAddingProduct"))
                        {
                            //redirect to shopping cart page
                            Response.Redirect(SEOHelper.GetShoppingCartUrl());
                        }
                        else
                        {
                            //display notification message
                            this.DisplayAlertMessage( GetLocaleResourceString("Products.ProductHasBeenAddedToTheCart"));
                        }
                    }
                    else
                    {
                        var addToCartWarningsSb = new StringBuilder();
                        for (int i = 0; i < addToCartWarnings.Count; i++)
                        {
                            addToCartWarningsSb.Append(Server.HtmlEncode(addToCartWarnings[i]));
                            if (i != addToCartWarnings.Count - 1)
                            {
                                addToCartWarningsSb.Append(sep);
                            }
                        }
                        string errorFull = addToCartWarningsSb.ToString();
                        lblError.Text = errorFull;
                        if (this.SettingManager.GetSettingValueBoolean("Common.ShowAlertForProductAttributes"))
                        {
                            this.DisplayAlertMessage(errorFull.Replace(sep, "\\n"));
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                this.LogService.InsertLog(LogTypeEnum.CustomerError, exc.Message, exc);
                lblError.Text = Server.HtmlEncode(exc.Message);
            }
        }
    }
}
12 years ago
Looks like the problem is in the .ascx, not code.

Have you modified anything?

Post your .ascx
12 years ago
I modified ascx just add controls that I need for related product box.

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AArelated.ascx.cs" Inherits="NopSolutions.NopCommerce.Web.Modules.AArelated" %>
<%@ Register TagPrefix="nopCommerce" TagName="ProductPrice1" Src="~/Modules/ProductPrice1.ascx" %>
<%@ Register TagPrefix="nopCommerce" TagName="ProductAttributes" Src="~/Modules/ProductAttributes.ascx" %>
<%@ Register TagPrefix="nopCommerce" TagName="NumericTextBox" Src="~/Modules/NumericTextBox.ascx" %>
<%@ Register TagPrefix="nopCommerce" TagName="SimpleTextBox" Src="~/Modules/SimpleTextBox.ascx" %>
<div class="one-variant-price">
    <nopCommerce:ProductPrice1 ID="ctrlProductPrice" runat="server" />
</div>
<div class="add-info">
    <nopCommerce:NumericTextBox runat="server" ID="txtQuantity" Value="1" RequiredErrorMessage="<% $NopResources:Products.EnterQuantity %>"
        RangeErrorMessage="<% $NopResources:Products.QuantityRange %>" MinimumValue="1"
        MaximumValue="999999" Width="50" />
    <asp:Button ID="btnAddToCart" runat="server" OnCommand="OnCommand" Text="<% $NopResources:Products.AddToCart %>"
        CommandName="AddToCart" CommandArgument='<%#Eval("ProductVariantId")%>' CssClass="productvariantaddtocartbutton" />
</div>
<div class="shortdescription">
    <asp:Literal ID="lShortDescription" runat="server" />
</div>
<div class="attributes">
    <nopCommerce:ProductAttributes ID="ctrlProductAttributes" runat="server" />
</div>
<asp:Label runat="server" ID="lblError" EnableViewState="false" CssClass="error" />
<div class="clear">
</div>
12 years ago
Change <%#Eval("ProductVariantId")%> to <%#Eval("ProductId")%>

Then find the MinProductVariant from it.
12 years ago
anyway I get error in code behing

string sep = "<br />";
                    var addToCartWarnings = this.ShoppingCartService.AddToCart(
                        ShoppingCartTypeEnum.ShoppingCart,
                        pv.ProductVariantId,
                        attributes,
                        quantity);
                    if (addToCartWarnings.Count == 0)


in this line:      this.ShoppingCartService.AddToCart(
12 years ago
What is the error?
12 years ago
That's the Visual Studio Error

Error  2  No overload for method 'AddToCart' takes 4 arguments
12 years ago
wxDevelopment wrote:
That's the Visual Studio Error

Error  2  No overload for method 'AddToCart' takes 4 arguments


I recommend downloading a fresh ProductBox and codebehind.
12 years ago
Thanks I fix it. and Last question I want also add PproductsAttributes to related product box, do I need some code? If I just registered a control ctrlProductAttributes it doesn't show me anything in product box.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.