close ad
Databridge V2 with MySQLi support IS Now Available!
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

How to include product specific discounts to my current cart..??

Thread began 7/22/2011 3:38 am by angelleye | Last modified 7/22/2011 8:28 pm by angelleye | 1107 views | 6 replies |

angelleye

How to include product specific discounts to my current cart..??

I've got some discounts already setup and working great in my eCart (you guys helped me with that awhile back, actually).

My database stores the discount code data and I have session vars for DiscountCode, DiscountType, and DiscountAmount.

Then I have 2 separate discount rules setup in eCart: one for the flat rate type and another for percentage based type. The flat rate rule just uses the flat rate amount set in the DiscountAmount session. The percentage rule takes the DiscountAmount times the cart's subtotal.

I'd like to add the ability to include discount codes in my system that only apply to certain items. For example, if we're going to do a special on one product we don't want people getting 10% off everything in their cart...just that one line item. We have plenty of codes that do pertain to an entire card, though, so I need both to function.

This probably isn't the best way to do this, but I added a single field into my discount codes db table called product_ids. I'm simply adding the ID's of any products I want this coupon to work for separated by commas. Then I have those available in my "apply discounts" script so I can loop through them and handle it accordingly.

I just don't want to get too far with that and figure out I should do it an entirely different way. Should I go with a separate table that just stores related products to discount codes, or can I work with what I'm doing here?

Either way, some advice on the best plan of attack would be greatly appreciated. Thanks!!!

Sign in to reply to this post

Ray BorduinWebAssist

Comma separated will work, but it depends on the end user not having any typos and entering valid data which could be hard to validate.

A separate table might be easier because you could use the manage relational table and multiple insert and multiple update server behaviors from dataassist to help, but because of the complexity those are no walk in the park for most people, and would be extra work and complexity from the developer perspective, although would likely result in a more user friendly result for the end user.

The single table would depend on returning the comma separated field in a recordset, then exploding that into an array. The separate table technique you would loop through the recordset and probably also want to store the data in an array.

Ultimately you would have to have a nested loop that loops through both the array of supported products and compares it to each of the products in the cart to set the discount. Both techniques will involve hand coding and a strong knowledge of php to pull off effectively. Both have pros and cons and ultimately you should use whichever you are most comfortable with and whichever makes the most sense to you.

Sign in to reply to this post

angelleye

The looping and general PHP coding is not a problem for me. It's just the logic for setting up the rules in eCart that I always get lost with.

I'm not really sure how the end user could typo anything...they're just selecting which products belong to a discount code as they create it. Nothing really to mis-type.

So then in my "apply discount" script I've already got the exploded value for product_ids in an array and I can loop through those no problem. What I don't really get is how to apply that to the shopping cart items.

Right now my discounts just work on the entire order. Even after looping through each of the valid product IDs for a given code I don't see how to get it to apply to only that single line item on the cart. The best I can seem to get to is to either apply the discount or not based on whether an item is in the cart, but then it still applies to the entire order instead of just that one line item.

Sign in to reply to this post

Ray BorduinWebAssist

You could probably add a column to the cart for individualDiscount and set it to zero when you add anything to the cart. Then you could add a discount rule to the cart that simply takes the total of that column and applies it as a discount.

Now your loop through the cart can update that column and the rest would work automatically.

Sign in to reply to this post

angelleye

I think I got it worked out. I went a little different route, but it seems to be working so far.

As I loop through the shopping cart I'm setting a session var for DiscountProductID when the ID of the current cart product matches the ID associated with the discount code. Then I created new eCart discount rules for Product Specific Flat and Product Specific Percentage that are just like my regular discount code rules except that they look for the DiscountProductID session var in order to get triggered. My percentage rule here, though, was based on an items' column value where I used the DiscountProductID session var to pull the line item total for that ID accordingly. I learned here that I needed to use ".$_SESSION['DiscountProductID']." as oppose to just $_SESSION['DiscountProductID'] or it created syntax errors in eCart_PHP.php.

Then I adjusted my original rules so that they included a custom expression of !isset($_SESSION['DiscountProductID']) so they wouldn't get triggered if the code had flagged a product ID.

That seems to have done it! Thanks for your help. I may come back if I find bugs in what I've done. :)

Sign in to reply to this post

angelleye

Well, that didn't take long. I just found a small bug in the way I did it.

Say I have a discount code that's set for 10% 1 specific product. If I don't have that product in my cart when I use that code it tells me the code isn't valid for my current cart like I want it to. If I do have that item in the cart then the code works fine and calculates against only that line item, which is wanted.

However, if I remove the item that belonged to the discount code after the discount has already been applied it's leaving the discount on the order.

So now I'm stumped again. I hadn't seen your response until already went down the other road so I'll look back and maybe try that route.

Sign in to reply to this post

angelleye

Looks like another small adjustment fixed that. I added another check in my discount rules for whether or not that particular was on the cart so now it removes it when the item is removed.

Whew!

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...