SaaS CMS has officially launched! Learn more now.

cecilia@nansen.se
Jul 13, 2011
  9549
(6 votes)

An example using Business Foundation in EPiServer Commerce

The current EPiServer Commerce project I am working on has a requirement to allow logged-in customers to register products they have purchased, including those not purchased via the e-commerce site. They should then be able to see all of their registered products on their account page.

I decided to use the Business Foundation (BF) technology in EPiServer Commerce. You do this by creating a ‘Business Object’ in Commerce Admin, which you can read more about here.

My business object is called RegisteredProduct and contains two properties: CatalogEntryId and ContactId:


businessobject

The CatalogEntryId will hold the id of the product being registered (note that the product has to be in the e-commerce site catalog even if purchased elsewhere) and the ContactId holds the id of the logged-in customer registering the product.

In the code-behind for the ‘Register Products’ page, I have a method to create an instance of a RegisteredProduct BF object using the BusinessManager class. The properties are set from the values collected in the form and then the object is saved, again using the BusinessManager class:

 private void SaveRegisteredProduct()
 {
    var customerContact = CustomerContext.Current.CurrentContact;
    if (customerContact == null)
       return;

     // Creates an instance of your Business Object
     var registeredProduct = BusinessManager.InitializeEntity("RegisteredProduct");

     // Set values to properties in your Business Object
     registeredProduct.Properties["ContactId"].Value = 
          customerContact.PrimaryKeyId;
     registeredProduct.Properties["RegisteredProduct"].Value = 
          DropDownListProductModel.SelectedItem.Text;
     registeredProduct.Properties["CatalogEntryId"].Value = 
          int.Parse(DropDownListProductModel.SelectedValue);

     BusinessManager.Create(registeredProduct);
 }

To present a logged-in customer registered products, the BusinessManager’s List method is used. The method takes the name of the BF object type to query, the property to query on and a value to match against, in this case the logged-in customer’s id. Then for each RegisteredProduct object returned, the product can be read from the commerce catalog using the CatalogEntryId property as the key:

 private void ShowRegisteredProducts()
 {
     var customer = CustomerContext.Current.CurrentContact;
     if (customer == null)
         return;

     var registeredProducts = BusinessManager.List
        ("RegisteredProduct", new[] { FilterElement.EqualElement
        ("ContactId", customer.PrimaryKeyId) }).ToList();

     var entries = registeredProducts.Select
     (registeredProduct => CatalogContext.Current.GetCatalogEntry
     ((int) registeredProduct.Properties["CatalogEntryId"].Value)).ToList();

      RepeaterRegisteredProducts.DataSource = entries;
      RepeaterRegisteredProducts.DataBind();
 }

As I am new to EPiServer Commerce I don’t know if this is the best way to solve the problem. If anyone has an alternative solution then I welcome any feedback you have.

Jul 13, 2011

Comments

Fredrik von Werder
Fredrik von Werder Sep 22, 2011 05:01 PM

This really helped me a lot, I got stuck on some specified cast exception stuff when creating the item, but now it works like a charm.
Many thanks!

/Fredrik von Werder

Sep 29, 2011 11:23 AM

This is very useful and an excellent way of using Business Foundation, thanks for sharing this Cecilia!

Please login to comment.
Latest blogs
Creating Custom Actors for Optimizely Forms

Optimizely Forms is a powerful tool for creating web forms for various purposes such as registrations, job applications, surveys, etc. By default,...

Nahid | Jul 16, 2024

Optimizely SaaS CMS Concepts and Terminologies

Whether you're a new user of Optimizely CMS or a veteran who have been through the evolution of it, the SaaS CMS is bringing some new concepts and...

Patrick Lam | Jul 15, 2024

How to have a link plugin with extra link id attribute in TinyMce

Introduce Optimizely CMS Editing is using TinyMce for editing rich-text content. We need to use this control a lot in CMS site for kind of WYSWYG...

Binh Nguyen Thi | Jul 13, 2024

Create your first demo site with Optimizely SaaS/Visual Builder

Hello everyone, We are very excited about the launch of our SaaS CMS and the new Visual Builder that comes with it. Since it is the first time you'...

Patrick Lam | Jul 11, 2024