Developer forum

Forum » Development » Editing Carts from the backend

Editing Carts from the backend

Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi,

 

We have a customer who's been very insistive on editing a cart from the backend. I know DW is not supporting this particularly, but I am hopeful for some loophole or workaround to move past this. I am using 9.7.5

 

Process

  • User populates a cart but for some reasons calls Customer Service for help
  • Customer Service locates the Cart in the backend and changes quantities (there are other tasks, but I am focusing on this for now)
  • We can save the cart and the backend updates the order and calculations accordingly
    (I also checked the DB to ensure it was not only the object in memory/cache being used to display the values)
  • We go to the frontend and everything is reverted back

 

Attempts

  • I tried being logged out while editing the cart
  • I tried just refreshing the cart after the changes
  • I removed any browser cache between steps

 

Is this something we'll be able to do in 9.8 OR (if not) is there some way I can keep the changes that were committed to the DB?

 

Best Regards,

Nuno Aguiar


Replies

 
Nicolai Pedersen
Reply

We have made quite a few changes to how carts are handled in DW9.8 - it supports cart management in the frontend (using impersonation).

But this scenario I do not know if is supported. It seems like a missing cache reset when using order edit in the backend.

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Nicolai,

 

I would agree with a missing cache reset and I'd be ok custom developing something and letting our customer know they have to click something after saving the order.

 

That said, how can I clear the cache? I don't see any public method do to that, only ClearCachedPrices(). Is there another way we can do that?

 

Nuno

 
Nicolai Pedersen
Reply

ProductService.ClearProductsCache()

 
Nicolai Pedersen
Reply

Forget that - that is totally wrong!!

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Nicolai,

 

Sure, consider it forgotten :)

 

I dug into the source code a bit more and found 

OrderService().RemoveOrderCache()

 

I will explore that and see what I can do

 

Thanks anyway.

Nuno Aguiar

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Nicolai,

 

Can you tell if I am doing anything wrong here? I tried a few different things and ended up looking into how DW sets the Cart cachekey when a user logs in and mimiced the logic, but still with no luck. When the order is loaded from the frontend, it overides the backend / updates the database back.

 

using Dynamicweb.Ecommerce.Notifications;
using Dynamicweb.Ecommerce.Orders;
using Dynamicweb.Extensibility.Notifications;

namespace Dna.Patch.EditCarts
{
    [Subscribe(Ecommerce.Order.AfterSave)]
    public class OnAfterSaveOrder : NotificationSubscriber
    {
        public override void OnNotify(string notification, NotificationArgs args)
        {
            
            if (!(args is Ecommerce.Order.AfterSaveArgs afterSaveArgs)
                || !Dynamicweb.Environment.ExecutingContext.IsBackEnd())
            {
                return;
            }
            
            var orderService = new OrderService();
            var cacheKey = afterSaveArgs.Order.Id;

            if (!string.IsNullOrEmpty(afterSaveArgs.Order.OrderContextId)) cacheKey += ".'" + afterSaveArgs.Order.OrderContextId + "'";
            
            orderService.RemoveOrderCache(cacheKey);
        }
    }
}
 
Martin Vang
Martin Vang
Reply

Hi Nuno,

We had multiple places where the carts were cached in 9.7 and below. This has changed for 9.8.

Can you please try again and see if the problem also exists on 9.8? (I know... day after release and all... but still)

And please dont start your test with ContextCarts. I want to hear if things work for normal carts first ;)

BR

Martin

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Martin,

 

Sure thing. I'll ask for the upgrade on the dev environment and let you know how it works:

  • with and without any custom development
  • with and without cart context

 

Best Regards,

Nuno Aguiar

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Martin,

 

We upgraded it but now we have an error. I tried to rerun the updates and that fixed the error in the frontend, but NOT in the backend going to an order detail page

https://www.screencast.com/t/qTuOxfVcBxW1

 

Can you help me overcome this?

Best Regards,

Nuno Aguiar

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

You need to rerun your updates. Your database schema is out of whack and needs new columns. Resetting and rerunning the updates should fix that.

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Imar,

 

I had done that and it fixed the frontend (Order history), then the backend got the same error. Reran the updates again but without success.

 

Any other ideas? I can potentially dig through the source code, but I was hoping had a magnet to find the right line, because I am all out so finding that needle is harder for me cheeky

 

Best Regards,

Nuno

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

The package that adds the column looks like this:

    <package version="2140" date="14-10-2019">
        <database file="Ecom.mdb">
            <EcomCustomerFavoriteLists>
                <sql conditional="SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='EcomOrders' AND COLUMN_NAME='OrderDisplayName'">
                    ALTER TABLE [EcomOrders] ADD [OrderDisplayName] NVARCHAR(255) NULL;
                    ALTER TABLE [EcomOrders] ADD [OrderDiscountPercentage] [FLOAT] NOT NULL DEFAULT 0;
                    ALTER TABLE [EcomOrders] ADD [OrderDiscountAmount] [FLOAT] NOT NULL DEFAULT 0;
                    ALTER TABLE [EcomOrderLines] ADD [OrderLineDiscountPercentage] [FLOAT] NOT NULL DEFAULT 0;
                    ALTER TABLE [EcomOrderLines] ADD [OrderLineUnitPriceBeforeDiscount] [FLOAT] NOT NULL DEFAULT 0;
                </sql>
            </EcomCustomerFavoriteLists>
        </database>
    </package>

 

Do you see anything in the update log? Check your database for these columns and either rerun the updates again until it works, or add them manuallly to your database (that last option isn't recommended really, as it's followed by a whole bunch of other packages)

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Imar,

 

Tricky little thing. The updates were breaking and did not report anything much about it. I ended up confirming the EcomOrders table had been updated but the EcomOrderlines one did not. I ran the query you provided (thanks), and now I am past the errors in the backend.

 

 

Martin,

I started doing some tests and they were successful. I did not even had to logout in the frontend which is very nice indeed. I did not do all of the tests with adding/removing products and discounts, but I am confident right now.

 

Thank you both,

Nuno Aguiar

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Martin,

 

Just to confirm that I performed a few more tests and this works for the most part:

  • I can do any CRUD operations on Products
     
  • When decreasing unitprice and/or quantities, I need to notice if the discounts don't cause the order to have a negative amount
    In which case I can remove them at will, because once the order is reloaded in the frontend it will "fix it"
     
  • Whenever I try to add a discount (orderline or order) it renders an error and the frontend user "loses it"
    This means I cannot add any discounts in the backend

 

I am reporting this, not so it gets fixed, but because it might also affect some other desired features DW has/plans.

 

Thank you for your help,

Nuno Aguiar

 

You must be logged in to post in the forum