Autoship Hook Migration

To improve processing and reporting efficiency for Autoships, we've created two all-new Autoship Extension Hooks (GetAutoshipChargeRules and GetAutoshipOrderRetryRules). These hooks are designed to complete operations in bulk and reduce load times. As a result, the former ShouldChargeAutoShip and ShouldRetryAutoshipOrder hooks have been deprecated. We will continue to support these legacy hooks, however.

πŸ“˜

Performance gains will only be experienced by those that have implemented the GetAutoshipChargeRules and GetAutoshipOrderRetryRules bulk hooks in place of the deprecated ShouldChargeAutoShip and ShouldRetryAutoshipOrder hooks.

Review the code examples below to begin your migration.

ShouldRetryAutoshipOrder Example

The ShouldRetryAutoshipOrder hook is designed to review one Autoship order at a time. Here is an example of an implementation of the now-deprecated hook:

public class ShouldRetryAutoshipOrder : IHook<ShouldRetryAutoshipOrderHookRequest, ShouldRetryAutoshipOrderHookResponse>
{
    public ShouldRetryAutoshipOrderHookResponse Invoke(ShouldRetryAutoshipOrderHookRequest request, Func<ShouldRetryAutoshipOrderHookRequest, ShouldRetryAutoshipOrderHookResponse> func)
    {
        var response = new ShouldRetryAutoshipOrderHookResponse
        {
            RetryRule = new RetryRule
            {
                Charge = false
            }
        };
​
        var dateof_5_OfMonth = new DateTime(request.RetryDate.Year, request.Order.OrderDate.Month, 5);
        var dateof_20_OfMonth = new DateTime(request.RetryDate.Year, request.Order.OrderDate.Month, 20);
        if (request.RetryDate.Date == dateof_5_OfMonth.Date && !request.Order.Void)
        {
            response.RetryRule.Charge = true;
        }
        else if (request.Autoship.Frequency == Frequency.Monthly
             && request.RetryDate.Date == dateof_20_OfMonth.Date
             && !request.Order.Void
        )
        {
            response.RetryRule.Charge = true;
        }
​
        return response;
    }
}

In the example above, you'll notice that the response only has a single RetryRule associated with it. Thus, all of the logic inside of the Invoke method is geared towards determining whether a single Autoship Order may be retried.

GetAutoshipOrderRetryRules Implementation

🚧

The same pattern shown in the example below applies to both the GetAutoshipChargeRules and GetAutoshipOrderRetryRules hook implementations.

The GetAutoshipOrderRetryRules hook is designed to review Autoships in bulk. Here is an example of an implementation using the new bulk hook:

πŸ“˜

This example is a retrofit of the code shown in the ShouldRetryAutoshipOrder class.

public class GetAutoshipOrderRetryRules : IHook<GetAutoshipOrderRetryRulesRequest, GetAutoshipOrderRetryRulesResponse>
{
    public GetAutoshipOrderRetryRulesResponse Invoke(GetAutoshipOrderRetryRulesRequest request, Func<GetAutoshipOrderRetryRulesRequest, GetAutoshipOrderRetryRulesResponse> func)
    {
        var response = new GetAutoshipOrderRetryRulesResponse
        {
            RetryRulesByAutoshipId = new Dictionary<int, RetryRule>()
        };
​
        foreach (var autoshipOrderInfo in request.AutoshipOrderInfos)
        {
            var dateof_5_OfMonth = new DateTime(request.RetryDate.Year, autoshipOrderInfo.Order.OrderDate.Month, 5);
            var dateof_20_OfMonth = new DateTime(request.RetryDate.Year, autoshipOrderInfo.Order.OrderDate.Month, 20);
            if (request.RetryDate.Date == dateof_5_OfMonth.Date && !autoshipOrderInfo.Order.Void)
            {
                response.RetryRulesByAutoshipId.Add(autoshipOrderInfo.Autoship.AutoshipId, new RetryRule
                {
                    Charge = true
                });
            }
            else if (autoshipOrderInfo.Autoship.Frequency == Frequency.Monthly
                 && request.RetryDate.Date == dateof_20_OfMonth.Date
                 && !autoshipOrderInfo.Order.Void
            )
            {
                response.RetryRulesByAutoshipId.Add(autoshipOrderInfo.Autoship.AutoshipId, new RetryRule
                {
                    Charge = true
                });
            }
            else
            {
                response.RetryRulesByAutoshipId.Add(autoshipOrderInfo.Autoship.AutoshipId, new RetryRule
                {
                    Charge = false
                });
            }
        }
​
        return response;
    }
}

In this example, the response is a dictionary of RetryRules with AutoshipIds as the key. Thus, all of the logic inside of the Invoke method is geared towards determining whether multiple Autoship Orders may be retried.