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
The voucher may be released calling voucherService.release(aVoucherCode, aCart) or voucherService.release(aVoucherCode, anOrder).
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.
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
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