Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
namespace Sitecore.Commerce.Plugin.AdventureWorks.Commands
{
using System;
using System.Threading.Tasks;
using Sitecore.Commerce.Core;
using Sitecore.Commerce.Core.Commands;
using Sitecore.Commerce.Plugin.AdventureWorks.Components;
using Sitecore.Commerce.Plugin.Carts;
/// <summary>
/// The set cart vip status command.
/// </summary>
public class SetCartVIPStatusCommand : CommerceCommand
{
/// <summary>
/// The _calculate cart pipeline.
/// </summary>
private readonly ICalculateCartPipeline _calculateCartPipeline;
/// <summary>
/// The _get cart pipeline.
/// </summary>
private readonly IGetCartPipeline _getCartPipeline;
private readonly IPersistEntityPipeline _persistEntityPipeline;
/// <summary>
/// Initializes a new instance of the <see cref="SetCartVIPStatusCommand"/> class.
/// </summary>
/// <param name="calculateCartPipeline">
/// </param>
/// <param name="getCartPipeline">
/// </param>
/// <param name="serviceProvider">
/// The service provider.
/// </param>
/// <param name="persistEntityPipeline"></param>
public SetCartVIPStatusCommand(
ICalculateCartPipeline calculateCartPipeline,
IGetCartPipeline getCartPipeline,
IServiceProvider serviceProvider,
IPersistEntityPipeline persistEntityPipeline) : base(serviceProvider)
{
this._calculateCartPipeline = calculateCartPipeline;
this._getCartPipeline = getCartPipeline;
this._persistEntityPipeline = persistEntityPipeline;
}
/// <summary>
/// The process of the command
/// </summary>
/// <param name="commerceContext">
/// The commerce context
/// </param>
/// <param name="cartId">
/// The cart Id.
/// </param>
/// <param name="applyVIPPricing">
/// The apply VIP Pricing.
/// </param>
/// <returns>
/// The <see cref="Task"/>.
/// </returns>
public async Task<Cart> Process(CommerceContext commerceContext, string cartId, bool applyVIPPricing)
{
try
{
// Set the new fields on the component attached to the cart
var resolveCartArgument = new ResolveCartArgument(
commerceContext.CurrentShopName(),
cartId,
commerceContext.CurrentShopperId());
var cart =
await this._getCartPipeline.Run(resolveCartArgument, commerceContext.GetPipelineContextOptions());
if (cart == null)
{
return null;
}
// Set the custom fields on the cart
cart.GetComponent<CartVIPStatusComponent>().ApplyVipPricing = applyVIPPricing;
// Save the cart here to make sure the new flag is set
var result = await this._persistEntityPipeline.Run(new PersistEntityArgument(cart), commerceContext.GetPipelineContextOptions());
return result.Entity as Cart;
}
catch (Exception e)
{
return await Task.FromException<Cart>(e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment