Add to cart

Razor Example

Use BeginEkomForm to create the form for add to cart functionality. You can pass in Class and Id parameters.

VariantId is only used if you are adding products with variant to the cart.

StoreAlias is optional and is used if the context is not passed through for example with headless. Easiest way to get the current Store Alias is with the product object e.g. product.Store.Alias

    @using (Html.BeginEkomForm(FormType.AddToOrderProduct, "product-card__form"))
    {
        <input type="hidden" name="storeAlias" value="StoreAlias" />
        <input type="hidden" name="productId" value="@product.Key" />
        <input type="hidden" name="variantId" value="@variant.Key" />
        <input type="hidden" name="action" value="AddOrUpdate" />
            
        <input type="number" name="quantity" value="1" />
            
        <button type="submit">Add to cart</button>
    }

When submitted the controller will return a IOrderInfo json object.

If there is no stock available the controller will return 409 status code

Example with stock validation

@if (product.Stock > 0 || product.Backorder) 
{
    @using (Html.BeginEkomForm(FormType.AddToOrderProduct, "product__add-form"))
    {
        <input type="hidden" name="storeAlias" value="@product.Store.Alias" />
        <input type="hidden" name="productId" value="@product.Key" />
        <input type="hidden" name="quantity" value="1" />
        <input type="hidden" name="action" value="AddOrUpdate" />

        <button type="submit">
            <span class="button__text">Bæta við körfu</span>
        </button>
    }
} else
{
    <div>
        Product out of stock
    </div>
}

Last updated