External Download URL for each OrderItem

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
spakinz
Ensign (ENS)
Ensign (ENS)
Posts: 4
Joined: Wed May 05, 2010 5:53 am

External Download URL for each OrderItem

Post by spakinz » Wed May 05, 2010 6:28 am

Hello,

First of all, I've already read these posts:

viewtopic.php?f=45&t=7300&p=30533&hilit ... url#p30533
viewtopic.php?f=42&t=10722&p=45835&hili ... url#p45835

They are related but not exactly what I need.

The requirements are:

- When the order is placed I need to call an external WebService to get a unique download url for each digital good in the order. Where is the better place to make this request? The download server takes care about the security.

- I need a place to store the download url for each OrderItemDigitalGood. I've seen that neither, the OrderItem nor the OrderItemDigitalGood have a CustomField Property. What would be the better method to accomplish this? I've thought to create a new table with a FK to the OrderItem table but I don't like that idea very much.

- Finally I need to modify the notification email and the order page to replace the download url of each digital good in the order. I don't need help with this (I guess).

Thanks in advance.

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: External Download URL for each OrderItem

Post by mazhar » Thu May 06, 2010 9:51 am

This could be a little bit trick, I am not sure but you can look further into following workaround
- When the order is placed I need to call an external WebService to get a unique download url for each digital good in the order. Where is the better place to make this request? The download server takes care about the security.
I guess you need to handle it OnePageCheckout.ascx.cs files CheckingOut method. You can access newly placed order in this method by something like
Order order = OrderDataSource.Load(e.OrderId,false);
if(order != null)
{
//YOU CODE TO GENERTATE LINKS FROM EXTERNAL SERVER
}
- I need a place to store the download url for each OrderItemDigitalGood. I've seen that neither, the OrderItem nor the OrderItemDigitalGood have a CustomField Property. What would be the better method to accomplish this? I've thought to create a new table with a FK to the OrderItem table but I don't like that idea very much.
OrderItems supports custom fields. I think this is available from 7.0.4. You can have a look at your ac_OrderItems table and if there is a customfields column in it then no need to create any table every thing is already done. Its based on dictionary like structure, so you can set those custom fields just by doing something like below

Code: Select all

foreach(OrderItem item in order.Items)
{
item.CustomFields.Add("downloadurl", "your custom url here");
}
order.Save();
Then you will need to update your receipt or myorderpage where these downloads are available. You will update current download location and to make use of link from this custom field.
- Finally I need to modify the notification email and the order page to replace the download url of each digital good in the order. I don't need help with this (I guess).
Well with the approach I described above you will not be able to use automatic Email feature. Reason is that Email will be triggered as soon as order gets created just before the CheckedOut method gets called so customer will receive invalid links. You need to remove order placed trigger from order confirmation Email. Then in CheckedOut method once you are done with generation of custom download links then you can load and send order confirmation Email template manually through code. Have a look at following thread for manual Email help viewtopic.php?f=42&t=8571

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: External Download URL for each OrderItem

Post by mazhar » Thu May 06, 2010 9:59 am

- Finally I need to modify the notification email and the order page to replace the download url of each digital good in the order. I don't need help with this (I guess).
Well I believe that there could be one more workaround as well that may even save you from manual Emails but that would require you to generate link before creation of order if it suits you. In this case if you look at ConLib/OnePageCheckout.ascx.cs file there is a method called CheckingOut. This called just before executing checkout process, at this point customer can't change basket items. In this method you don't have any order but you can adjust basket. BasketItem do support custom fields. Actually if you have something in custom field of a basket item then it will turn into custom field of corresponding order item after checkout. So trick is that in checking out you can generate download urls for basket items and then save them in their custom fields. Then upon checkout completion these custom fields will be copied over to order items of order. Then all you need is to update your confirmation email and make sure to only output download urls of order items if order is marked paid or what ever is suitable order status against which you can allow downloads.

spakinz
Ensign (ENS)
Ensign (ENS)
Posts: 4
Joined: Wed May 05, 2010 5:53 am

Re: External Download URL for each OrderItem

Post by spakinz » Thu May 06, 2010 10:23 am

Thank you so much Mazhar. This is exactly what I needed!!

The solution sounds very good. I guess I won't have any problems to implement it.

Thanks agains Mazhar.
Best Regards

spakinz
Ensign (ENS)
Ensign (ENS)
Posts: 4
Joined: Wed May 05, 2010 5:53 am

Re: External Download URL for each OrderItem

Post by spakinz » Wed Sep 08, 2010 11:50 am

Hello Mazhar,

You were right when you said this:
Well with the approach I described above you will not be able to use automatic Email feature. Reason is that Email will be triggered as soon as order gets created just before the CheckedOut method gets called so customer will receive invalid links. You need to remove order placed trigger from order confirmation Email. Then in CheckedOut method once you are done with generation of custom download links then you can load and send order confirmation Email template manually through code. Have a look at following thread for manual Email help viewtopic.php?f=42&t=8571
I created the download links and stored them in the OrderItem CustomFields but then, I cannot make the email template to work as I want. I want to include a link to a Custom Order Download Page only if the order contains downloadable items. Downloadable items are, in this case, OrderItems that their CustomFields property contains the key "DownloadURL".

I tried to make an extension method on the Order class named HasDownloadableItems just to do something like

Code: Select all

#if ($order.HasDownloadableItems())
...
but it is not being called at all.

So finally I did something like this:

Code: Select all

#set ($downloadItemCounter = 0)
#foreach ($orderItem in $order.Items)
#each
#if (($orderItem.OrderItemType == "Product") && ($orderItem.CustomFields.ContainsKey("DownloadUrl")))
#set ($downloadItemCounter = $downloadItemCounter + 1)
#end
#end

#if ($downloadItemCounter > 0)
<p>Your order contains downloadable items. Click
<a href="${store.StoreUrl}Members/MyOrderDownloads.aspx?OrderId=$order.OrderId">here</a> to go to the download page.</p>
#end
That doesn't work either. Then I remembered what you said and I removed the trigger that sends the email and added the code just after the links are generated:

Code: Select all

            //SEND CUSTOMER EMAIL
            EmailTemplateCollection emailTemplates = EmailTemplateDataSource.LoadForCriteria(" Name = 'Customer Order Notification' ");
            if (emailTemplates.Count > 0)
            {
                Order order = OrderDataSource.Load(response.OrderId);

                emailTemplates[0].Parameters.Add("store", Token.Instance.Store);
                emailTemplates[0].Parameters.Add("order", order);
                emailTemplates[0].Parameters.Add("payments", order.Payments);
                emailTemplates[0].Parameters.Add("customer", Token.Instance.User);
                emailTemplates[0].ToAddress = Token.Instance.User.Email;
                emailTemplates[0].Send();
            }
This sends the email but doesn't display the message.

What am I doing wrong?

Thanks in advance.
Ruben Lopez

spakinz
Ensign (ENS)
Ensign (ENS)
Posts: 4
Joined: Wed May 05, 2010 5:53 am

Re: External Download URL for each OrderItem

Post by spakinz » Thu Sep 09, 2010 8:30 am

I've found how to solve it (very easy):

Code: Select all

emailTemplates[0].Parameters.Add("hasDownloadableItems", order.HasDownloadableItems()); // HasDownloadableItems is an extension method
and then, in the template:

Code: Select all

#if $hasDownloadableItems
display message
#end

Post Reply