Вы находитесь на странице: 1из 7

Task: Sending email to the customer at the checkout

1. Overview
In this documentation, I have created, configured and sent email in SAP Hybris at the
checkout. This email consists of all the cart items available in the customer cart.
The Email is triggered at the checkout on the click of the checkout button the event is
published, and the listener is invoked where the business process is created and once the
required details are saved under the process model the process is started.
The event and listener are created by extending required classes/interfaces separately.

An email in Hybris consists of at least 3 different components:


 Email Template and Email Page.
 Email Process.
 Email Context.

The below process consists of all the required classes and xml files to send the email at the
checkout.

2. Implementation
2.1 Business Process
I. I have created Item type CheckoutEmailProcessModel which is extending
StoreFrontCustomerProcess.

<itemtype code="CheckoutEmailProcess" extends="StoreFrontCustomerProcess"


autocreate="true" generate="true"
jaloclass="de.hybris.platform.commerceservices.jalo.process.CheckoutEmailProcess">

<description>Represents process that is used to send the email at the


checkout. </description>
<attributes>
<attribute qualifier="cart" type="Cart">
<persistence type="property" />
</attribute>
</attributes>
</itemtype>
II. Created a new xml file, named it checkoutEmailProcess.xml, I have defined the steps to
be executed by the Process Engine in this file.

<process xmlns="http://www.hybris.de/xsd/processdefinition" start="generateCheckoutEmail"


name="checkoutEmailProcess"
processClass="de.hybris.platform.commerceservices.model.process.CheckoutEmailProcessModel"
onError="error">
<action id="generateCheckoutEmail" bean="generateCheckoutEmail">
<transition name="OK" to="sendEmail"/>
<transition name="NOK" to="error"/>
</action>

<action id="sendEmail" bean="sendEmail">


<transition name="OK" to="removeSentEmail"/>
<transition name="NOK" to="failed"/>
</action>

<action id="removeSentEmail" bean="removeSentEmail">


<transition name="OK" to="success"/>
<transition name="NOK" to="error"/>
</action>

<end id="error" state="ERROR">Something went wrong. </end>


<end id="failed" state="FAILED">Could not send forgotten password email. </end>
<end id="success" state="SUCCEEDED">Sent forgotten password email. </end>
</process>

III. Registering the generateCheckoutEmail as a bean in Spring:

<bean id="generateCheckoutEmail" parent="abstractGenerateEmailAction">


<property name="frontendTemplateName" value="CheckoutEmailTemplate"/>
</bean>

IV. Registering the process as a ProcessDefinitionResource into Spring, to be recognized by


Hybris Process Engine.

<bean id="checkoutEmailProcessDefinitionResource"
class="de.hybris.platform.processengine.definition.ProcessDefinitionResource" >
<property name="resource"
value="classpath:/yacceleratorcore/processes/checkoutEmailProcess.xml"/>
</bean>
V. Created a new Java class that extends AbstractEmailContext, named
it CheckoutEmailContext.
An EmailContext is the one that holds the data to be passed to the email template, in my
case I have passed CartModel to the template.

public class CheckoutEmailContext extends AbstractEmailContext<CheckoutEmailProcessModel>


{
private CartData cartData;

@Override
public void init(final CheckoutEmailProcessModel checkoutEmailProcessModel, final
EmailPageModel emailPageModel)
{
super.init(checkoutEmailProcessModel, emailPageModel);
if (checkoutEmailProcessModel instanceof CheckoutEmailProcessModel)
{
setCartData(((CheckoutEmailProcessModel)
checkoutEmailProcessModel).getCartData());
}
}

public String getCartData() {


return cartData;
}

public void setCartData(CartData cartData) {


this.cartData = cartData;
}
}

VI. Register the CheckoutEmailContext as a bean in Spring.


<bean id="checkoutEmailContext"
class="de.hybris.platform.yacceleratorfacades.process.email.context.CheckoutEmailContext"
parent="abstractEmailContext" scope="prototype" >
<property name="cartConverter" ref=" cartConverter "/>
</bean>

2.2 Email Template


1. Created 2 Velocity templates, one for the email Subject and the other for the Body.

<%-- ... /import/emails/email-checkout-subject.vm --%>


Your powertools cart :${ctx.cartData.getCode()} details.
Body Template for the cart Details

<%-- ... /import/emails/email-checkout-body.vm --%>

<html>
<head>
<title> Email of the Checkout</title>
</head>
<body>
<center>
<h3>Cart ID :${ctx.cartData.code}</h3>
<table>
<tr>
<td><strong>Product</strong></td>
<td><strong>Item #</strong></td>
<td><strong>Price</strong></td>
<td><strong>Qty</strong></td>
<td><strong>Item Total</strong></td>
</tr>
#foreach($entry in $ctx.cartData.getEntries())
<tr>
<td>$entry.product.name</td>
<td>$entry.product.code</td>
<td>$entry.basePrice.value</td>
<td>$entry.quantity</td>
<td>$entry.totalPrice.value</td>
</tr>
#end
</table>
</center>
</body>
</html>
2. The following impex is used to create RendererTemplates for the Subject and
the Body, and to attach them to an EmailPageTemplate.

$contentCatalog=powertoolsContentCatalog
$contentCV=catalogVersion(CatalogVersion.catalog(Catalog.id[default=$contentCatalog]),Cata
logVersion.version[default=Online])[default=$contentCatalog:Online]
$wideContent=CMSImageComponent,BannerComponent
$jarEmailResource=jar:de.hybris.platform.powertoolsstore.constants.PowertoolsStoreConstant
s&/powertoolsstore/import/coredata/contentCatalogs/powertoolsContentCatalog/emails
$emailResource=jar:de.hybris.platform.yacceleratorcore.setup.CoreSystemSetup&/yaccelerator
core/import/emails
$emailPackageName=de.hybris.platform.yacceleratorfacades.process.email.context

# Create Template Renderers


INSERT_UPDATE RendererTemplate ;code[unique=true] ;contextClass

;templateScript[lang=en,translator=de.hybris.platform.commerceservices.impex.impl.F
ileLoaderValueTranslator];rendererType(code)[default='velocity']
;email-checkout-body
;$emailPackageName.CheckoutEmailContext ;$emailResource/email-checkout-body.vm
;email-checkout-subject
;$emailPackageName.CheckoutEmailContext ;$emailResource/email-checkout-subject.vm

# Create Email page Template


INSERT_UPDATE EmailPageTemplate ;$contentCV[unique=true];uid[unique=true]
;active ;frontendTemplateName ;subject(code)
;htmlTemplate(code) ;restrictedPageTypes(code)
;
;CheckoutEmailTemplate;true ;helloWorldEmail ;email-checkout-subject
;email-checkout-body ;EmailPage

# Create Email Page


INSERT_UPDATE EmailPage ;$contentCV[unique=true];uid[unique=true]
;masterTemplate(uid,$contentCV);approvalStatus(code)[default='approved']
;
;CheckoutEmail ;CheckoutEmailTemplate;

2.3. Email Config


Email properties to be added to configure properties in config/local.properties.

emailservice.send.enabled = true
mail.smtp.server=smtp.mailtrap.io
mail.smtp.port=2525
mail.smtp.user=**************
mail.smtp.password=*************
mail.use.tls=true
2.4. Sending Email
Sending a Cart Details Email by starting a new instance of the Checkout Email Process in the
CheckoutEmailEventListener.

public class CheckoutEmailEventListener extends


AbstractAcceleratorSiteEventListener<CheckoutEmailEvent>
{
private BusinessProcessService businessProcessService;
private ModelService modelService;
@Override
protected void onSiteEvent(final CheckoutEmailEvent event)
{
final CheckoutEmailProcessModel checkoutEmailProcessModel =
(CheckoutEmailProcessModel) getBusinessProcessService()
.createProcess("checkoutEmail-" + event.getCustomer().getUid() +
"-" + System.currentTimeMillis(),"checkoutEmailProcess");
checkoutEmailProcessModel.setSite(event.getSite());
checkoutEmailProcessModel.setCustomer(event.getCustomer());
checkoutEmailProcessModel.setLanguage(event.getLanguage());
checkoutEmailProcessModel.setCurrency(event.getCurrency());
checkoutEmailProcessModel.setStore(event.getBaseStore());
checkoutEmailProcessModel.setCart(event.getCartModel());
getModelService().save(checkoutEmailProcessModel);
getBusinessProcessService().startProcess(checkoutEmailProcessModel);
}
}

CheckoutEmailEvent

public class CheckoutEmailEvent extends AbstractCommerceUserEvent<BaseSiteModel>


{
private CartModel cartModel;
public CheckoutEmailEvent(final CartModel cartModel)
{
super();
this.cartModel = cartModel;
}
public CheckoutEmailEvent()
{
super();
}
public CartModel getCartModel()
{
return cartModel;
}
}
Screenshot of the sent email to the customer:

Вам также может понравиться