Class CartItemPricingUtils

java.lang.Object
com.broadleafcommerce.cartoperation.service.pricing.CartItemPricingUtils

public class CartItemPricingUtils extends Object
Utility to aid in pricing CartItems.
Author:
Chad Harchar (charchar), Nathan Moore (nathandmoore)
  • Constructor Details

    • CartItemPricingUtils

      public CartItemPricingUtils(CartItemPriceableTargetUtils cartItemPriceableTargetUtils, com.fasterxml.jackson.databind.ObjectMapper mapper, com.broadleafcommerce.common.extension.TypeFactory factory)
  • Method Details

    • getPriceableTargets

      public Set<com.broadleafcommerce.pricing.client.domain.PriceableTarget> getPriceableTargets(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.CartItem cartItem, Map<String,Integer> totalQuantityForTargetId)
      Builds the PriceableTargets for the given CartItem and its dependents.
      Parameters:
      cartItem - CartItem for which to build PriceableTargets
      totalQuantityForTargetId - A map of the SKU or pricing key of an underlying item from a cart item to the total quantity of that item across all cart items.
      Returns:
      the PriceableTargets for cartItem and its dependents.
    • determineMatchingPriceInfo

      @Deprecated(since="2.1.0", forRemoval=true) public <P extends com.broadleafcommerce.pricing.client.domain.PriceInfo> Optional<P> determineMatchingPriceInfo(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.CartItem cartItem, @NonNull @NonNull Collection<P> priceInfos)
      Deprecated, for removal: This API element is subject to removal in a future version.
    • determineMatchingPriceInfo

      @Deprecated(since="2.1.0", forRemoval=true) public <P extends com.broadleafcommerce.pricing.client.domain.PriceInfo> Optional<P> determineMatchingPriceInfo(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.CartItem cartItem, @NonNull @NonNull Collection<P> priceInfos, @Nullable String variantId)
      Deprecated, for removal: This API element is subject to removal in a future version.
    • determineMatchingPriceInfo

      public <P extends com.broadleafcommerce.pricing.client.domain.PriceInfo> Optional<P> determineMatchingPriceInfo(@NonNull @NonNull com.broadleafcommerce.pricing.client.domain.context.PriceContext priceContext, @NonNull @NonNull com.broadleafcommerce.cart.client.domain.CartItem cartItem, @NonNull @NonNull Collection<P> priceInfos, @Nullable String variantId)
      Determines the matching PriceInfo for a CartItem from among the given priceInfos. When given a variantId, this will prioritize a PriceInfo for a variant override pricing if it exists.
      Type Parameters:
      P - The type of the PriceInfo in the collection
      Parameters:
      priceContext - The PriceContext
      cartItem - The CartItem for which to determine a PriceInfo
      priceInfos - Collection of PriceInfos from which to choose a match
      variantId - The variant ID of a parent CartItem, if it exists
      Returns:
      The matching PriceInfo for cartItem.
    • populatePricingInfoForItem

      public CartItemPricingUtils.PriceInfoResponse populatePricingInfoForItem(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.CartItem cartItem, @NonNull @NonNull CatalogItem catalogItem, @Nullable Variant variant)
      Determines and populates the PriceInfos and pricing key for the CartItem based on the related CatalogItem and optionally the Variant. This specifically determines the values for CartItemAttributeConstants.Internal.SKU_PRICE_INFOS and CartItemAttributeConstants.Internal.PRICING_KEY_PRICE_INFOS
      Parameters:
      cartItem - The cart item to get price info for
      catalogItem - The catalog item represented by the cart item
      variant - Optional variant of the catalog item that is represented by the cart item
      Returns:
      The PriceInfos and pricing key for the CartItem
      Since:
      Cart Operations 2.3.0, Release Train 2.3.0
    • cleanPriceInfo

      public com.broadleafcommerce.pricing.client.domain.PriceInfo cleanPriceInfo(@NonNull @NonNull com.broadleafcommerce.pricing.client.domain.PriceInfo priceInfo)
      Exclude PriceableTarget information from the PriceInfo as it's unnecessary to calculate pricing.
      Parameters:
      priceInfo - The price info to clean up for addition to CartItem attributes
      Returns:
      The price info with target information excluded
      Since:
      Cart Operations 2.3.0, Release Train 2.3.0
    • getPricingKey

      public String getPricingKey(@NonNull @NonNull CatalogItem catalogItem, @NonNull @NonNull com.broadleafcommerce.cart.client.domain.CartItem cartItem)
      Returns the catalogItem price key, if specified, otherwise retrieves it from the cart item or generates a new one. This in general only needed for the DefaultProductType.MERCHANDISING_PRODUCT as it doesn't have the SKU and pricing key.
      Parameters:
      catalogItem - the catalogItem to get the pricing key from
      cartItem - the cart item to get the pricing key from
      Returns:
      the pricing key
      Since:
      Cart Operations 2.3.0, Release Train 2.3.0
    • getPriceInfoForVariant

      protected <T extends com.broadleafcommerce.pricing.client.domain.PriceInfo> Optional<T> getPriceInfoForVariant(com.broadleafcommerce.cart.client.domain.CartItem cartItem, List<T> candidatePriceInfos, @Nullable com.broadleafcommerce.pricing.client.domain.context.PriceContext priceContext)
    • hasAdjustmentPriceData

      protected boolean hasAdjustmentPriceData(com.broadleafcommerce.pricing.client.domain.PriceInfo info)
      Returns true if the record contains an adjustment with a price data id.
      Parameters:
      info -
      Returns:
    • isPricingKeyTarget

      protected boolean isPricingKeyTarget(com.broadleafcommerce.pricing.client.domain.PriceInfo priceInfo)
    • checkForPriceInfoForVariantFromPriceList

      protected <T extends com.broadleafcommerce.pricing.client.domain.PriceInfo> Optional<T> checkForPriceInfoForVariantFromPriceList(List<T> candidatePriceInfos, com.broadleafcommerce.cart.client.domain.CartItem cartItem)
    • isValidPriceSourceForVariantSku

      protected boolean isValidPriceSourceForVariantSku(com.broadleafcommerce.pricing.client.domain.PriceInfo priceInfo)
    • isTargetingVariantSku

      protected boolean isTargetingVariantSku(com.broadleafcommerce.pricing.client.domain.PriceInfo priceInfo, String sku)
    • filterPriceInfosForCurrency

      protected <T extends com.broadleafcommerce.pricing.client.domain.PriceInfo> List<T> filterPriceInfosForCurrency(List<T> priceInfos, @Nullable com.broadleafcommerce.pricing.client.domain.context.PriceContext priceContext)
    • hasPriceInfoFromPriceList

      protected <T extends com.broadleafcommerce.pricing.client.domain.PriceInfo> Optional<T> hasPriceInfoFromPriceList(List<T> candidatePriceInfos)
    • isBasePrice

      protected boolean isBasePrice(com.broadleafcommerce.pricing.client.domain.PriceInfo priceInfo)
    • isBaseSalePrice

      protected boolean isBaseSalePrice(com.broadleafcommerce.pricing.client.domain.PriceInfo priceInfo)
    • calculateAndAttachBundleItemProratedPrices

      public void calculateAndAttachBundleItemProratedPrices(com.broadleafcommerce.cart.client.domain.Cart cart, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Calculates the prorated prices for the dependent cart items based on the bundle item total price. For example, item 1: $6 and item 2: $4, then item 1 takes 60% of the bundle price and item 2 takes 40%. It creates an item adjustment that represents the normal merchandise total minus the prorated merchandise total. For example: unitPrice = $5 | quantity = 2 | proratedMerchTotal = $8 >> item adjustment of $2 (ie = ($5 * 2) - $8) This method also rounds the prorated prices using CartPricingRoundingHelper.roundListOfMonetaryAmounts(java.util.Map, javax.money.MonetaryAmount, javax.money.CurrencyUnit) to the bundle total to add the remainder unit amounts. This prevents the situation e.g when the bundle total is $40 but the sum of the prorated item prices is $39.99.
      Parameters:
      cart - the cart that contains the bundle products to calculate the prorated price
    • getPriceInfo

      public Optional<com.broadleafcommerce.pricing.client.domain.PriceInfo> getPriceInfo(com.broadleafcommerce.cart.client.domain.CartItem cartItem)
      Gets the PriceInfo from the given CartItem.
      Parameters:
      cartItem - the CartItem to get the PriceInfo from.
      Returns:
      the PriceInfo from the given CartItem
    • isItemPriceLimitedByQuantity

      public boolean isItemPriceLimitedByQuantity(com.broadleafcommerce.cart.client.domain.CartItem cartItem)
      Checks if the CartItem has a price with limited quantity available such as for a flash sale. By default, this checks CartItemAttributeConstants.Internal.IS_PRICE_LIMITED_BY_QUANTITY in the item's internal attributes.
      Parameters:
      cartItem - The CartItem to check.
      Returns:
      The result of the check.
    • setIsItemPriceLimitedByQuantityFlag

      public void setIsItemPriceLimitedByQuantityFlag(com.broadleafcommerce.cart.client.domain.CartItem cartItem)
      Sets the CartItemAttributeConstants.Internal.IS_PRICE_LIMITED_BY_QUANTITY attribute to true for the given CartItem.getInternalAttributes().
      Parameters:
      cartItem - CartItem to set the CartItemAttributeConstants.Internal.IS_PRICE_LIMITED_BY_QUANTITY flag for
    • removeIsItemPriceLimitedByQuantityFlag

      public void removeIsItemPriceLimitedByQuantityFlag(com.broadleafcommerce.cart.client.domain.CartItem cartItem)
      Removes the CartItemAttributeConstants.Internal.IS_PRICE_LIMITED_BY_QUANTITY attribute from the given CartItem.getInternalAttributes().
      Parameters:
      cartItem - CartItem to remove the CartItemAttributeConstants.Internal.IS_PRICE_LIMITED_BY_QUANTITY from
    • getProductType

      @Nullable protected Object getProductType(com.broadleafcommerce.cart.client.domain.CartItem cartItem)
    • calculateAndAttachBundleItemProratedPrices

      protected void calculateAndAttachBundleItemProratedPrices(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.CartItem bundleCartItem, @NonNull @NonNull javax.money.CurrencyUnit expectedCurrency)
    • calculateBundleDependentItemPriceProration

      protected Map<String,javax.money.MonetaryAmount> calculateBundleDependentItemPriceProration(com.broadleafcommerce.cart.client.domain.CartItem bundleCartItem, javax.money.CurrencyUnit expectedCurrency, javax.money.MonetaryAmount bundleTotalAmount)
    • attachBundleItemProratedPrices

      protected void attachBundleItemProratedPrices(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.CartItem bundleCartItem, Map<String,javax.money.MonetaryAmount> roundedDependentItemProratedPrice, javax.money.CurrencyUnit expectedCurrency)
    • createBundleDependentItemAdjustment

      protected com.broadleafcommerce.order.common.domain.Adjustment createBundleDependentItemAdjustment(javax.money.MonetaryAmount adjustmentAmount, Integer bundleItemQuantity)
    • attachBundleItemProratedPrices

      @Deprecated(since="1.7.2") protected void attachBundleItemProratedPrices(List<com.broadleafcommerce.cart.client.domain.CartItem> dependentCartItems, Map<String,javax.money.MonetaryAmount> roundedDependentItemProratedPrice, javax.money.CurrencyUnit expectedCurrency)
    • createBundleDependentItemAdjustment

      @Deprecated(since="1.7.2") protected com.broadleafcommerce.order.common.domain.Adjustment createBundleDependentItemAdjustment(javax.money.MonetaryAmount adjustmentAmount)
    • getItemPriceInfo

      protected <P extends com.broadleafcommerce.pricing.client.domain.PriceInfo> Optional<P> getItemPriceInfo(com.broadleafcommerce.cart.client.domain.CartItem dependentCartItem)
    • getPriceInfos

      protected <P extends com.broadleafcommerce.pricing.client.domain.PriceInfo> Map<String,P> getPriceInfos(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.CartItem cartItem)
    • calculateBundleDependentItemProratedPrice

      protected javax.money.MonetaryAmount calculateBundleDependentItemProratedPrice(com.broadleafcommerce.cart.client.domain.CartItem dependentCartItem, javax.money.MonetaryAmount bundleSubtotal, javax.money.MonetaryAmount dependentItemsTotal, javax.money.CurrencyUnit expectedCurrency)
    • getPriceInfoForDependentItem

      protected <P extends com.broadleafcommerce.pricing.client.domain.PriceInfo> Optional<P> getPriceInfoForDependentItem(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.CartItem cartItem, @NonNull @NonNull Collection<P> priceInfos, @Nullable String variantId)
      Determines the matching PriceInfo for a dependent CartItem.
      1. It checks based on its DependentItemDetails.getVariantPricing() for an override price that matches the parent CartItem's Variant if present.
      2. Then, it checks its AdditionalItemPricing from among the given priceInfos.
      3. Finally, it checks based on its parent ItemChoice's pricing in the DependentItemDetails.
      Type Parameters:
      P - The type of the PriceInfo in the collection
      Parameters:
      cartItem - The dependent CartItem for which to determine a PriceInfo
      priceInfos - Collection of PriceInfos from which to choose a match
      variantId - The variant ID of a parent CartItem, if it exists
      Returns:
      The matching PriceInfo for cartItem.
    • getPartitionTargetKey

      public String getPartitionTargetKey(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.CartItem cartItem)
      Given a CartItem, create a key that will be used to denote a single PriceableTarget for the count of all same CartItem instances. This is generally a simple pricingKey or sku determination for the comparison. However, in some cases, it may be helpful to embellish the key further to express differentiating pricing considerations - even for the same sku in the same cart. For example, this can arise in combined cart scenarios where the same CartItem may exist more than once in the cart with different pricing per line item - depending on the context in which the CartItem was added.
      Parameters:
      cartItem - The CartItem instance from which the partition target key is derived
      Returns:
      The partition target key
      See Also:
    • getTargetsForDependentItem

      protected List<com.broadleafcommerce.pricing.client.domain.PriceableTarget> getTargetsForDependentItem(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.CartItem cartItem, @NonNull @NonNull Map<String,Integer> totalQuantityForTargetId, @Nullable String parentCartItemVariantId)
      Builds the list of PriceableTargets for the given dependent CartItem.
      Parameters:
      cartItem - CartItem for which to build PriceableTargets
      totalQuantityForTargetId - A map of the SKU or pricing key of an underlying item from a cart item to the total quantity of that item across all cart items.
      parentCartItemVariantId - The ID of the Variant of the parent CartItem if CartItem.getVariantId() is present. This can affect the pricing of the dependent item.
      Returns:
      The PriceableTargets for cartItem
    • getItemChoiceTarget

      protected com.broadleafcommerce.pricing.client.domain.PriceableTarget getItemChoiceTarget(com.broadleafcommerce.cart.client.domain.DependentItemDetails details, String vendorRef, int quantity)
      Builds a PriceableTarget for the parent ItemChoice of a dependent CartItem representing a SpecificItemChoice.
      Parameters:
      details - The DependentItemDetails of the dependent CartItem
      vendorRef - The vendorRef
      quantity - The quantity of the dependent CartItem
      Returns:
      A PriceableTarget for the parent ItemChoice of a dependent CartItem representing a SpecificItemChoice.
    • getSpecificChoiceTarget

      protected com.broadleafcommerce.pricing.client.domain.PriceableTarget getSpecificChoiceTarget(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.DependentItemDetails details, @Nullable String vendorRef, int quantity)
      Builds a PriceableTarget for a dependent CartItem.
      Parameters:
      details - The DependentItemDetails of the dependent CartItem
      vendorRef - The vendorRef
      quantity - The quantity of the dependent CartItem
      Returns:
      A PriceableTarget for a dependent CartItem.
    • getSpecificChoiceVariantOverrideTarget

      protected com.broadleafcommerce.pricing.client.domain.PriceableTarget getSpecificChoiceVariantOverrideTarget(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.SpecificItemChoiceVariantPricingOverride override, @Nullable String vendorRef, int quantity)
    • itemIsTargeted

      protected boolean itemIsTargeted(com.broadleafcommerce.cart.client.domain.CartItem cartItem, com.broadleafcommerce.pricing.client.domain.PriceInfo priceInfo)
    • itemIsTargeted

      protected boolean itemIsTargeted(com.broadleafcommerce.cart.client.domain.CartItem cartItem, com.broadleafcommerce.pricing.client.domain.PriceInfo priceInfo, com.broadleafcommerce.cart.client.domain.AdditionalItemPricing itemPricing)
    • itemIsTargeted

      protected boolean itemIsTargeted(com.broadleafcommerce.cart.client.domain.CartItem cartItem, com.broadleafcommerce.pricing.client.domain.PriceInfo priceInfo, com.broadleafcommerce.cart.client.domain.DependentItemDetails details)
    • getCartItemPriceableTargetUtils

      protected CartItemPriceableTargetUtils getCartItemPriceableTargetUtils()
    • getMapper

      protected com.fasterxml.jackson.databind.ObjectMapper getMapper()
    • getFactory

      protected com.broadleafcommerce.common.extension.TypeFactory getFactory()
    • getRoundingHelper

      protected CartPricingRoundingHelper getRoundingHelper()
    • setRoundingHelper

      @Autowired public void setRoundingHelper(CartPricingRoundingHelper roundingHelper)