Discount Names in Orders

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
How difficult would it be to change the code so that Discount "name" is included in the exported Excel file for orders?

I assume the discount name could be written to the oders table at te time the order was placed. Currely the only discount item in the orders excel export is the discount amount.

Any ideas about this would be appreciated.
12 years ago
JoeReynolds wrote:
How difficult would it be to change the code so that Discount "name" is included in the exported Excel file for orders?

I assume the discount name could be written to the oders table at te time the order was placed. Currely the only discount item in the orders excel export is the discount amount.

Any ideas about this would be appreciated.



You would have to modify the nop.services.exportimport.exportmanager  (ExportManager.cs) and recompile.

check to see if adding a column header and
                        worksheet.Cells[row, col].Value = order.DiscountUsageHistory;
                        col++;
does what you're looking for; i'm not sure what the discountusagehistory contains;

I don't think adding a column to the table would be needed because
table dbo.DiscountUsageHistory contains the DiscountId and OrderId, and dbo.Discount has the name.

i believe orders can have multiple discounts applied, keep this inmind for whatever you're trying to do.
12 years ago
dfwitdepartment wrote:


You would have to modify the nop.services.exportimport.exportmanager  (ExportManager.cs) and recompile.

check to see if adding a column header and
                        worksheet.Cells[row, col].Value = order.DiscountUsageHistory;
                        col++;
does what you're looking for; i'm not sure what the discountusagehistory contains;

I don't think adding a column to the table would be needed because
table dbo.DiscountUsageHistory contains the DiscountId and OrderId, and dbo.Discount has the name.

i believe orders can have multiple discounts applied, keep this inmind for whatever you're trying to do.


Thasnks for the response. I modified the ExportManager.cs file so that I have a new column named DiscountName and can populate it with the OrderId. However, I see no way to get at the associated Discount table Name via the current code.

Any ideas?
12 years ago
Here's where I'm at:


worksheet.Cells[row, col].Value = order.DiscountUsageHistory.FirstOrDefault().DiscountId;


The above, of course, only grabs and displays the DiscountId of the first entry.

Short of writing some new code to do selects and joining to obtain the correct value of the cell, is there any way to change the above code to get the DiscountId from order.DiscountUsageHistory using WHERE to grab the entry where the order.Id = the DiscountUsageHistory OrderId?

If so, this would at least get me the DiscountId associated with an order (I only allow one discount per order) for a column to be dispalyed in the Export to Excel file.
12 years ago
well, there's gotta be a simplier way to do this but

add to the beginning of ExportManager.cs

using Nop.Core.Domain.Discounts;
using Nop.Services.Discounts;


and then where it begins the class add

    public partial class ExportManager : IExportManager
    {
        private readonly IDiscountService _discountService;
        private readonly ICategoryService _categoryService;



now the following should get you the name of discount id 1

_discountService.GetDiscountById(1).Name.ToString();


of course i haven't tested this, but i hope this helps get you closer.
12 years ago
Thanks Jacob. Big help but I'm not home yet. :)

Here's some code I'me experimenting with.

Notice I can use your suggestion to get the discount name but only if I manually apply a discount id to it.

The code also shows how I am trying to take the discounthistory list and and grab the discount id so I have that to use when getting the discount name per your suggestion. In order to use your suggestion for grabbing the discount name, I first have to know the discount id stored in discountusagehistory for the order.

The code below (the try) does not return a discount id.



string DiscountName = "Begin";
                    foreach (var order in orders)
                    {
                        int col = 1;

                        
                                if (Convert.ToString(order.OrderSubTotalDiscountExclTax) == "0.0000")
                                {
                                    worksheet.Cells[row, col].Value = "00000";
                                    col++;
                                }
                                else
                                {


                                    try
                                    {
                                        var uh = _discountUsageHistory.ToList();
                                        var results = from r in uh
                                                      where r.OrderId == order.Id
                                                      select r.DiscountId;
                                        int orderdiscountid = Convert.ToInt32(results);

   <fails> DiscountName = _discountService.GetDiscountById(orderdiscountid).Name.ToString();
                                        if (string.IsNullOrEmpty(DiscountName))
                                        {
                                            DiscountName = "None";
                                        }
                                    }
                                    catch
                                    {
                                      
                                    }


<works with manual id of 1>DiscountName = _discountService.GetDiscountById(1).Name.ToString();    
                                        
                                      

       worksheet.Cells[row, col].Value = DiscountName;
            col++;
12 years ago
i thought
order.DiscountUsageHistory.First().DiscountId
would give you the first discount id in the list, is that not the case?

What happens when you try:

DiscountName = _discountService.GetDiscountById(order.DiscountUsageHistory.First().DiscountId).Name.ToString();


What error is it giving if any?
12 years ago
Thanks again. The following code produces no errors.

However, the line you provided of:

DiscountName = _discountService.GetDiscountById(order.DiscountUsageHistory.First().DiscountId).Name.ToString();

looks to me like it will always return only the first entry in DiscountUsage History. Would it not need some "Where" selection to select the row in DiscountUsageHistory WHERE OrderId in DiscountUsageHistory = order.ID???????

Or am I missing something?






               string DiscountName = "Begin";
                    foreach (var order in orders)
                    {
                        int col = 1;
                      
                                if (Convert.ToString(order.OrderSubTotalDiscountExclTax) == "0.0000")
                                {
                                    worksheet.Cells[row, col].Value = "None";
                                    col++;
                                }
                                else
                                {
                                     DiscountName = _discountService.GetDiscountById(order.DiscountUsageHistory.First().DiscountId).Name.ToString();
                                      
       worksheet.Cells[row, col].Value = DiscountName;
            col++;
                  
                   }
        
12 years ago
order.DiscountUsageHistory will already be filtered to just the ones that apply to that Order

Also, I think you will want FirstOrDefault().  Just using First() will throw an exception if you try to access the name and there are no Discounts in the list.
12 years ago
Thanks Andy.

Another question. Since I want to use some Discout codes purely for tracking purposes, if I create a Discount with a fixed discount amount of

0.0001

can I assume the customer will see a discount of zero but the order entry will show a discount of 0.0001 and thus trigger my new code to display a Discount Name. I'm only applying a Discount name in the xl order export file if the order discount is not 0.0000.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.