Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • ianhoughton 281 posts 605 karma points c-trib
    May 04, 2023 @ 16:30
    ianhoughton
    0

    Assign order to customer

    We currently create a new order, set properties / payment etc and then finalise the order all inside a custom Forms Workflow.

    We are creating the order using:

    var order = Order.Create(uow, orderId, store.Id, languageIsoCode, currency.Id, taxClass.Id, orderStatus.Id);
    

    then if the member is logged in, we are assigning their member key to the order with:

    if (!string.IsNullOrEmpty(context.Record.MemberKey))
    {
        order.AssignToCustomer(context.Record.MemberKey);
    }
    

    but when we check the order in the commerce section, the Customer Details section is empty.

    Other orders that we create using the normal method i.e:

    var order = _vendrApi.GetCurrentOrder(store.Id)
    

    the customer details are present?

  • Matt Brailsford 4124 posts 22220 karma points MVP 9x c-trib
    May 04, 2023 @ 16:32
    Matt Brailsford
    0

    Could do with seeing your whole code block and whether you are completing your uow

  • ianhoughton 281 posts 605 karma points c-trib
    May 04, 2023 @ 16:34
    ianhoughton
    0
    using (var uow = _unitOfWorkProvider.Create())
    {
        var store = _vendrApi.GetStore("xxxx");
    
        var properties = new Dictionary<string, string>
        {
            {BcnConstants.Commerce.Properties.Customer.Title, context.Record.GetRecordValue(new[] { "title" })},
            {BcnConstants.Commerce.Properties.Customer.FirstName, context.Record.GetRecordValue(new[] { "first_name", "firstName" })},
            {BcnConstants.Commerce.Properties.Customer.LastName, context.Record.GetRecordValue(new[] { "last_name", "lastName", "surname" })},
            {BcnConstants.Commerce.Properties.Customer.Email, context.Record.GetRecordValue(new[] { "email", "email_address", "emailAddress" })},
            {BcnConstants.Commerce.Properties.Other.FormId, context.Form.Id.ToString()},
            {BcnConstants.Commerce.Properties.Other.FormName, context.Form.Name},
            {BcnConstants.Commerce.Properties.Other.RecordId, context.Record.UniqueId.ToString()}
        };
        var orderId = Guid.NewGuid();
        var languageIsoCode = "en-GB";
        var currency = _currencyService.GetCurrency(store.Id, "GBP");
        var taxClass = _taxService.GetTaxClass(store.Id, "standard");
        var orderStatus = _orderStatusService.GetOrderStatus(store.Id, "new");
    
        // We manually create an order, so that its not stored against the users session
        var order = Order.Create(uow, orderId, store.Id, languageIsoCode, currency.Id, taxClass.Id, orderStatus.Id);
    
        order.AddProduct(sku, string.Empty, 1, properties);
    
        var freeShippingMethod = _shippingMethodService.GetShippingMethod(store.Id, "free");
        order.SetShippingMethod(freeShippingMethod.Id);
    
        var paymentMethod = _paymentMethodService.GetPaymentMethod(store.Id, "zeroValue");
        order.SetPaymentMethod(paymentMethod.Id);
    
        if (!string.IsNullOrEmpty(context.Record.MemberKey))
        {
            order.AssignToCustomer(context.Record.MemberKey);
        }
        else
        {
            if (_memberManager.Value.IsLoggedIn())
            {
                var member = _memberManager.Value.GetCurrentMemberAsync().Result;
                order.AssignToCustomer(member.Key.ToString());
            }
        }
    
        order.InitializeTransaction();
        var orderTotal = order.TotalPrice.Value.WithTax;
        order.Finalize(orderTotal, 0, Guid.NewGuid().ToString(), PaymentStatus.Authorized);
    
        _orderService.SaveOrder(order);
    
        uow.Complete();
    
        SetConfirmationUrl(context, pageUrl);
    
        return WorkflowExecutionStatus.Completed;
    }
    
  • Matt Brailsford 4124 posts 22220 karma points MVP 9x c-trib
    May 04, 2023 @ 17:04
    Matt Brailsford
    0

    Hmm, I can’t see a reason why it wouldn’t be persisted. Are you not seeing a value if you fetch the order back?

    On a side note, I’d avoid setting the OrderId manually as whilst it accepts a Guid, we actually use COMBs which ensure they are sortable in the database meaning better performance.

Please Sign in or register to post replies

Write your reply to:

Draft