Class DefaultCartPricingService

java.lang.Object
com.broadleafcommerce.cartoperation.service.pricing.DefaultCartPricingService
All Implemented Interfaces:
CartPricingService

public class DefaultCartPricingService extends Object implements CartPricingService
Author:
Chad Harchar (charchar)
  • Constructor Details

  • Method Details

    • priceCart

      public com.broadleafcommerce.cart.client.domain.Cart priceCart(@NonNull com.broadleafcommerce.cart.client.domain.Cart cart, @Nullable PriceCartRequest priceCartRequest, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Description copied from interface: CartPricingService
      Price the cart with the provided locale and currency.
      Specified by:
      priceCart in interface CartPricingService
      Parameters:
      cart - The cart on which to perform the operation.
      priceCartRequest - optional info to be used when deciding how to price the cart
      contextInfo - the context info
      Returns:
      the priced cart
    • priceCartWithCatalogPriceUpdates

      public com.broadleafcommerce.cart.client.domain.Cart priceCartWithCatalogPriceUpdates(@NonNull com.broadleafcommerce.cart.client.domain.Cart cart, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Description copied from interface: CartPricingService
      Reprice the cart, including applying the latest catalog-based prices for each of its items
      Specified by:
      priceCartWithCatalogPriceUpdates in interface CartPricingService
      Parameters:
      cart - The cart on which to perform the operation.
      contextInfo - the context info
      Returns:
      the priced cart
    • calculateProratedFulfillmentPricing

      @Deprecated(forRemoval=true, since="1.8.7") protected void calculateProratedFulfillmentPricing(@NonNull com.broadleafcommerce.cart.client.domain.Cart cart)
      Deprecated, for removal: This API element is subject to removal in a future version.
    • calculateProratedOrderOfferAdjustments

      protected void calculateProratedOrderOfferAdjustments(@NonNull com.broadleafcommerce.cart.client.domain.Cart cart)
      This method aids in calculating prorated fulfillment pricing for the given Cart, by populating the CartItem.getProratedOrderOfferAdjustments() to FulfillmentItem.getProratedOrderAdjustments().

      If there are multiple FulfillmentItems for one single CartItem, the prorated order offer adjustments are split across all the FulfillmentItems based on FulfillmentItem.getQuantity().

      Parameters:
      cart - the Cart to calculate the prorated fulfillment pricing for
    • calculateProratedFulfillmentCharge

      protected void calculateProratedFulfillmentCharge(@NonNull com.broadleafcommerce.cart.client.domain.Cart cart)
      Calculates the prorated fulfillment charge for the given FulfillmentItem.
      Parameters:
      cart - the Cart whose FulfillmentItems' prorated fulfillment charges are to be calculated.
    • calculateFulfillmentChargeProportion

      protected BigDecimal calculateFulfillmentChargeProportion(com.broadleafcommerce.cart.client.domain.FulfillmentGroup fg, com.broadleafcommerce.cart.client.domain.FulfillmentItem fi, Set<String> bandFields, BigDecimal sumFulfillmentItemWeightValue, javax.money.MonetaryAmount sumFulfillmentItemPriceValue)
      Calculates the proportion needed to determine each FulfillmentItem.getProratedFulfillmentCharge() from the FulfillmentGroup's PricedFulfillmentOption.getPrice(). This method will derive a proportion by price and/or weight based on the available PricedFulfillmentOption.getBandFields(). When no band data is available, the fallback method denoted by FulfillmentPriceConfigurationProperties.getFulfillmentPriceProportionMethod() will be used.
      Parameters:
      fg - the FulfillmentGroup whose fulfillment option price to prorate.
      fi - the FulfillmentItem whose proportion to calculate
      bandFields - the set of BandFields used to calculate the fulfillment price.
      sumFulfillmentItemWeightValue - the total sum of all FulfillmentItems' weight.
      sumFulfillmentItemPriceValue - the total sum of all FulfillmentItems' price.
      Returns:
      the calculated proportion
    • getProportionByQuantity

      protected BigDecimal getProportionByQuantity(com.broadleafcommerce.cart.client.domain.FulfillmentGroup fg, com.broadleafcommerce.cart.client.domain.FulfillmentItem fi)
      Default method to get the splitting proportion for the fulfillment item. This method returns the proportion based on quantity.
      Returns:
      the proportion by quantity
    • getProportionByLineItem

      protected BigDecimal getProportionByLineItem(com.broadleafcommerce.cart.client.domain.FulfillmentGroup fg, com.broadleafcommerce.cart.client.domain.FulfillmentItem fi)
      Default method to get the splitting proportion for the fulfillment item. This method returns equal proportions for each line item.
      Returns:
      the proportion by line item
    • getTotalWeight

      protected BigDecimal getTotalWeight(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.FulfillmentItem fi)
    • updateCatalogPricesForCartItems

      @Deprecated(forRemoval=true) protected com.broadleafcommerce.cart.client.domain.Cart updateCatalogPricesForCartItems(com.broadleafcommerce.cart.client.domain.Cart cart, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Deprecated, for removal: This API element is subject to removal in a future version.
    • updateCatalogPricesForCartItems

      protected com.broadleafcommerce.cart.client.domain.Cart updateCatalogPricesForCartItems(com.broadleafcommerce.cart.client.domain.Cart cart, @Nullable PriceCartRequest priceCartRequest, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
    • applyPricesToCart

      protected com.broadleafcommerce.cart.client.domain.Cart applyPricesToCart(com.broadleafcommerce.cart.client.domain.Cart cart, CatalogItemList<? extends CatalogItem> catalogItemList)
    • cleanPriceInfo

      protected com.broadleafcommerce.pricing.client.domain.PriceInfo cleanPriceInfo(@NonNull @NonNull com.broadleafcommerce.pricing.client.domain.PriceInfo priceInfo)
    • getPricingKey

      protected String getPricingKey(CatalogItem catalogItem, com.broadleafcommerce.cart.client.domain.CartItem cartItem)
      Returns the catalog item's pricing 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 catalog item to get the pricing key from
      cartItem - the cart item to get the pricing key from
      Returns:
      the pricing key
    • priceCart

      public com.broadleafcommerce.cart.client.domain.Cart priceCart(@NonNull com.broadleafcommerce.cart.client.domain.Cart cart, @Nullable PriceCartRequest priceCartRequest, boolean update, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Description copied from interface: CartPricingService
      Price the cart with the provided locale and currency.

      1. Initialize pricing request
      2. Clear current pricing information from cart
      3. Update cart item pricing
        1. Retrieve prices from the pricing provider
        2. Calculate each item's subtotal
        3. Record whether each item was priced
      4. If the items were all priced, continue, otherwise mark the cart as un-priced, update, and return
      5. Calculate fulfillment pricing by calling the fulfillment pricing provider
      6. Apply adjustments by calling the offer provider
      7. Calculate the cart totals
      8. Record the cart as priced, update (if specified), and return
      Specified by:
      priceCart in interface CartPricingService
      Parameters:
      cart - The cart on which to perform the operation.
      priceCartRequest - optional info to be used when deciding how to price the cart
      update - whether to update (save) the cart after pricing
      contextInfo - the context info
      Returns:
      the priced cart
    • priceCartItems

      public void priceCartItems(com.broadleafcommerce.cart.client.domain.Cart cart, @Nullable PriceCartRequest priceCartRequest, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Specified by:
      priceCartItems in interface CartPricingService
    • calculateTotals

      public void calculateTotals(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.Cart cart, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Description copied from interface: CartPricingService
      Calculate the totals for the cart. This assumes pricing such as item amount, adjustments, and taxes have been determined already.
      Specified by:
      calculateTotals in interface CartPricingService
      Parameters:
      cart - The cart to calculate totals for.
    • determineTaxCalculationStrategy

      protected String determineTaxCalculationStrategy(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.Cart cart, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Hook point to determine the TaxCalculationStrategy based on the given Cart.

      By default, DefaultTaxCalculationStrategies.ACTUAL is used when taxes can be calculated, otherwise DefaultTaxCalculationStrategies.SKIP is used.

      For different requirements for actual vs. estimated taxes, customization should be added here.

      Parameters:
      cart - the Cart to determine the TaxCalculationStrategy for
      contextInfo - additional sandbox and multitenant info
      Returns:
      the TaxCalculationStrategy for the given Cart
      See Also:
    • applyTaxes

      protected void applyTaxes(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.Cart cart, boolean estimate, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Applies taxes for the given Cart.
      Parameters:
      cart - the Cart to calculate the taxes for
      estimate - whether or not to apply estimated taxes, typically used when there's only a partial address associated with the cart.
      contextInfo - additional sandbox and multitenant info
    • determineTaxableAmounts

      protected void determineTaxableAmounts(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.Cart cart)
      Determines and sets FulfillmentItem.getMerchandiseTaxableAmount() and FulfillmentGroup.getFulfillmentTaxableAmount(). This will by default prorate the CartPricing.getAdjustmentsTotal() across FulfillmentItem.getMerchandiseTotalAmount().

      By default, a tax item is created for each group as a whole when calculating fulfillment tax, therefore no proration is necessary. To also prorate fulfillment-group adjustments across its items, extend this to add that proration.

      Parameters:
      cart - Cart with items to be taxed
    • prorateOrderAdjustments

      @Deprecated(since="1.7.2", forRemoval=true) protected void prorateOrderAdjustments(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.Cart cart, @NonNull @NonNull Collection<? extends com.broadleafcommerce.cart.client.domain.FulfillmentItem> fulfillmentItems, @NonNull @NonNull javax.money.MonetaryAmount adjustmentsTotal)
      Deprecated, for removal: This API element is subject to removal in a future version.
      since 1.7.2, adjustment prorations are now done in OfferServices
      Distributes the order-level adjustments to all of the items for tax purposes.
      Parameters:
      fulfillmentItems - FulfillmentItems onto which to distribute the adjustmentsTotal
      adjustmentsTotal - Total amount of the order-level adjustments to be distributed usually from CartPricing.getAdjustmentsTotal().
    • updateTaxableAmounts

      protected <T extends com.broadleafcommerce.cart.client.domain.CartItem> void updateTaxableAmounts(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.Cart cart, @NonNull @NonNull Collection<? extends com.broadleafcommerce.cart.client.domain.FulfillmentGroup> fulfillmentGroups, @NonNull @NonNull Collection<T> cartItems)
      Updates the taxable amount fields on FulfillmentItem. This should run after applying offers to ensure discounts are properly accounted for.

      By default, fulfillment taxable amounts are calculated at the fulfillment group level, and not the fulfillment item level. To calculate fulfillment taxes at the item level, extend this to calculate fulfillment taxable amounts for each fulfillment item.

      Parameters:
      fulfillmentGroups - All of the fulfillment groups in a cart to be taxed.
      cartItems - All of the cart items from a cart to check CartItem.isTaxable()
    • isGroupFulfillmentTaxable

      protected boolean isGroupFulfillmentTaxable(com.broadleafcommerce.cart.client.domain.FulfillmentGroup fg)
    • getTaxableItemFulfillmentTotal

      protected javax.money.MonetaryAmount getTaxableItemFulfillmentTotal(com.broadleafcommerce.cart.client.domain.FulfillmentGroup fg, javax.money.CurrencyUnit currency)
    • calculateItemSubtotal

      protected boolean calculateItemSubtotal(com.broadleafcommerce.cart.client.domain.Cart cart, boolean shouldPriceCart, com.broadleafcommerce.cart.client.domain.CartItem item, javax.money.CurrencyUnit expectedCurrency)
    • calculateItemSubtotal

      @Deprecated protected boolean calculateItemSubtotal(boolean shouldPriceCart, com.broadleafcommerce.cart.client.domain.CartItem item, javax.money.CurrencyUnit expectedCurrency)
      Deprecated.
    • updateCartItemPricing

      protected boolean updateCartItemPricing(com.broadleafcommerce.cart.client.domain.Cart cart, Locale locale, javax.money.CurrencyUnit expectedCurrency, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
    • updateCartItemPricing

      @Deprecated(forRemoval=true) protected boolean updateCartItemPricing(List<com.broadleafcommerce.cart.client.domain.CartItem> cartItems, Locale locale, javax.money.CurrencyUnit expectedCurrency, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Deprecated, for removal: This API element is subject to removal in a future version.
    • buildPriceCartRequest

      protected PriceCartRequest buildPriceCartRequest(com.broadleafcommerce.cart.client.domain.Cart cart)
    • setDefaultsForPriceCartRequest

      protected void setDefaultsForPriceCartRequest(com.broadleafcommerce.cart.client.domain.Cart cart, PriceCartRequest priceCartRequest)
    • isLocaleChange

      protected boolean isLocaleChange(PriceCartRequest request, com.broadleafcommerce.cart.client.domain.Cart cart)
    • isCurrencyChange

      protected boolean isCurrencyChange(PriceCartRequest request, com.broadleafcommerce.cart.client.domain.Cart cart)
    • getCurrencyFromCart

      protected javax.money.CurrencyUnit getCurrencyFromCart(com.broadleafcommerce.cart.client.domain.Cart cart)
    • prepareCartForPricing

      protected void prepareCartForPricing(com.broadleafcommerce.cart.client.domain.Cart cart, PriceCartRequest priceCartRequest, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
    • clearCartPrices

      protected void clearCartPrices(com.broadleafcommerce.cart.client.domain.Cart cart)
    • clearFulfillmentPricing

      protected void clearFulfillmentPricing(com.broadleafcommerce.cart.client.domain.FulfillmentGroup fulfillmentGroup)
    • clearItemPricing

      protected void clearItemPricing(com.broadleafcommerce.cart.client.domain.CartItem ci)
    • zeroIfNull

      protected final javax.money.MonetaryAmount zeroIfNull(@Nullable javax.money.MonetaryAmount amount, @Nullable javax.money.CurrencyUnit currency)
      Defaults the amount to zero if null.
      Parameters:
      amount - The MonetaryAmount to default to zero
      currency - The amount's CurrencyUnit
      Returns:
      The amount or zero if null.
    • buildCatalogItemRequests

      protected Collection<CatalogItemRequest> buildCatalogItemRequests(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.Cart cart, @NonNull @NonNull List<com.broadleafcommerce.cart.client.domain.CartItem> cartItems, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
    • catalogItemRequestAlreadyExist

      protected boolean catalogItemRequestAlreadyExist(@NonNull @NonNull Set<CatalogItemRequest> catalogItemRequests, @NonNull @NonNull com.broadleafcommerce.cart.client.domain.CartItem cartItem, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Hook point to prevent duplicate CatalogItemRequests from being built based on the CartItem.

      By default, we filter on the product ID.

      Parameters:
      catalogItemRequests - the list of requests to pass to the CatalogProvider
      cartItem - the CartItem to build the CatalogItemRequest
      contextInfo - context information around sandbox and multitenant state
      Returns:
      true if building a new CatalogItemRequest would be a duplicate of an existing request in catalogItemRequests
    • buildCatalogItemRequest

      protected CatalogItemRequest buildCatalogItemRequest(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.CartItem cartItem, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
      Hook point to create the CatalogItemRequest and populate any additional attributes that should be passed to the CatalogProvider when fetching the catalog representation for the cart item.
      Parameters:
      cartItem - the CartItem to build the CatalogItemRequest
      contextInfo - context information around sandbox and multitenant state
      Returns:
      the populated CatalogItemRequest for the CartItem
    • getCatalogItemKeyFromCartItem

      protected String getCatalogItemKeyFromCartItem(com.broadleafcommerce.cart.client.domain.CartItem cartItem)
      Hook point to determine what the catalog item is keyed on from the CartItem.

      By default, this is set to the product ID.

      Parameters:
      cartItem - the cart item to identify a catalog item key from
      Returns:
      the key for the catalog item
    • getPriceContext

      @Nullable protected com.broadleafcommerce.pricing.client.domain.context.PriceContext getPriceContext(@Nullable Locale locale, @Nullable javax.money.CurrencyUnit currency)
    • getFulfillmentPricingService

      @Deprecated(forRemoval=false, since="1.9") public FulfillmentPricingService getFulfillmentPricingService()
      Deprecated.
      Backward-compatible getter for FulfillmentPricingService. This should only be used if the shipping libraries (shipping-common and shipping-services) are in use. In 1.9, the shipping libraries have been replaced with fulfillment-common and fulfillment-services. A CommonFulfillmentPricingService bean is configured by default in CartOps in 1.9. Use getCommonFulfillmentPricingService() instead.
      Returns:
      The CommonFulfillmentPricingService field cast to FulfillmentPricingService.
    • getCartItemPricingUtils

      protected CartItemPricingUtils getCartItemPricingUtils()
    • getCartProvider

      protected CartProvider getCartProvider()
    • getPricingProvider

      protected PricingProvider getPricingProvider()
    • getOfferProvider

      protected OfferProvider getOfferProvider()
    • getCatalogProvider

      protected CatalogProvider<? extends CatalogItem> getCatalogProvider()
    • getCartItemConfigurationService

      @Deprecated protected CartItemConfigurationService<? extends CatalogItem> getCartItemConfigurationService()
      Deprecated.
    • getTaxService

      protected TaxService getTaxService()
    • getCommonFulfillmentPricingService

      protected CommonFulfillmentPricingService getCommonFulfillmentPricingService()
    • getObjectMapper

      protected com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
    • getCartTotalsCalculator

      protected CartTotalsCalculator getCartTotalsCalculator()
    • getRoundingHelper

      protected CartPricingRoundingHelper getRoundingHelper()
    • getCartOperationServiceProperties

      protected CartOperationServiceProperties getCartOperationServiceProperties()
    • getTypeFactory

      protected com.broadleafcommerce.common.extension.TypeFactory getTypeFactory()
    • getMessageSource

      protected org.springframework.context.MessageSource getMessageSource()
    • getCartOpsServiceOfferUtils

      protected CartOperationServiceOfferUtils getCartOpsServiceOfferUtils()
    • setCartOpsServiceOfferUtils

      @Autowired public void setCartOpsServiceOfferUtils(CartOperationServiceOfferUtils cartOpsServiceOfferUtils)
    • setStaleCartItemsService

      @Autowired public void setStaleCartItemsService(StaleCartItemsService staleCartItemsService)
    • getStaleCartItemsService

      protected StaleCartItemsService getStaleCartItemsService()
    • setBroadleafProductService

      @Autowired public void setBroadleafProductService(BroadleafProductService<Product> broadleafProductService)
    • getBroadleafProductService

      protected BroadleafProductService<Product> getBroadleafProductService()
    • setFulfillmentOptionRequestService

      @Autowired @Lazy public void setFulfillmentOptionRequestService(FulfillmentOptionRequestService fulfillmentOptionRequestService)
    • getFulfillmentOptionRequestService

      protected FulfillmentOptionRequestService getFulfillmentOptionRequestService()
    • setMapper

      @Autowired public void setMapper(com.fasterxml.jackson.databind.ObjectMapper mapper)
    • getMapper

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

      @Autowired public void setConfigurationProperties(FulfillmentPriceConfigurationProperties configurationProperties)
    • getConfigurationProperties

      protected FulfillmentPriceConfigurationProperties getConfigurationProperties()