November Happy Hour will be moved to Thursday December 5th.

Add product to cart

Vote:
 

Hi,

I've created Model, Controller and View for my specific Products.

Each product has some properties like descriptionpartecipants, etc.

In a StartPage View I list all my products.

Now I need to add a simple "Add to chart" link for each product but, as I can see here http://world.episerver.com/documentation/Items/Developers-Guide/Episerver-Commerce/75/Orders/EPiServerCommerceSample-guideline/

void AddToCart(Entry entry)
{
    decimal quantity = 5;                       
    CartHelper cartHelper = new CartHelper(Cart.DefaultName);
    cartHelper.AddEntry(entry, quantity, false);
}

AddToCart method need an Entry type in input.

How can I add my products (of type Product) into the Cart?

Thanks

#149803
Jun 07, 2016 23:47
Vote:
 

Hi, 

As I have not received an answer, I changed my approach and transform AddToCart method like this:

public static string AddToCart(Product p)
        {
            OrderForm orderForm;
            LineItem lineItem;

            //If GetCart can't find the cart, it will create it for you
            
            Cart defaultCart = OrderContext.Current.GetCart(Cart.DefaultName, EPiServer.Security.PrincipalInfo.CurrentPrincipal.GetContactId());

            //manually create an orderform and add a lineitem to it
            orderForm = new OrderForm();
            lineItem = new LineItem();
            lineItem.Quantity = 1;
            lineItem.Code = p.Code;

            lineItem.DisplayName = p.DisplayName;
            orderForm.LineItems.Add(lineItem);

            //add the orderform to the cart
            defaultCart.OrderForms.Add(orderForm);

            //now save the changes to the database
            defaultCart.AcceptChanges();

            return String.Empty; // only for test.
        }

Now in my ProductList View I have:

<ul>

    @foreach (var p in StartPageController.GetProducts())
    {
        <li> @p.Title </li>
    }
</ul>

and all products in cart are listed properly.

The problem is that in Commerce Manager ->Carts->MyCart-> Order Summary is shown only 1 line

Here a screenshot.

Any idea?

#149840
Jun 08, 2016 16:49
Vote:
 

By creating a new orderform when you add a product to your cart, and adding it to your cart, every add to cart creates a new orderform. If that's what you mean by 1 line.

You could use something like this in your add to cart method, you have the code available in your product.

Entry entry = CatalogContext.Current.GetCatalogEntry(code);
cartHelper.AddEntry(entry, quantity, false);

.....
#149844
Jun 08, 2016 17:47
Vote:
 

Thank you Jeroen, bu  CatalogContext.Current.GetCatalogEntry(p.code) return nullMediachase.Commerce.Catalog.Objects.Entry.

In debug I see that p.code is not null and it is set to "p02_1" that is my product code.

Also CatalogContext.Current in debug is not null.

What'is wrong?

#149880
Jun 09, 2016 15:09
Vote:
 

Is your "Product" is based on VariationContent? If so, you should be able to get your variation by it's code.

btw, it may be that naming your class "product" could create a problem, not sure though, but it is also the ClassTypeId for ProductContent.

Have you looked at Quicksilver?

#149882
Jun 09, 2016 16:50
Vote:
 

Quicksilver starter kit is a good idea if you don't want to re-implement all shopping workflow.

I'm trying to start with Quicksilver. I rebuild the solution and try to install the database but when I run  Setup\SetupDatabases.cmd  to install the DB this error is shown:

Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..
Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : Named Pipes Provider: Could not open a connection to SQL Server [2]. .

My Sql Server work fine and MSSQL$SQLEXPRESS Services is running.

#149951
Jun 09, 2016 19:31
Vote:
 

Maybe you will have to change the connectionstring to fit your version of sql(express)?

#150166
Jun 13, 2016 11:28
Vote:
 

For quick silver, Go through the steps manually included in Setup.cmd file.

Those will be 
Creating 2 database
Creating Users.
Run a script from cms package
Run a script from commerce package
Run few workflow packages

Regards
/K

#150180
Jun 13, 2016 13:30
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.