GetOrCreateCurrentOrder() after BeginPaymentForm()
Hi,
I'm having problems finding the correct way of doing this.
On my cart page I edit the quantity of the cart. I do this by GetOrCreateCurrentOrder().AsWritable() and then editing the order. The same goes for when I add new products to the cart.
My scenario is
1. Add product to cart
2. Go to the step in the Checkout with the BeginPaymentForm()
3. Go back to the edit cart step
4. GetOrCreateCurrentOrder() now returns a new and very empty order and my old order i gone.
Should I not use GetOrCreateCurrentOrder() anywhere? Unless GetCurrentOrder() returns null?
My goal is to keep my old order until it has bee finalized.
Note: I use both GetCurrentOrder() and GetOrCreateCurrentOrder() with a customerReference, to make sure two customers can share the same cart. In this case there's only me though.
BeginPaymentForm will initialize the transaction and assigned the orde rnumber but it shouldn't yet move the order out of the current order state so GetOrCreateCurrentOrder should return the current order still.
If you call GetCurrentOrder does that actually return the current order? or does it return null?
Also, if you call GetCurrentFinalizedOrder can you confirm the order hasn't been moved to that state somehow?
GetCurrentOrder returns the correct old order. Only GetOrCreateCurrentOrder discards the old order and makes a new one.
I can confirm that GetCurrentFinalizedOrder returns null.
Ok, so we know the current order is still in the current order state.
So, the GetOrCreateCurrentOrder firstly calls GetCurrentOrder so this should be doing the same thing, but on top of fetching the current order it also does a customer reference check. Are you doing anything with customer reference assignments?
Generally speaking, if you are not logged in, then it should just return the found order, if you are logged in though if the order has no customer reference it will become assigned to you and then returned, or if the order has a customer reference and it's not your customer reference then this should be the only time a new order is created.
Ok, so that’s likely the issue. If you don’t pass the customer reference to the GetOrCreateOrder method then the current logged in members ID will be used so if you are using some other customer reference, you’ll need to pass it to the method.
But are you passing that to your Get OrCreateCurrentOrder method call? There is an overload that takes it as a second argument. If you don’t pass it, the current logged in member ID is used and you mentioned these aren’t the same thing which should be the only scenario under which the current order isn’t returned (ie, Vendr thinks the current order doesn’t belong to you)
Yes. GetCurrentOrder has the same customer reference number, but returns the correct order. So I guess I could just use that method, but it seems like I shouldn't have to. GetOrCreateOrder should do the same.
Indeed. I can't for the life of me figure out why it's wrong though. Maybe if you replicate the code Vendr is using you can see where it's going wrong?
This is basically the code that is the start of the GetOrCreateCurrentOrder method and is responsible for returning the current order
var currentOrder = GetCurrentOrder(storeId, customerReference);
if (currentOrder != null)
{
if (string.IsNullOrWhiteSpace(customerReference))
{
// If no customer reference is supplied, then just return whatever the current order is
// this could be an order assigned to a customer or not.
return currentOrder;
}
else if (!string.IsNullOrWhiteSpace(customerReference) && string.IsNullOrWhiteSpace(currentOrder.CustomerInfo.CustomerReference))
{
// Order has no customer ref, but one was supplied so assign the order to the customer now and return it
using (var uow = _uowProvider.Create())
{
var writableOrder = currentOrder.AsWritable(uow)
.AssignToCustomer(customerReference);
_orderService.SaveOrder(writableOrder);
currentOrder = writableOrder;
uow.Complete();
}
return currentOrder;
}
else if (!string.IsNullOrWhiteSpace(customerReference) && currentOrder.CustomerInfo.CustomerReference == customerReference)
{
// Customer ref was supplied and it matches the order customer ref, so we can continue to use this order
return currentOrder;
}
// The only other option left is that as customer ref was passed but it doesn't match the
// customer ref on the current order, so we won't return it and will fall through to create a new order
}
If you can let me know what that hits in your code, I believe in your case it should hit the last else if but it sounds like it's not matching any of those statements for some reason.
Thanks for the code bit. It's my customer reference that is not the same as currentOrder.CustomerInfo.CustomerReference.
So the code runs past your code example. So what happens if the line in the screenshot is false? And why is currentOrder.CustomerInfo.CustomerReference not the same as mine? Is it my responsibility to set it at some point? My expectation is that it should be set right in the beginning when I created the order with my own customer reference.
Bingo! Ok, so it looks like it's a problem in BeginPaymentForm as this is when it also attempts to assign the customer reference, but it's basically saying if it's not the same than that coming from the membership helper then overrwrite it, which really it should be if the reference is set, don't bother attempting to set it again.
I'll see if I can push a v2 patch out with an update to this
UPDATE I've pushed the fix to our unstable feed at https://nuget.outfield.digital/unstable/vendr/v3/index.json if you install v2.4.1-beta0002 this should resolve the issue. If you are able to test and let me know this is the case I'll get a full patch release out.
Not sure if it's me that's a nuget n00b but I cannot seem to find your beta.
As you can see on my screenshot I have added your unstable nuget link to VS, but there's no beta's and the latest version is the 2.4.0 which I have already installed.
GetOrCreateCurrentOrder() after BeginPaymentForm()
Hi,
I'm having problems finding the correct way of doing this.
On my cart page I edit the quantity of the cart. I do this by GetOrCreateCurrentOrder().AsWritable() and then editing the order. The same goes for when I add new products to the cart.
My scenario is 1. Add product to cart 2. Go to the step in the Checkout with the BeginPaymentForm() 3. Go back to the edit cart step 4. GetOrCreateCurrentOrder() now returns a new and very empty order and my old order i gone.
Should I not use GetOrCreateCurrentOrder() anywhere? Unless GetCurrentOrder() returns null?
My goal is to keep my old order until it has bee finalized.
Note: I use both GetCurrentOrder() and GetOrCreateCurrentOrder() with a customerReference, to make sure two customers can share the same cart. In this case there's only me though.
/Rune
Hey Rune,
BeginPaymentForm
will initialize the transaction and assigned the orde rnumber but it shouldn't yet move the order out of the current order state soGetOrCreateCurrentOrder
should return the current order still.If you call
GetCurrentOrder
does that actually return the current order? or does it returnnull
?Also, if you call
GetCurrentFinalizedOrder
can you confirm the order hasn't been moved to that state somehow?GetCurrentOrder returns the correct old order. Only GetOrCreateCurrentOrder discards the old order and makes a new one. I can confirm that GetCurrentFinalizedOrder returns null.
Sounds like it could be a bug?
I can perform the problem on a v2.4
/Rune
Ok, so we know the current order is still in the current order state.
So, the
GetOrCreateCurrentOrder
firstly callsGetCurrentOrder
so this should be doing the same thing, but on top of fetching the current order it also does a customer reference check. Are you doing anything with customer reference assignments?Generally speaking, if you are not logged in, then it should just return the found order, if you are logged in though if the order has no customer reference it will become assigned to you and then returned, or if the order has a customer reference and it's not your customer reference then this should be the only time a new order is created.
I am sending my own string as customer reference. So yes, that might be a source of error. Using the same customer reference for all my tests.
I am logged in with a member at the same time, but I create my own customer reference.
Ok, so that’s likely the issue. If you don’t pass the customer reference to the GetOrCreateOrder method then the current logged in members ID will be used so if you are using some other customer reference, you’ll need to pass it to the method.
No. I'm using the same customer reference for all calls throughout. There's never not a customer reference.
But are you passing that to your Get OrCreateCurrentOrder method call? There is an overload that takes it as a second argument. If you don’t pass it, the current logged in member ID is used and you mentioned these aren’t the same thing which should be the only scenario under which the current order isn’t returned (ie, Vendr thinks the current order doesn’t belong to you)
Oups. Just marked you previous comment as the solution. Miss click.
I send the customer reference to all the methods that allows it. And that part works very fine.
Ok, and so passing that reference still results in a new order being created?
Correct. That has been the case all along.
Gotcha. Ok, let me take another look.
It would seem it’s around this comparison that the issue lies.
It's not a casing issue is it? We do a simple
==
comparison for the current orders CustomerReference so I think that would be case sensitive.If you call GetCurrentOrder and compare it's customer reference with the one you passed in, does it say it matches?
Nope. The current customer reference I'm using is a telephone number with only 8 digits and nothing else.
Edit: As a string of cause
And regarding my second question, if you retrieve the order via GetCurrentOrder does it have a customer reference set to that same value?
Yes. GetCurrentOrder has the same customer reference number, but returns the correct order. So I guess I could just use that method, but it seems like I shouldn't have to. GetOrCreateOrder should do the same.
Indeed. I can't for the life of me figure out why it's wrong though. Maybe if you replicate the code Vendr is using you can see where it's going wrong?
This is basically the code that is the start of the
GetOrCreateCurrentOrder
method and is responsible for returning the current orderIf you can let me know what that hits in your code, I believe in your case it should hit the last
else if
but it sounds like it's not matching any of those statements for some reason.Thanks for the code bit. It's my customer reference that is not the same as currentOrder.CustomerInfo.CustomerReference.
So the code runs past your code example. So what happens if the line in the screenshot is false? And why is currentOrder.CustomerInfo.CustomerReference not the same as mine? Is it my responsibility to set it at some point? My expectation is that it should be set right in the beginning when I created the order with my own customer reference.
/Rune
Bingo! Ok, so it looks like it's a problem in
BeginPaymentForm
as this is when it also attempts to assign the customer reference, but it's basically saying if it's not the same than that coming from the membership helper then overrwrite it, which really it should be if the reference is set, don't bother attempting to set it again.I'll see if I can push a v2 patch out with an update to this
UPDATE I've pushed the fix to our unstable feed at https://nuget.outfield.digital/unstable/vendr/v3/index.json if you install v2.4.1-beta0002 this should resolve the issue. If you are able to test and let me know this is the case I'll get a full patch release out.
Not sure if it's me that's a nuget n00b but I cannot seem to find your beta. As you can see on my screenshot I have added your unstable nuget link to VS, but there's no beta's and the latest version is the 2.4.0 which I have already installed.
Am I missing something?
There should also be a checkbox when searching for the packages for "Include Prerelease" which needs to be checked
*Facepalm I knew that.
Thanks
It works 👍
I cannot replicate the bug with v2.4.1-beta0002. I can also complete a payment, so the payment form still works as well. Good work.
Will you be making a release of 2.4.1?
/Rune
Yea, I’ll see if I can do that this afternoon 👍🏻
2.4.1 should be out now. I won't be making a big public announcement though, but it's there for you anyway :D
I will test it asap. There's a small issue with my staging environment, so I cannot test it there yet. But my local version works as expected.
Good work as always 👍
FYI we just released the v2.4.1 version to the live site and all seems good.
is working on a reply...