public Guid CreateOrUpdateBasket(Guid userId, string branchId, Basket basket, List <LineItem> items, bool runPipelines = false) { var updateOrder = new CommerceUpdate <Basket, CommerceModelSearch <Basket>, CommerceBasketUpdateOptionsBuilder>(); updateOrder.SearchCriteria.Model.UserId = userId.ToString(); updateOrder.SearchCriteria.Model.BasketType = 0; if (!string.IsNullOrEmpty(basket.Id)) { updateOrder.SearchCriteria.Model.Id = basket.Id; } else { updateOrder.SearchCriteria.Model.Name = basket.Name; } updateOrder.Model = basket; updateOrder.UpdateOptions.ToOptions().ReturnModel = (new Basket()).ToCommerceEntity(); updateOrder.UpdateOptions.RefreshBasket = runPipelines; if (items != null) { foreach (var item in items) { if (string.IsNullOrEmpty(item.Id) || item.Id == Guid.Empty.ToCommerceServerFormat()) { var lineItemCreate = new CommerceCreateRelatedItem <LineItem>(Basket.RelationshipName.LineItems); lineItemCreate.Model = item; updateOrder.RelatedOperations.Add(lineItemCreate); } else { var lineItemUpdate = new CommerceUpdateRelatedItem <LineItem>(Basket.RelationshipName.LineItems); lineItemUpdate.SearchCriteria.Model.Id = item.Id; lineItemUpdate.Model = item; updateOrder.RelatedOperations.Add(lineItemUpdate); } } } // create the request var response = FoundationService.ExecuteRequest(updateOrder.ToRequest()); if (response.OperationResponses.Count != 1) { return(Guid.Empty); } return(((CommerceUpdateOperationResponse)response.OperationResponses[0]).CommerceEntities[0].Id.ToGuid()); }
protected override void CreateRelatedItem(CommerceCreateRelatedItem createRelatedItemOperation) { CommerceEntity shipmentEntity = createRelatedItemOperation.Model; var newShipment = CommerceServerClassFactory.CreateInstance <Shipment>( shipmentEntity.ModelName, CommerceServerArea.Orders); shipmentEntity.Id = newShipment.ShipmentId.ToString("B"); Translator.ToExternalEntity(shipmentEntity, newShipment); OrderForm orderForm = CachedOrderGroup.GetDefaultOrderForm(); UpdateShippingLineItemsAssociation(newShipment, orderForm); orderForm.Shipments.Add(newShipment); }
protected override void CreateRelatedItem(CommerceCreateRelatedItem createRelatedItemOperation) { CommerceEntity paymentEntity = createRelatedItemOperation.Model; var newPayment = CommerceServerClassFactory.CreateInstance <Payment>( paymentEntity.ModelName, CommerceServerArea.Orders, new[] { paymentEntity.Properties[MetadataDefinitions.Payment.Properties.BillingAddressId], Guid.Empty, }); paymentEntity.Id = newPayment.PaymentId.ToString("B"); Translate(paymentEntity, newPayment); OrderForm orderForm = CachedOrderGroup.GetDefaultOrderForm(); orderForm.Payments.Add(newPayment); }
public Guid?AddItem(Guid cartId, LineItem newItem, Basket basket, bool runPipelines = false) { var updateOrder = new CommerceUpdate <Basket, CommerceModelSearch <Basket>, CommerceBasketUpdateOptionsBuilder>(); updateOrder.SearchCriteria.Model.UserId = basket.UserId; updateOrder.SearchCriteria.Model.BasketType = 0; updateOrder.SearchCriteria.Model.Id = cartId.ToCommerceServerFormat(); updateOrder.UpdateOptions.RefreshBasket = runPipelines; // disable running of pipelines to optimize save time updateOrder.UpdateOptions.ToOptions().ReturnModel = (new Basket()).ToCommerceEntity(); var lineItemUpdate = new CommerceCreateRelatedItem <LineItem>(Basket.RelationshipName.LineItems); lineItemUpdate.Model = newItem; updateOrder.RelatedOperations.Add(lineItemUpdate); updateOrder.UpdateOptions.ToOptions().ReturnModelQueries.Add( new CommerceQueryRelatedItem() { RelationshipName = Basket.RelationshipName.LineItems, Model = (new LineItem()).ToCommerceEntity() }); var response = FoundationService.ExecuteRequest(updateOrder.ToRequest()); CommerceServer.Foundation.CommerceRelationshipList lineItemsFromResponse = (response.OperationResponses[0] as CommerceUpdateOperationResponse) .CommerceEntities[0].Properties[Basket.RelationshipName.LineItems] as CommerceServer.Foundation.CommerceRelationshipList; var newId = lineItemsFromResponse.Where(b => !basket.LineItems.Any(i => i.Id.Equals(b.Target.Id))).FirstOrDefault(); if (newId != null) { return(newId.Target.Id.ToGuid()); } return(null); }