• Not Answered

Getting Product Information

Hello Everyone,

I need to get some information about the products on Sitecore. I have product Items.

I need this information,

Price, Image Link, Stock Status, Product Link

Do you have any idea how to fetch this information?

1 Reply

  • Hi

    You have one Product Template and under this there are some fields names Price, Image Link, Stocj Status, Product Link etc .

    You can get these field values using datasource or also directly using the item id .

    So using Item Id or Item Path

    Sitecore.Data.Items.Item someItem = Sitecore.Context.Database.GetItem("Give Your Item Path of your product content ");

    //get fields values
    string price= someItem["ProductPriceFieldName"];

    Now the same thing also could be access if we are using component base architecture, use datasource

    //In your component controller
    var dataSourceId = RenderingContext.CurrentOrNull.Rendering.DataSource;
    var dataSource = Sitecore.Context.Database.GetItem(dataSourceId);

    var viewModel = new ProductViewModel
    {
    ProductItems = dataSource.Children
    };
    return View(viewModel);


    //Product Viewmodel class

    public class ProductViewModel
    {
    public IEnumerable<Item> ProductItems { get; set; }
    }

    //Finally in your views you can achieve the all fields values

    @model ProductViewModel

    <h1>This is a Product Item Description!</h1>
    @foreach (var item in Model.ProductItems )
    {
    <h2>@item.Fields["Price"]</h2>
    <h2>@item.Fields["Image"]</h2>

    ....

    }