Class BasicCartDiscountProcessor

All Implemented Interfaces:
BusinessObject, BusinessProcessor, CartDiscountProcessor

public class BasicCartDiscountProcessor extends BaseBusinessProcessor implements CartDiscountProcessor
Processes business logic having to do with discounts in a user's cart.

BasicCartDiscountProcessor is the default implementation of the CartDiscountProcessor interface for the application.

Author:
David Tobey
See Also:
  • Field Details

    • log

      static org.apache.commons.logging.Log log
    • justProcessGiftCertificates

      boolean justProcessGiftCertificates
  • Constructor Details

    • BasicCartDiscountProcessor

      public BasicCartDiscountProcessor()
  • Method Details

    • processAddItems

      public Map processAddItems(Collection newOrderItems) throws Exception
      Description copied from interface: CartDiscountProcessor
      Processes discounts upon adding an item or items to the user's cart. The default implementation simply calls processDiscounts(Collection)
      Specified by:
      processAddItems in interface CartDiscountProcessor
      Parameters:
      newOrderItems - The collection of OrderItem objects being added to the cart.
      Returns:
      A Map containing results of the processing. The default implementation returns null.
      Throws:
      Exception
    • processCouponCode

      public Map processCouponCode(Map parameters) throws Exception
      Description copied from interface: CartDiscountProcessor
      Processes discounts as a user enters a coupon code. The default implementation simply calls processDiscounts(Collection)
      Specified by:
      processCouponCode in interface CartDiscountProcessor
      Parameters:
      parameters - A Map with any parameters needed for the processing. The default implementation does not use this argument.
      Returns:
      A Map containing results of the processing. The default implementation returns null.
      Throws:
      Exception
    • processOnCheckout

      public Map processOnCheckout(Map parameters) throws Exception
      Description copied from interface: CartDiscountProcessor
      Processes discounts during checkout, as taxes and shipping are processed. The default implementation simply calls processDiscounts(Collection)
      Specified by:
      processOnCheckout in interface CartDiscountProcessor
      Parameters:
      parameters - A Map with any parameters needed for the processing. The default implementation does not use this argument.
      Returns:
      A Map containing results of the processing. The default implementation returns null.
      Throws:
      Exception
    • processAfterTaxes

      public Map processAfterTaxes(Map parameters) throws Exception
      Description copied from interface: CartDiscountProcessor
      Processes discounts after shipping and taxes have been calculated. This is so that if there are any gift certificates, they may be applied against the order's total rather than its subtotal as is the case with regular discounts. The default implementation simply calls processDiscounts(Collection)
      Specified by:
      processAfterTaxes in interface CartDiscountProcessor
      Parameters:
      parameters - A Map with any parameters needed for the processing. The default implementation does not use this argument.
      Returns:
      A Map containing results of the processing. The default implementation returns null.
      Throws:
      Exception
    • processCustomerLogin

      public Map processCustomerLogin(Map parameters) throws Exception
      Description copied from interface: CartDiscountProcessor
      Processes discounts as a customer logs in. Some discounts only apply if the user is logged in as a customer, so discounts must be rerun. The default implementation simply calls processDiscounts(Collection)
      Specified by:
      processCustomerLogin in interface CartDiscountProcessor
      Parameters:
      parameters - A Map with any parameters needed for the processing. The default implementation does not use this argument.
      Returns:
      A Map containing results of the processing. The default implementation returns null.
      Throws:
      Exception
    • processCustomerLogout

      public Map processCustomerLogout(Map parameters) throws Exception
      Description copied from interface: CartDiscountProcessor
      Processes discounts as a customer logs out. Some discounts only apply if the user is logged in as a customer, so discounts must be rerun. The default implementation simply calls processDiscounts(Collection)
      Specified by:
      processCustomerLogout in interface CartDiscountProcessor
      Parameters:
      parameters - A Map with any parameters needed for the processing. The default implementation does not use this argument.
      Returns:
      A Map containing results of the processing. The default implementation returns null.
      Throws:
      Exception
    • processOrderComplete

      public Map processOrderComplete(Map parameters) throws Exception
      Description copied from interface: CartDiscountProcessor
      Processes discounts as a user's order is completed. The default implementation simply calls processDiscounts(Collection)
      Specified by:
      processOrderComplete in interface CartDiscountProcessor
      Parameters:
      parameters - A Map with any parameters needed for the processing. The default implementation does not use this argument.
      Returns:
      A Map containing results of the processing. The default implementation returns null.
      Throws:
      Exception
    • processDiscounts

      public Map processDiscounts(Collection newOrderItems) throws Exception
      Processes discount for the current user and his or her cart. This method calls the following methods in turn:
      1. processRemoveDiscounts()
      2. processSkuDiscounts(Collection)
      3. processGlobalDiscounts()
      Note all the discounts already in the user's cart are removed, then added back in.
      Parameters:
      newOrderItems - The Collection of OrderItems in the user's cart.
      Returns:
      A Map containing results of the processing. This implementation returns null.
      Throws:
      Exception
    • processRemoveDiscounts

      public Map processRemoveDiscounts() throws Exception
      Description copied from interface: CartDiscountProcessor
      Removes all the discounts in the current user's cart. Updates the cart totals accordingly. Called before items are removed from the cart by BasicCartProcessor.processRemoveItem(OrderItem)
      Specified by:
      processRemoveDiscounts in interface CartDiscountProcessor
      Returns:
      A Map containing results of the processing. The default implementation returns null.
      Throws:
      Exception
    • processSkuDiscounts

      public Map processSkuDiscounts(Collection orderItems) throws Exception
      Processes discounts associated with specific SKUs in the current user's cart.
      Parameters:
      orderItems - The Collection of OrderItems in the user's cart.
      Returns:
      A Map containing results of the processing. This implementation returns null.
      Throws:
      Exception
    • processGlobalDiscounts

      public Map processGlobalDiscounts() throws Exception
      Processes global discounts, which are not associated with specific SKUs.
      Returns:
      A Map containing results of the processing. This implementation returns null.
      Throws:
      Exception
    • processShippingDiscounts

      public Map processShippingDiscounts() throws Exception
      Queries for and inserts the shipping discounts applicable for the user/order. The discount is not actually applied - a zero-total discount is inserted. The discount is actually applied during shipping processing, ie, processShippingDiscountsOnShippingOptions()
      Returns:
      A Map, null in this implementation.
      Throws:
      Exception
    • hasDiscountBeenAddedToOrder

      protected boolean hasDiscountBeenAddedToOrder(Order order, Discount discount)
    • processShippingDiscountsOnShippingOptions

      public Collection processShippingDiscountsOnShippingOptions(Collection shippingOptions) throws Exception
      Description copied from interface: CartDiscountProcessor
      Applies shipping discounts to each of the shipping options in the incoming Collection.
      Specified by:
      processShippingDiscountsOnShippingOptions in interface CartDiscountProcessor
      Returns:
      the same Collection of shipping options, with discounts applied
      Throws:
      Exception
    • processGiftCertificates

      public Map processGiftCertificates() throws Exception
      Queries for and applies the gift certs applicable for the user/order.
      Returns:
      A Map, null in this implementation.
      Throws:
      Exception
    • processTimesUsed

      public Map processTimesUsed() throws Exception
      Throws:
      Exception
    • processTimesUsed

      public Map processTimesUsed(int addOrSubtract) throws Exception
      Throws:
      Exception
    • getSkuIDs

      public Collection getSkuIDs(OrderItem oi, Collection matchingSkus)
    • getDiscountsBySkuIDs

      public Collection getDiscountsBySkuIDs(Collection skuIDs) throws Exception
      Returns a Collection of Discount objects associated with the given Collection of SKU ids. Used during SKU discount processing.
      Parameters:
      skuIDs - A Collection of SKU ids.
      Returns:
      A Collection of Discount objects
      Throws:
      Exception
    • getGlobalDiscounts

      public Collection getGlobalDiscounts() throws Exception
      Returns a Collection of global Discount objects that are not associated with a particular SKU. Used during global discount processing.
      Returns:
      A Collection of Discount objects
      Throws:
      Exception
    • loadShippingDiscounts

      public Collection loadShippingDiscounts() throws Exception
      Returns a Collection of shipping discounts - Discount objects matching the list of coupon codes that the current user has entered. Used during discount processing.
      Returns:
      A Collection of Discount objects
      Throws:
      Exception
    • loadGiftCertificates

      public Collection loadGiftCertificates() throws Exception
      Returns a Collection of gift certificate Discount objects matching the list of coupon/gift certificate codes that the current user has entered. Used during discount processing.
      Returns:
      A Collection of Discount objects
      Throws:
      Exception
    • isDiscountOk

      public boolean isDiscountOk(Discount discount, Double previousOrderTotal) throws Exception
      Checks that a given discount is applicable. Used by discount processing to help determine which discounts to apply, and in the presentation to determine which discounts to display to the user.

      This method calls the following methods in turn:

      1. isStartedAndNotExpired(Discount)
      2. isCouponOk(Discount)
      3. isUserQualified(Discount, Double)
      4. isCartItemsOk(Discount)
      5. isTimesUsedOk(Discount)
      Specified by:
      isDiscountOk in interface CartDiscountProcessor
      Parameters:
      discount - The discount whose applicability is being checked.
      previousOrderTotal - The previous order total of the current user, or null. Some discount are only applicable if the user has previously placed orders totaling a certain amount.
      Returns:
      true, the discount is applicable, otherwise, false.
      Throws:
      Exception
    • isDiscountOkIfItemInCart

      public boolean isDiscountOkIfItemInCart(Discount discount, Double previousOrderTotal, String productCode) throws Exception
      Checks if a given discount would be applicable, if the given product code were in the user's cart. Used by the presentation layer to communicate the discounts that would be available if the item were added to the cart.

      This method calls the following methods in turn:

      1. isStartedAndNotExpired(Discount)
      2. isCouponOk(Discount)
      3. isUserQualified(Discount, Double)
      4. isTimesUsedOk(Discount)
      Specified by:
      isDiscountOkIfItemInCart in interface CartDiscountProcessor
      Parameters:
      discount - The discount whose applicability is being checked.
      previousOrderTotal - The previous order total of the current user, or null. Some discount are only applicable if the user has previously placed orders totaling a certain amount.
      Returns:
      true, the discount is applicable, otherwise, false.
      Throws:
      Exception
    • isStartedAndNotExpired

      public boolean isStartedAndNotExpired(Discount discount) throws Exception
      Checks that the current time is not before a given Discount's start date or after its expiration date.
      Parameters:
      discount - The Discount to check.
      Returns:
      true, if the current time is not before the Discount 's start date or after its expiration date.
      Throws:
      Exception
    • isTimesUsedOk

      public boolean isTimesUsedOk(Discount discount) throws Exception
      Checks that a given Discount has not been used more than the maximum number of times it's allowed to be used.
      Parameters:
      discount - The Discount to check.
      Returns:
      true, if the Discount has not been used more than the maximum number of times it's allowed to be used.
      Throws:
      Exception
    • isCouponOk

      public boolean isCouponOk(Discount discount) throws Exception
      Checks that a given Discount either is not a coupon, or is a coupon whose code the current user has already entered in the session.
      Parameters:
      discount - The Discount to check.
      Returns:
      true, if the Discount either is not a coupon, or is a coupon whose code the current user has already entered in the session.
      Throws:
      Exception
      See Also:
    • isUserQualified

      public boolean isUserQualified(Discount discount, Double previousOrderTotal) throws Exception
      Checks that the current user is qualified to use a given Discount based on its settings.
      Parameters:
      discount - The Discount to check.
      previousOrderTotal - The previous order total of the current user, or null. Some discounts are only applicable if the user has previously placed orders totalling a certain amount.
      Returns:
      true, if the current user is qualified to use the Discount.
      Throws:
      Exception
    • isCartItemsOk

      public boolean isCartItemsOk(Discount discount) throws Exception
      Checks that the current user's cart contains the required items to use a given Discount based on its settings.
      Parameters:
      discount - The Discount to check.
      Returns:
      true, if the current user is qualified to use the Discount.
      Throws:
      Exception
    • isShippingMethodOk

      public boolean isShippingMethodOk(Discount discount) throws Exception
      Checks that the current user's cart contains the required shipping method to use a given Discount based on its settings.
      Parameters:
      discount - The Discount to check.
      Returns:
      true, if the current user is qualified to use the Discount.
      Throws:
      Exception
    • isItemWouldAllowDiscount

      public boolean isItemWouldAllowDiscount(Discount discount, String productCode) throws Exception
      Checks whether the given productCode is among the products that are required to be in the user's cart for the Discount to apply, based on its settings.
      Parameters:
      discount - The Discount to check.
      Returns:
      true, if the current user is qualified to use the Discount.
      Throws:
      Exception
    • getPreviousOrderTotal

      public Double getPreviousOrderTotal() throws Exception
      Looks up the total of all of the orders the current user has previously placed. Used to determine if certain discount are applicable.
      Returns:
      The total of all of the orders the current user has previously placed.
      Throws:
      Exception
    • getTimesUsedByCustomer

      public Integer getTimesUsedByCustomer(Discount discount) throws Exception
      Looks up the number of times the current user has used a given Discount.
      Parameters:
      discount - The Discount to check.
      Returns:
      An Integer representing the number of times the current user has used the Discount.
      Throws:
      Exception
    • clearDiscountNotAppliedReasons

      protected void clearDiscountNotAppliedReasons()
    • addDiscountNotAppliedReason

      protected void addDiscountNotAppliedReason(Discount discount, String reasonCode)
    • calculateDiscount

      public Map calculateDiscount(Discount discount, Double totalToDiscount, Integer quantity)
      Calculated the value of a discount as applied to the current user's cart.
      Parameters:
      discount - The Discount to be applied.
      totalToDiscount - The total that the discount is to be applied against. Discounts are not allowed to be more than the amount they are being applied against.
      quantity - The total number of items in the user's cart, or the quantity of an OrderItem in the user's cart. Some discounts are applied according to DiscountRange that corresponds to the total number of items in the user's cart (in the case of global discounts), or the quantity of an OrderItem correponding to a SKU associated with the discount (in the case of SKU discounts).
      Returns:
      A Map with information about the discount, and a key named "discountTotal", holding the calculated value of the discount.
    • excludeGiftCertificateProducts

      public Double excludeGiftCertificateProducts(Discount discount, Double totalToDiscount, Integer quantity)
      When computing a global discount, exclude the value of any gift certificates that are being purchased if called for by the setting 'excludeGiftCertificatesFromDiscounts'.
      Parameters:
      discount -
      totalToDiscount -
      quantity -
      Returns:
      Updated totalToDiscount that does not include the value of Gift Certificates
    • updateOrderAndDeliveryTotals

      public void updateOrderAndDeliveryTotals(Double discountTotal)
      Subtracts a discount total from the current user's order.
      Parameters:
      discountTotal - The discount total to subtract.
    • updateGiftCertOrderAndDeliveryTotals

      public void updateGiftCertOrderAndDeliveryTotals(Double discountTotal)
      Subtracts a gift certificate total from the current user's order's total (rather than its subtotal as is the case for other types of discounts).
      Parameters:
      discountTotal - The discount total to subtract.
    • addSkuDiscount

      public void addSkuDiscount(Collection skuDiscountsToApply, Discount discount, OrderItem oi)
    • addOrderItemToSkuDiscount

      public void addOrderItemToSkuDiscount(Discount ad, OrderItem oi)
    • defineDiscountSettings

      public Map defineDiscountSettings(Product product, Collection attributeSKUCollection, Collection storeDiscounts) throws Exception
      Description copied from interface: CartDiscountProcessor
      Inspects a Product and returns a Map of useful variables related to the state of the product's discounts.
      Specified by:
      defineDiscountSettings in interface CartDiscountProcessor
      attributeSKUCollection - An optional Collection of attribute-only SKUs used to define the inventory settings that are returned.
      storeDiscounts - An optional Collection of global, store discounts, used to determine if "promotions" for the product exist, ie, discounts that would be applied if the product were added to the cart.
      Returns:
      A Map of variables related to the product's discounts.
      Throws:
      Exception