Developer forum

Forum » Development » Filter by Discount?

Filter by Discount?

Dimitrij Jazel
Reply

Hi Guys,

Customer aproached with a request to have a possibility to filter by Discount.

 

Simmilar like you filter by product field.  Product fields is already defined in among new field sources in the Index fields.

But in my case, if I want to filter by something, I need first to index it. 

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

There is a DW tag called "HasDiscount" or something like that, that does return true or false if Product belongs to any discount. It would be great if I could Index that for each product.

I also tried experimenting with Source, but that was not much success.

 

Anyone has any tips on that?


Replies

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply
This post has been marked as an answer

Hi Dmitrij,

 

You could do it using an IndexBuilderExtender and add your own logic to each document in Lucene.

 

For this particular scenario it makes sense to look into the Discounts and check if there are any associated with it (either by direct reference our to a group the product belongs to), but with Order Discounts that's almost impossible to determine, because they might depending on the other settings (order amount, country,...).

 

Hope this helps,

Nuno Aguiar

Votes for this answer: 1
 
Nicolai Pedersen
Reply

And keep in mind that discounts will not always apply - some only apply if you buy 2, or if the sun is shining etc.

 
Dimitrij Jazel
Reply

Hi Guys, thanks for the input!

That is exact same approach I also was considering, but I was wondering if I could come arround with something out of the box.

But no problem. That is even better. :-)

 

Cheers,

/Dmitrij

 
Dimitrij Jazel
Reply

Hi Guys,

 

Actually, I just came to actual implementation, and thought to myself, how do you Access the list of products, and check whether they have discounts.

While you are Macro implementation?

This is what I have so far (check the file attached).

More specifically I need to know what should I do in line #54.

 

Any tips would be great? I am actually not sure what to do.

Should I use some kind of SQL query to get the list of IDs, or can I call some API magic, and iterate through all products that have discounts?

But either way, I need a way to access products and check whether they have discount.

 

/Dmitrij

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Dmitrij,

 

This is what you need to be looking at:

https://github.com/dynamicweb/dwtc16/blob/master/CodingTheNewIndexCracks/tc16.local.dynamicweb.dk/CustomModules/NewIndex/MyIndexBuilderExtender.cs

 

This allows you do add aditional data to a lucene document, which is what you need to filter on. Within the ExtendDocument method you can either use the API to get Discounts associated with the product, or use SQL to query the Discount table and do some custom logic to determine if there are discounts or not.

 

Keep in mind this is easier said than done, since a discount may only apply if order total amount is more than €100 (just an example), and that's not a conclusion. Unless you want to add a flag called "Might-have-a-discount" :P

 

Hope that helps,

Nuno

 
Nicolai Pedersen
Reply

I have no pointers to help. If we could implement it, we would have. We could make a solution that sometimes and maybe worked in some scenarios, but we would get a million error reports, so we better not.

 
Dimitrij Jazel
Reply

Hi Nicolai, sorry for mega late response.

But still, when you are preparing products to be rendered in the front-end, Product.Discount tags are filled in.

I think the best "safe" way for me would be to do my own SQL calls, and see if I can find any discount products or not.

 

I tried checking DB, and see if I find any hints for discounts. But I was not able to find much.

So if a product has a discount. What SQL query should I use?

 

/Dmitrij

 

 
Nicolai Pedersen
Reply

You cannot SQL it out. It is not that simple...

Check out this thread: http://doc.dynamicweb.com/forum/development/development/filtering-on-prices-with-sales-discounts

 
Dimitrij Jazel
Reply

Ok, I think I found it :)

 
Dimitrij Jazel
Reply

Ok, so I made this Macro (please see file attached).

and my problem is that it is not getting invoked.

1) I am sure I am using correct Macro in the Catalog settings.

2) in the query settings, my macro was found. And I was able to select it. Here is how my Query looks right now: https://www.screencast.com/t/aeKrM7hV25

As you can see I added some logging methods, and no log appears in the System logs. So I assume this macro is not getting invoked!?

 

/Dmitrij

 

 
Nicolai Pedersen
Reply

You have to attach your code in zip for us to see it.

 
Dimitrij Jazel
Reply

Sure thing, here it goes :)

 
Nicolai Pedersen
Reply

Except it looks like it will perform like crap because you try to kill the DB, it looks right.

So you have to start debugging...

 
Dimitrij Jazel
Reply

Hello again Nicolai,

Not sure whos idea was to add an xml into a column in the first place, but whatever...

Happy you share your oppinion on potential performance killer, I agree with you regarding DB kill :) and you confirmed it.

But if you have some suggestions I would be glad to hear.

 

I give it a shot (with my limited HDD and Internet connection). Still strange why it wouldn't atleast add the log entry.

 

But I think I have a work-arround.

/Dmitrij

 
Dimitrij Jazel
Reply

Ok, another question.

How do I know to what language each EcomSalesDiscount entry belongs to?

/Dmitrij

 
Dimitrij Jazel
Reply

This is what I ended up using smiley

This does not seem to kill DB, and works quite well.

(see file attached)

 
Nicolai Pedersen
Reply

Nice. A few comments.

You can create a new instance of ProductDiscount and call LoadParametersFromXml with the data from the SQL you have. On that instance you have a property called ProductsAndGroups that contains the data from the selector

You can then use a ProductsAndGroupsHandler  to parse the content of that property.

And you REALLY need to cache the output of this macro for i.e. 5 mins or so.

Thanks for sharing, Nicolai

 

You must be logged in to post in the forum