SilverCart Forum

We moderate this Forum and we're here to help. Have you already run a forum search to check if your problem has already been solved?

You can help us helping you by providing detailed error messages, screenshots and logfile entries.

SebastianRamon

Page: 1
Topic Special Prices (discount) 7504 Views

Special Prices (discount)

23 November 2012 at 11:11am

What would be the best way to extend Silvercart to support special prices (or prices with a discount applied).

I would like to display 2 prices for certain products. The gross price and a discount price. Only certain products would have this special price.

Re: Special Prices (discount)

23 November 2012 at 11:36am Last edited: 23 November 2012 11:38am

Hi balazs,

amazed to see you are from Romania :)

If I get your point right, you would not need to extend SilverCart.

In the prices tab, there is a field "RRP", which translates to "recommended retail price". You can use this field to maintain the gross price of the product when it has a discount price. You then enter the discount price of your product as the gross price.

In your theme you can check for the RRP and if present, your for example display it struck out.

For products without RRP (and hence no "discount") the regular price is shown and nothing changes.

I think this is the easiest way to solve your problem, since it would only require some minor template-kung-fu and no extension of SilverCart.

Do you think this would work for you?

Cheers
Ramon

Re: Special Prices (discount)

23 November 2012 at 1:51pm

Hi Ramon,

Something similar was our first approach too, however this could be unclear for the client who updates the prices in the admin area.

Basically we said to use PriceGross for the special price (this way we don't have to make changes to the code which handles the shopping cart, invoices etc.) and we decided to add another field to the admin, something like PriceNoDiscount which would contain the normal price (without the discount applied).

The second approach I've been thinking of was to modify the price in the updatePrice() method of the product decorator we already have in place (SilvercartProductDecorator). In this approach I've been thinking to use 2 more fields for a product. A field called DiscountAmount and another field called DiscountType (with 2 possible options, one for Amount Off and another one for Percentage Off).

Using these two fields I can update the price in the updatePrice() method, but then I need a way to calculate the normal price based on the special price, discount amount and discount type.

The problem with this approach (when using the percentage off discount type) is the following:

1. the price in the getPrice() method in the SilvercartProduct class is rounded to two decimal digits

2. the price from point 1. is updated in SivlercartProductDecorator by subtracting the calculated discount amount (based on the precentage) and to have a nice special price the result is rounded as well

3. to get the normal price based on the discount percentage and the special price I created a separate method in SilvercartProductDecorator, but since the special price was rounded to two decimal digits at point 2. there are situations when the initial normal price (from point 1.) is not the same with this calculated normal price

I wrote a little test program, and I found the solution, which would be to use the exact (not rounded) special price in calculations at point 3. - which means that I need to keep track (save to the DB) the exact special price (probably when the prices are saved in the admin area). Now, I'm a newby to Silverstripe and don't know exactly if this is possible and if yes, how should I do that.

Here's a small example:
Price Gross = $4.84

Discount = 1% (so DiscountType is Percentage off)

Calculated discount amount: $0.0484

Rounded special price: $4.84 - $0.0484 = round(4.7916, 2) = 4.79

Exact special price: $4.7916

Calculated normal price (using rounded special price) = 100 / (100 - 1) * specialPrice = 4.838383..., which if it's rounded as well is $4.83 (AND THIS ONE IS NOT THE SAME AS $4.84)

Calculated normal price (using exact special price) = 100 / (100 - 1) * 4.7916 = $4.84 (exactly as the normal inital gross price)

I just hope this all makes sens for you guys... I can post some code later too if not.

Thanks a lot for the fast feedback !

Re: Special Prices (discount)

27 November 2012 at 8:36pm

Hi balazs,

sorry for not getting back to you earlier. Our schedule was so tight these days because of the current release 1.3.3. of our shopsoftware SilverCart.

We'll discuss your issue tomorrow and see if we can find a solution for you.

Cheers
Ramon

Re: Special Prices (discount)

29 November 2012 at 11:03am

Ramon,

I solved it the following way:

To the SilvercartProductDecorator class I added 4 new DB fields:

hasSpecialPrice (of type Boolean)
DiscountType (of type Enum('AmountOff,PercentageOff'))
Discount (of type Decimal(19,2))
CalculatedDiscount (of type Decimal(19,2))

hasSpecialPrice, DiscountType and Discount are added in the backend as CMS fields

In the onBeforeWrite() method of SilvercartProductDecorator I set the value of CalculatedDiscount to the value of Discount if DiscountType is set to AmountOff, and I set it to the computed and rounded discount amount if DiscountType is set to PercentageOff.

I update the price in the getPrice() method using that rounded amount set in the CalculatedDiscount field. I created a getNormalPrice() method which adds the value of CalculcatedDiscount to the current price if special price is enabled for the product, and this way I have a way to get the special price, using getPrice() and another way to get the normal price using the getNormalPrice().

Discounts are calculated based on the net price.

Here are some examples (with PriceNet = PriceGross = $4.84):

For DiscountType set to AmountOff
Initial price: $4.84
DiscountType: AmountOff
Discount: $1.84
CalculatedDiscount will be: $1.84
Special Price (Price): will become $4.84 - CalculatedDiscount ($1.84) = $3
NormalPrice will return: Special Price (Price) + CalculatedDiscount ($1.84) = $3 + $1.84 = $4.84

For DiscountType set to PercentageOff
Initial price: $4.84
DiscountType: PercentageOff
Discount: 50%
CalculatedDiscount will be: $2.42
Special Price (Price): will become $4.84 - CalculatedDiscount ($2.42) = $2.42
NormalPrice will return: Special Price (Price) + CalculatedDiscount ($2.42) = $2.42 + $2.42 = $4.84

Let me know if there's a better way !