Documentation Home

Subscription Fulfillment Tutorial

There are many different ways to fulfill a Subscription for a Customer, depending on your requirements. The key component is how you extend and implement the SubscriptionStatusService.updateCustomer() method. The following document details how to fulfill a Subscription that gives Customers a permanent price discount on certain products.

Note: The following document assumes that you have already completed the Subscription Product Tutorial document

Create a Custom Field

Add a new Custom Field similar to the following:

Custom Field

Create an Offer

Create an automatic order item Offer targeting the new custom field:

Offer

Extend SubscriptionStatusService

Step 1: Extend and implement SubscriptionStatusService.updateCustomer(). This method is triggered on status change for a Subscription. The implementation here verifies that the Subscription for "Hot Sauce Club" Product has not been cancelled or suspended and updates the previously created custom field on the Customer to be true.

public class MyCompanySubscriptionStatusServiceImpl extends SubscriptionStatusServiceImpl {

    public static final String IS_SUBSCRIBED_TO_HOT_SAUCE_CLUB = "isSubscribedToHotSauceClub";
    public static final String HOT_SAUCE_CLUB = "Hot Sauce Club";

    @Override
    public Customer updateCustomer(Subscription subscription, Customer customer, SubscriptionStatus status) {
        Order primaryOrder = subscription.getPrimaryOrder();
        List<DiscreteOrderItem> discreteOrderItems = primaryOrder.getDiscreteOrderItems();
        DiscreteOrderItem discreteOrderItem = discreteOrderItems.get(0);
        Product product = discreteOrderItem.getProduct();
        String name = product.getName();
        if (name.equals(HOT_SAUCE_CLUB)) {
            CustomerAttribute attribute = new CustomerAttributeImpl();
            attribute.setName(IS_SUBSCRIBED_TO_HOT_SAUCE_CLUB);
            if (status.equals(SubscriptionStatus.CANCELLED) || status.equals(SubscriptionStatus.SUSPENDED)) {
                attribute.setValue("false");
            } else {
                attribute.setValue("true");
            }
            attribute.setCustomer(customer);
            customer.getCustomerAttributes().put(IS_SUBSCRIBED_TO_HOT_SAUCE_CLUB, attribute);
        }
        return customer;
    }
}

Step 2: Override the bean in your core applicationContext.xml

<bean id="blSubscriptionStatusService" class="com.broadleafcommerce.demo.service.subscription.MyCompanySubscriptionStatusServiceImpl"/>

All done! Now your Subscription should be properly fulfilled on Subscription status change.