Skip to content

ProductVariant

// RB2 Core Connect
using System.Text.Json;
using CoreConnect.Commerce.Core;
using HotChocolate;
using HotChocolate.Types;
using Attribute = CoreConnect.Commerce.Core.Attribute;
namespace CoreConnect.Commerce.Catalog;
/// <summary>
/// The product variant.
/// </summary>
public class ProductVariant
{
/// <summary>
/// The ID of the Product Variant.
/// </summary>
public required string Id { get; set; }
/// <summary>
/// The SKU of the Product Variant.
/// </summary>
public required string Sku { get; set; }
/// <summary>
/// The Price Include Tax of the Product Variant.
/// </summary>
public Money? PriceInclTax { get; set; }
/// <summary>
/// The Price Exclude Tax of the Product Variant.
/// </summary>
public Money? PriceExclTax { get; set; }
/// <summary>
/// The Sale Price Include Tax (Discounted Price Include Tax) of the Product Variant.
/// </summary>
public Money? SalePriceInclTax { get; set; }
/// <summary>
/// The Sale Price Exclude Tax (Discounted Price Exclude Tax) of the Product Variant.
/// </summary>
public Money? SalePriceExclTax { get; set; }
/// <summary>
/// Discounted prices for a product based on quantity tiers
/// </summary>
public IEnumerable<TierPrices>? TierPrices { get; set; }
/// <summary>
/// The Default Image of the Product Variant.
/// </summary>
public Image? DefaultImage { get; set; }
/// <summary>
/// The Images of the Product Variant.
/// </summary>
public required IEnumerable<Image> Images { get; set; }
/// <summary>
/// The Attributes of the Product Variant.
/// </summary>
public required IEnumerable<Attribute> Attributes { get; set; }
/// <summary>
/// The stock info
/// </summary>
public ProductVariantStock? Stock { get; set; }
/// <summary>
/// The Inventories of the Product Variant.
/// </summary>
public IEnumerable<Inventory>? Inventories { get; set; }
/// <summary>
/// Price custom objects.Object is of type CentPrecisionMoney
/// </summary>
[GraphQLType(typeof(AnyType))]
public Dictionary<string, object>? CustomFields { get; set; }
/// <summary>
/// Available discounts for a product
/// </summary>
public List<string>? Discounts { get; set; }
}
public class ProductVariantStock
{
public int AvailableQuantity { get; set; }
public bool IsSellableWithoutStock { get; set; }
public bool IsAvailable { get; set; }
}
public class Inventory
{
public required string Id { set; get; }
public string? ChannelId { set; get; }
public long? AvailableQuantity { get; set; }
}
[ExtendObjectType<ProductVariant>]
public class ProductVariantExtension
{
private const string _parsedAttributes = "parsedAttributes";
public ProductVariantMetadata GetMetadata([Parent] ProductVariant product)
{
return SetMetadata(product);
}
private static ProductVariantMetadata SetMetadata(ProductVariant product)
{
var attributes = product.Attributes
.ToDictionary(
a => a.Name,
a => AttributeValueHelper.GetAttributeValue(a.Value ?? new AttributeValue())
);
var jsonAttributes = JsonSerializer.Serialize(attributes);
return new ProductVariantMetadata(_parsedAttributes, JsonDocument.Parse(jsonAttributes).RootElement);
}
}
public class ProductVariantMetadata
{
public ProductVariantMetadata(string key, JsonElement value)
{
Key = key;
Value = value;
}
public string Key { get; set; }
public JsonElement Value { get; set; }
}
public record TierPrices
{
public string? Discount { get; set; }
public long? Quantity { get; set; }
public Money? Price { get; set; }
}