Add to Cart Web Part
The Add to Cart Web Part provides the ability to add a product to the shopping cart only if the product is in stock. You can specify a quantity for the product, if needed.
Architecture
The Add to Cart Web Part interacts with the Product Provider Web Part through a Web Part connection. The Add to Cart Web Part sends a list of properties to the Product Provider Web Part, which then returns the product to the Add to Cart Web Part.
The Add to Cart Web Part consists of the following components:
AddToCartWebPart/SPAddToCartWebPart: loads all the necessary user controls dynamically. It is derived from BaseUserControlWebPart.
AddToCart.ascx User Control: houses the user interface components and the corresponding event handlers. The user interface components include the variant drop-down selector, quantity box, and an Add to Cart button.
BaseUserControlWebPart: encapsulates ASCX loading functionality.
SiteContext: gets information about the current request, such as UserID, Channel, and so on.
Properties
The following table lists the mapping of the properties for the Add to Cart Web Part:
Property |
Maps to |
---|---|
"Add to list" adds to default list |
AddToShopperListDefault |
Commerce "Basket" to use |
BasketName |
"Add to cart" button label |
ButtonLabel |
Layout : Horizontal alignment |
ControlAlignment |
Display "add to list" button |
DisplayAddToShopperList |
Product property used as display name |
DisplayNameProperty |
"Out of stock" message |
OutOfStockMessage |
Product property used as price |
PriceProperty |
Variant list item display format |
SelectorTemplate |
"Add to list" button text |
ShopperListButtonLabel |
Display "quantity" input box |
ShowQuantityBox |
Customization
You can customize the Add to Cart Web Part by modifying the AddToCart.ascx control, including the code-behind component.
Error Handling
There is no design-time error handing required for this Web Part.
Run Time Validation
The following table lists the Add to Cart Web Part fields that the Product Query Web Part validates at run time.
Field to validate |
Description |
Occurs |
System response |
---|---|---|---|
DisplayNameProperty |
Valid if DisplayNameProperty is set. |
When rendering the Web Part. |
Display error that indicates that the DisplayNameProperty is required. |
The Add to Cart Web Part handles the following scenarios:
Does not let shoppers add a product to the shopping cart with a quantity set to 0, a negative number, or a non-digit.
Does not let shoppers add a product to the shopping cart that is out of stock.
API Dependencies
There are no direct API dependencies for the Add to Cart Web Part. However, there are indirect dependencies to determine the following:
Check whether a product is out of stock
Retrieve the list price of the product
Retrieve the variant price of the product
Retrieve the display name of the product