Table of Contents

Vouchers - Key Concepts and Features

The voucher extension allows the business to create and manage public promotions, bonus or compensation for the customers, discounts for the employees or gifts.

This extension is deprecated since SAP Hybris 6.3. You must use the coupons of the Promotion Engine for new projects. Promotions (Legacy) vs. Promotion Engine

Key Concepts

Key Features

Workflow

  1. The customer enters the voucher code.
  2. The eShop calls VoucherService.redeemVoucher(voucherCode, currentCart)
    • The cart is recalculated (using jalo)
    • Each restriction is checked
    • If the cart fulfills all the restrictions, the applicable entries are calculated. Based on the total price of the applicable entries -which fulfill all the restrictions- the value of the discount is calculated.
    • If one restriction isn't fulfilled, the voucher isn't redeemed.
    • This method doesn't reserved the voucher code. Another customer can still add the same code to another cart.
  3. If the customer adds or removes products to the cart and any voucher restriction isn't fulfilled, the voucher stays assigned to the cart but the discount would be zero.
  4. At the end of the checkout process, an order is created from the cart.
  5. The voucher code must be applied to the order calling VoucherService.redeemVoucher(voucherCode, order). This method returns a VoucherInvalidationModel which is an association between the voucher, the customer and the order. This limits the number of redemptions per customer.

The voucher may be released calling voucherService.release(aVoucherCode, aCart) or voucherService.release(aVoucherCode, anOrder).

Pitfalls

Use of the Jalo Layer in the voucher extension

The Voucher extension hasn't migrated yet to the model layer and the creation, redeem and removal of vouchers depends heavily on Jalo attributes which aren't accessible in the model layer. This means that any custom code you implement for the vouchers will have an akward mix of jalo and model layers calls. There are also some pitfalls you must be aware of.

Calculation of the cart

Place Order

When you clone a cart to create the order, the list of applied voucher codes won't be copied. The field AbstractOrder.appliedVoucherCodes is private. Due to this no accessors are created for the model. Another issue is that vouchers aren't consumed when you copy them to the order.

To avoid this issue you have to clone the order from the cart, remove the vouchers and reapply them:

public void transferVouchers(final CartModel cart, final OrderModel order)
  {
    final Collection appliedVouchers = getAppliedVoucherCodes(cart);
    //We remove any voucher discount which may be copied.
    removeAllVouchers(order);
    for (final String voucherCode : appliedVouchers)
    {
      redeemVoucher(voucherCode, order);
    }
  }
 
/**
   * Removes all the voucher information from the abstract order. It is used after an order was created from a cart.
   *
   * @param pAbstractOrderModel
   */
  private void removeAllVouchers(final AbstractOrderModel pAbstractOrderModel) {
    for (DiscountModel aDiscount : pAbstractOrderModel.getDiscounts()) {
      if (aDiscount instanceof VoucherModel) {
        try
        {
          removeDiscount(null, pAbstractOrderModel, (VoucherModel)aDiscount);
        }
        catch (AcmeVoucherNotFoundException e)
        {
          if (LOG.isDebugEnabled())
          {
            LOG.debug("Unable to remove the voucher " + aDiscount + " from the list of discount. Ignoring the error.");
          }
 
        }
      }
    }
    removeAllAppliedVoucherCodes(pAbstractOrderModel);
  }
 
private void removeDiscount(final String pVoucherCode, final AbstractOrderModel pCartModel, final VoucherModel pVoucherModel)
      throws AcmeVoucherNotFoundException
  {
    final List newDiscounts = new ArrayList(pCartModel.getDiscounts());
    if (newDiscounts.remove(pVoucherModel))
    {
      pCartModel.setDiscounts(newDiscounts);
    }
    else
    {
      throw new AcmeVoucherNotFoundException("Unable to find the voucher "
          + (pVoucherCode == null ? pVoucherModel : "with the code " + pVoucherCode)
          + " in the cart.");
    }
    // We send the changes to the jalo class.
    this.modelService.save(pCartModel);
  }
 
private void removeAllAppliedVoucherCodes(final AbstractOrderModel pAbstractOrderModel)
  {
    final AbstractOrder jaloOrder = this.modelService.getSource(pAbstractOrderModel);
    jaloOrder.setProperty(
          GeneratedVoucherConstants.Attributes.AbstractOrder.APPLIEDVOUCHERCODES, new ArrayList());
  }

I would prefer to avoid using Jalo, but I can't. This problem is a known issue: https://wiki.hybris.com/pages/viewpage.action?pageId=186258194

Serial Vouchers

The code of the voucher is generated using a cipher and random values stored in the database. Due to this, you cannot reuse the code in another database. If you try to export and import the serial vouchers using Impex, the codes won't work because you cannot export the fields key, alphabet and the secret key. If you would like to use serial vouchers during your integration tests, you would have to create the code on the fly:

String voucherCode = this.getCurrentCase().getVoucherCode();
if (voucherCode.length() == 3)
{
      voucherCode = this.getNewSerialVoucherCode(voucherCode);
}

–Based on SAP Hybris 5.3