Developer forum

Forum » Ecommerce - Standard features » The world of discounts - selectable gifts

The world of discounts - selectable gifts

Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi there,

I have another interesting discount problem. Here are some of the requirements:

  1. A user can add products to the cart using Express buy (with subscriptions).
  2. When they are creating their first subscription, they can choose one free product. The products they can choose from come from a group called Gifts.
  3. Products that are in the Gifts group are not only gifts; they can also be purchased normally.
  4. A product that is also in the Gifts group should not be added to the cart as a discount when ordered normally.

Here's the data set up

PRODUCTS
--------
PRODA
PRODB
PRODC
PRODD
PRODE
PRODF

GIFTS
-----
PRODA
PRODB

So, PRODA and PRODB can both be ordered normally and added as a gift when the user as selects it. Here's the user flow:

  1. I log in and go to Express Buy
  2. I see the products from the Products group above
  3. I add one or more of these to the cart and click Next to go the Gifts page
  4. The gifts page shows just the products from the Gifts group above
  5. I select one of them and click Add. This adds that product to the cart for free with a quantity of one.

A lot of this is already working with standard and custom functionality. For example, I have a standard discount that gives 100% off on the products in the Gifts group. I also have a custom DiscountExtender that marks the discount as not applicable when a user is not creating his first subscription.

Where I am running into problems is with these two items (at least for now)

  1. When I add a product to the cart in #3 above that is also in the Gifts group (i.e. PRODA or PRODB) it gets discounted immediately because of the 100% off discount. I don't want that; they can only be discounted when selected as a gift (and only once)
  2. When I add a product to the cart in #3 above that is also in the Gifts group (i.e. PRODA or PRODB), it gets discounted for the entire quantity that the user has chosen. What I want instead is just give one product away for free.

So, in other words, when I add PRODA with a quantity of 2 and then also select PRODA as the gift in the next step, the cart should contain three products and charge for two products and give one away for free.
If instead I add, say, PRODC with a quantity of 2 and select PRODA as a gift in the next step, my cart contains three products and should charge for two products PRODC and give one PRODA away for free.

In a real world analogy, I would have a basket of products on my counter and ask my customer to pick one if their purchase contained eligible products. Sounds simple, but turns out to be quite hard to set up in Dynamicweb.

Any ideas on how to implement this? Open for all implementations, including a custom (old) discount provider if I have to. I just need to understand what to implement and how.

Thanks!

Imar


Replies

 
Nicolai Pedersen
Reply

Sounds very simple....

You definitly not set that up using default behavior. You would need an extenstion and then store information on the order on the user choice of the possible products. And then add some. Alternatively you can consider creating a BOM "Free gift" where the option is to choose ProdA or ProdB as part of the config - probably not easy and also requires some coding against the orderlines.

A third option is to create 2 discounts - one for proda and one for prodb - and then let each of them trigger on a hidden voucher field (or custom field) where you stamp the user selection into that hidden field?

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Thanks for your reply Nicolai. I agree it's not an easy task.

Can you elaborate on "You would need an extenstion and then store information on the order on the user choice of the possible products. And then add some."? Are you talking about a DiscountExtender or a fully custom (old) discount provider? And what do I need to add?

The other two options don't seem attractive at the moment as I could have a large number of products that are eligible as well as a large number of products that can be a gift. I simplified to ProdA and ProdB to explain the setup.

Thanks!

Imar

 
Nicolai Pedersen
Reply

A custom DiscountExtender - not the old salesdiscountproviders.

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Sorry to keep coming back to this but I am not sure how to implement this completely. 

With a DiscountExtender I get access to DiscountValidForProduct and DiscountValidForOrder which I can use to determine if the user is a first time subscriber. That's working currently (I use DiscountValidForProduct for that at the moment).

But then what? Should I use DiscountValidForProduct and then change the discount that is available on the extender? Or always return false from this method but manually add a discount line to the user's cart using Context.Cart when my code determines we should discount one of the products?

Thanks!

Imar

 
Nicolai Pedersen
Reply
This post has been marked as an answer

I dunno either - you are the smart one of us :-).

You can add the IDiscountExtenderCalculateOrderDiscount on the discount extenderbase - which allows you to take over the calculation of the actual discount. But I guess you need to add an orderline with the selection. So maybe use the discount extender base, use the IDiscountExtenderCalculateOrderDiscount to return 0 and then add an orderline with type=fixed and parentid to the product the discount applies to. Then magic in the template to pick these 2 up - the 0 amount discount should be hidden and the fixed orderline should be rendered as discount.

Maybe you can also change Discount.ProductAsDiscount property in DiscountValidForOrder method?

Votes for this answer: 1
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

With the help from some black magic voodoo witch doctors, I think I have it working. Here's what I did in case someone else wants to try this:

1. Inherited from DiscountExtenderBase

2. Implemented the IDiscountExtenderCalculateOrderDiscount interface

3. When adding the gift to the cart, I add an OrderLineField to it called IsGift and set it to true. That way, I can have a normal product at any quantity and the same product as a gift in the cart side by side.

4. In DiscountValidForProduct, I check if the cart contains the gift product (by checking the OrderLineField and return true when it does. One tricky point here is that DiscountValidForProduct can be called multiple times for the same product: the one added as a regular product and the other as a gift. I return true on the first call only (using HttpContext items) and false on the other calls. This applies this discount just once.

5. One side effect of calling it multiple times is that the discount can be associated with the wrong order line (the regular product, not the gift as the order isn't guaranteed). So I also added a subscriber for Discounts.AfterDiscountCalculation where I check if the discount line points to the gift line correctly, and update it when it doesn't.

6. Finally, in GetDiscount of IDiscountExtenderCalculateOrderDiscount I return 0 when the cart doesn't contain a gift or I return a discount value which I derive from the gifted product at a quantity of 1.

On top of that I made a few changes to templates to highlight gifts and make sure you can't add more than one gift but these changes didn't have much to do with the discount calculation, more with the overall user flow.

If anyone ever needs functionality like this let me know and I can supply code and a walkthrough. You may have to give up your firstborn to make it work :-)

Thanks for the guidance Nicolai!

Imar

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Well done Imar.

 

I am also assuming you changed the templates to determine when to show the options for Gifts. I assume that's #6, that by returning a dummy discount value, you render the gift card options/products (previously described in bullet #3 of how to add them). Is that accurate?

 

Best Regards,

Nuno Aguiar

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

>> I assume that's #6, that by returning a dummy discount value, you render the gift card options/products (previously described in bullet #3 of how to add them).

I have no idea what you are saying so it's probably not accurate :-)

I have a three step process:

1. Show normal products

2. Select a gift

3. Continue checkout

When you are allowed to add a gift, step 1 takes you to step 2. Otherwise, you are taken to step 3.

On step 2 we render a list of products from a specific group using a customized template that sets the IsGift order line field to true for the product you are adding. It also ensures you can add only one product by using radio buttons to select the gift. 

That's pretty much all.

Imar

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Ah I see, you select the gifts when adding to Cart. I thought you were trying to do so within the cart, after considering the entire cart content, in case you were only entitled to a Gift if both Prod A and Prod B were in the cart (which would be harder to solve for).

 

Thanks for explaining that.

 

You must be logged in to post in the forum