How can I execute plugin method when order placed

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
M.Ang wrote:

Ah, i actually thought that was the problem, i placed your plugin folder in the src/Plugins folder and after build the result never actually shows up (due of the wrong output path). Maybe the version you gave to me was an old one?

I'm not by my dev machine either, i'll report back later


I'll send across the latestest version of the plugin tomorrow morning with install instructions and summary of the issue.

Apart from the EventHandler issue the rest is working great. It could even be released as it without the automatic adding of favorites based on orders placed.
11 years ago
Did you get the latest version by email?
11 years ago
Yes, and i've carefully debugged through the instance creation process.

With the old version, where the constructor parameters for the Plugin class were concrete classes instead of interfaces (not registered in Autofac) the error was happening here:


        public object ResolveUnregistered(Type type)
        {
            var constructors = type.GetConstructors();
            foreach (var constructor in constructors)
            {
                try
                {
                    var parameters = constructor.GetParameters();
                    var parameterInstances = new List<object>();
                    foreach (var parameter in parameters)
                    {
                        var service = Resolve(parameter.ParameterType);
                        if (service == null) throw new NopException("Unkown dependency");
                        parameterInstances.Add(service);
                    }
                    return Activator.CreateInstance(type, parameterInstances.ToArray());
                }
                catch (NopException)
                {

                }
            }
            throw new NopException("No contructor was found that had all the dependencies satisfied.");
        }

As you can see, he walks through the constructor parameters and try to resolve them, then asks for the Activator to create an instance at runtime providing the constructor parameters.


Now all this proceeds well, so the thing get past this line:

            var plugin = pluginDescriptor.Instance() as FavoriteCategoriesPlugin;



Instead, the plugin code hangs there:


            if (plugin.AddFavoriteCategories(order.Id))


with a NullReferenceException. Taking a peek at the code from AddFavoriteCategories it is pretty obvious why :)



        public bool AddFavoriteCategories(int orderId)
        {
            Order order = _orderService.GetOrderById(orderId);
            IList<Order> orders = null;
            orders.Add(order);

            return _favoriteCategoriesService.AddFavoriteCategories(orders);
        }


You have to initialize the list with new List<order>() or else it will cast the Exception.

If you debug from this you should be fine!

Let us know how it's going
11 years ago
Thanks for all your help. Works like a charm. See it in action.

Add a favorite at the category level
Sx235W Ink

Add a favorite at the product level
SX235W Ink Cartridges

login using nop test account
email: [email protected]
password: noptest

See current favorites and remove them
My Account

Every time an order is placed those categories get added to their favorites too.

Have some work to do on "Settings" before release. Should be next week.

Next step is to have a customised homepage based on favorites and send customised emails based on favorites.

P.S Do you have a printer? I'll send you some free ink as a thank you

Also, if anyone has time to test this plugin, please let me know here and I'll send you the source.

Thanks

Darren
11 years ago
ahaha very kind from you, but since i let the last ink set wear out (just imagine how frequently i print stuff) i decided to dispose the printer :)

If i can give an advice, i'd make the "add to fav" buttons use ajax (so you don't "waste" a page reload), to make the feature more "in tone" with the 2.6 style ("add to cart" button, view of the shopping cart item on mouseover...)

consider it for the 2.0 :)
11 years ago
M.Ang wrote:
ahaha very kind from you, but since i let the last ink set wear out (just imagine how frequently i print stuff) i decided to dispose the printer :)

If i can give an advice, i'd make the "add to fav" buttons use ajax (so you don't "waste" a page reload), to make the feature more "in tone" with the 2.6 style ("add to cart" button, view of the shopping cart item on mouseover...)

consider it for the 2.0 :)


"add to fav" does actually use ajax at the minute. I just couldn't work out how to update the button text so I hacked it and did a page refresh.

Same with "remove" on the account info page. Uses ajax to fire the method but I coulnd't work out how to remove the relevant row from the table so I just did a page refresh :-(

Maybe someone else will contribute this, or I'll save it for 2.0 as you say.

Chao
11 years ago
Good job, guys!

wunpac wrote:

"add to fav" does actually use ajax at the minute. I just couldn't work out how to update the button text so I hacked it and did a page refresh.

How a look at how it's done for the forums "watch" button. \src\Presentation\Nop.Web\Views\Boards\Forum.cshtml file
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.