uCommerce How To: Create A Product Using The API

7. January 2010

If you want to create a product using the API, here’s how.

The class diagram for products looks like this:

Product

This might seem a bit verbose, but it’s all in favor of the flexible pricing and localization of products.

The data access is based on Active Record. If you haven’t already heart of that, check out the description on Wikipedia before reading any further.

Because localized descriptions, category placement and pricing information references the concrete product, we need to create the product itself first.

Before that, a short note on how to query the database for entites. If you want to find a specific entity, you can use the static SingleOrDefault<T>(Func<T, bool>) method. This will work for all entities:

var product = Product.SingleOrDefault(x => x.ProductId == 42);

If you want a collection of entities, use the Find<T>(Func<T, bool>) method. For example, to find all products modified within the last week:

var products = Product.Find(x => x.ModifiedOn >= DateTime.Now.AddDays(-7));

You can also use more advanced Linq queries. Getting products modified within the last week can be retrieved like this:

var products = from p in Product.All()
               where p.ModifiedOn >= DateTime.Now.AddDays(-7)
               select p;

You can also do joins and so on.

The Product

The create the product, simply create a new instance of the UCommerce.Entites.Product class, set the properties, and finally call the save method.

var product = new Product
                  {
                      Sku = "SKU-123456",
                      Name = "My Product",
                      AllowOrdering = true,
                      DisplayOnSite = true,
                      ProductDefinitionId = 1,  // Set to an existing defintion
                      ThumbnailImageMediaId = 1 // ID from Umbraco (nullable int)
                  };
product.Save();

After calling the save method, the ProductId property will return the ID of the new product.

Localized Product Description

To add a localized description for the product, create a new instance of the UCommerce.Entities.ProductDescription class.

var description = new ProductDescription
                      {
                          ProductId = product.ProductId,
                          CultureCode = "en-US", // or en-GB, da-DK etc.
                          DisplayName = "My display name",
                          ShortDescription = "My short description",
                          LongDescription = "My long description"
                      };
description.Save();

Like with the product, the ProductDescriptionId will return the new ID of the description after calling the save method.

Pricing Information

The save a price for a product, you find the ID of the price group you want the price to belong to. After that, you can create a new price using the PriceGroupPrice class (yes, I know).

var priceGroup = PriceGroup.SingleOrDefault(x => x.Name == "DKK");
var productPrice = new PriceGroupPrice
                       {
                           ProductId = product.ProductId,
                           PriceGroupId = priceGroup.PriceGroupId,
                           Price = 499.95m
                       };
productPrice.Save();

As of now, you have probably guess what the PriceGroupPriceId property will return.

Category Association

Last, but not least, you probably want to add the product to one or more categories. This is done using the CategoryProductRelation class.

var category = Category.SingleOrDefault(x => x.Name == "Software");
var relation = new CategoryProductRelation
                   {
                       ProductId = product.ProductId,
                       CategoryId = category.CategoryId
                   };
relation.Save();

That’s it, the new product is ready to be sold!

Let me know if you have any issues or questions.

Updated – How To Set Custom Properties

If you have added custom properties to a product using product definitions, you can access the properties simply by using the indexer on the product. Note that properties are always stored as strings (nvarchar), so you need to do your own casting, depending on the type of the property.

var property = product["MyProperty"];
property.Value = "New value";
property.Save();

uCommerce ,

Comments

1/7/2010 7:36:04 AM #
Pingback from topsy.com

Twitter Trackbacks for
        
        Lasse Eskildsen | uCommerce How To: Create A Product Using The API
        [lasseeskildsen.net]
        on Topsy.com
1/7/2010 7:38:15 AM #
As always a job well done. You're getting this week's UC Ninja Award Smile
1/8/2010 1:40:13 AM #
Nice guide, this has certainly gotten me very far in customizing uCommerce!

But how about those custom product properties you've got defined under Product Definitions?

Fx. I've got an enum property called "Brand". How do I set the value of that?
1/11/2010 8:38:19 AM #
Hi Søren - Good to hear!

I have updated the post with details on how to access custom properties.

1/11/2010 9:23:53 PM #
Got it working, ty for the update Smile

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading