You are on page 1of 8

Interceptors

Exercise
TABLE OF CONTENTS
BACKGROUND & GOAL ................................................................................................................................. 3
INSTRUCTIONS ............................................................................................................................................... 3
Preparation........................................................................................................................................................ 3
Step 1 • Create a Custom Extension ................................................................................................................ 3
Step 2 • Implement the Custom Interceptor ...................................................................................................... 3
Step 3 • Create Necessary Sample Data For The Commerce Shop ................................................................ 5
Verify ................................................................................................................................................................. 6
RECAP.............................................................................................................................................................. 7

2
BACKGROUND & GOAL

SAP Commerce Cloud has out-of-the-box support for customer group-based pricing. This means, if a
customer is assigned to a certain customer price group/list, a different price is displayed instead of the
normal product price for e.g., Photosmart E317 Digital Camera:

Normal price for everyone else: Special price for a certain group:

In this exercise, we are going to implement a new prepare interceptor to intercept customer models, so that
once a customer’s email address is detected to be a sap email address, the customer will be assigned to a
special price group and a different price of a specific product will then be displayed to this customer.

INSTRUCTIONS

Preparation
P1 Make sure you have a ready-to-use sap commerce instance with the B2C electronics shop –
recommended (Composable storefront is tested in this exercise, but if you have a Accelerator Storefront
only, it is ok as well).
(other shops would also be ok, but then exercise needs to be adapted to your different products)

You can also ask the trainer for a simple recipe called “live_sessions_dev”.

Step 1 • Create a Custom Extension


1. Execute ant extgen and create a custom extension to contain your implementation:

Template yempty
Name Any custom name, e.g. liveSessionInterceptor
Package name Any custom package name, e.g. de.hybris.liveSession

2. Add the new custom extension to your localextensions.xml file.


3. Import it into your IDE (manually, or by using commerce cloud plugin in eclipse)

Step 2 • Implement the Custom Interceptor


The idea is to assign any customer to a specified price group, if the customer’s email address is a sap email
address. Therefore, we are going to implement a PrepareInterceptor.

1. Write the class:


Please make sure you have selected the proper interceptor interface to implement, as well as the
package named interceptor.

package interceptor;

3
public class CustomerPrepareInterceptor implements PrepareInterceptor<CustomerModel>

It should be useful to define some constants for the implementation, e.g.:

private static final String SAPEmail = "sap.com";


private static final String SAPPriceGroup = "SAPEmployeePriceGroup";

Then we can implement the onPrepare() method, e.g.:

@Override
public void onPrepare(final CustomerModel customer, final InterceptorContext context)
throws InterceptorException
{
final String emailAddress = customer.getContactEmail();
if (emailAddress != null && emailAddress.endsWith(SAPEmail)){
customer.setEurope1PriceFactory_UPG(UserPriceGroup.valueOf(SAPPriceGroup));
}
}

2. Declare the new interceptor object in the spring context via YOUR_EXTENSION-spring.xml, e.g.:

<bean id="customerPrepareInterceptor"
class="YOUR_EXTEENSION_PACKAGE.interceptor.CustomerPrepareInterceptor">
</bean>

3. Now do the mapping for your interceptor in the same file, e.g.:

<bean id="customerPrepareInterceptorMapping"
class="de.hybris.platform.servicelayer.interceptor.impl.InterceptorMapping">
<property name="interceptor" ref="customerPrepareInterceptor" />
<property name="typeCode" value="Customer" />
</bean>

Finally, to make sure your changes take effect:


4. Compile your code by executing ant all under platform and
5. Restart your sap commerce cloud server.

4
Step 3 • Create Necessary Sample Data For The Commerce Shop
1. Log into backoffice (with admin/nimda)
2. Navigate to: Administration Cockpit | Price Settings | Prices | Customer Price Lists
3. Create a new customer price list with Identifier: SAPEmployeePriceGroup

4. Navigate to: Administration Cockpit | Price Settings | Prices | Price Rows


5. Create a new price row by
• Specifying a certain product (e.g. with the code: 300938, you can select online version
directly just for demo purpose)
• Assigning the previously created customer price list (SAPEmployeePriceGroup) as the
customer price list
• Setting Piece as the unit.
• Setting Price and Currency to 99 US Dollars

5
Verify
1. Start the composable storefront on the command prompt:
yarn start -ssl
(If you don’t have a composable storefront installed on your local system, you could also use the acc
storefront. After all, we just want to test the behavior of the custom intercetpor)

2. To verify your solution on the B2C shop, go directly to the products detail page (PDP) to see any
custom prices for Photosmart E317 Digital Camera:

• For Composable storefront:


https://localhost:4200/electronics-
spa/en/USD/product/300938/Photosmart%20E317%20Digital%20Camera

• For Accelerator storefront:


https://electronics.local:9002/yacceleratorstorefront/electronics/en/Open-
Catalogue/Cameras/Digital-Cameras/Digital-Compacts/Photosmart-E317-Digital-
Camera/p/300938

3. As an anonymous user, please ensure that this Camera shows a different price.
Note: If you just execute a full text search, Solr will only return general prices which are NOT
personalized – for performance reasons. Thus, you need to visit the PDP.

4. Click on SIGN IN/REGISTER on the top right of the B2C shop, register a new account and make sure
the email address contains “@sap.com” as the suffix.
Note: This triggers the creation of a new customer in the system. By adding properties this customer
needs to be saved, which results in calling our previously defined PrepareInterceptor logic.

6
5. Now go back to the previously selected product details page, you should note that the price is now
displayed with the special price (e.g., 99 US Dollars) you specified before.

RECAP
In this exercise, you learned how to implement a prepare interceptor to intercept customer data being saved
in Sap Commerce Cloud.

7
www.sap.com/contactsap

© 2023 SAP SE or an SAP affiliate company. All rights reserved.


No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP SE or an SAP affiliate company.

The information contained herein may be changed without prior notice. Some software products marketed by SAP SE and its distributors contain proprietary software components of other software vendors.
National product specifications may vary.

These materials are provided by SAP SE or an SAP affiliate company for informational purposes only, without representation or warranty of any kind, and SAP or its affiliated companies shall not be liable
for errors or omissions with respect to the materials. The only warranties for SAP or SAP affiliate company products and services are those that are set forth in the express warranty statements
accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.

In particular, SAP SE or its affiliated companies have no obligation to pursue any course of business outlined in this document or any related presentation, or to develop or release any functionality
mentioned therein. This document, or any related presentation, and SAP SE’s or its affiliated companies’ strategy and possible future developments, products, and/or platform directions and functionality are
all subject to change and may be changed by SAP SE or its affiliated companies at any time for any reason without notice. The information in this document is not a commitment, promise, or legal obligation
to deliver any material, code, or functionality. All forward-looking statements are subject to various risks and uncertainties that could cause actual results to differ materially from expectations. Readers are
cautioned not to place undue reliance on these forward-looking statements, and they should not be relied upon in making purchasing decisions.

SAP and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP SE (or an SAP affiliate company) in Germany and other
countries. All other product and service names mentioned are the trademarks of their respective companies. See www.sap.com/copyright for additional trademark information and notices.

You might also like