In the real world the discount on products you purchase is quite interesting. For example a seller offers a discount on his products in the following way:
On buying one quantity there will be no discount
On buying 2 he offers 10% discount
On buying 4 and 5 disount will be 20%
On 6 and onward 22%
What is the best way to accomplish this in an eCommerce application?
Take a ceiling function of the exponent or approximation thereof. For example Discount = MaxDiscount * (1 - (N-1)/N), where N is number of items. for 1 item discount is 0, for 2 items discount is 1/2 of the max, for large number of item it will approach MaxDiscount. Use ceiling function to you want discount to be integer number.
Related
This is a good example https://help.shopify.com/en/manual/checkout-settings/script-editor/examples/line-item-scripts#tiered-product-discount-by-quantity
But I need it to work only if you enter code herechoose the same variant. You cant mix and match. The product has multiple variants but I only want the discount to apply if the product is in the same variant group.
discount +10 same products
Example 1 correct
Phone / variant Black x10 = discount
phone / variant Red x5 = NO discount
Example 2 incorrect
Phone / variant Black x5 = discount
phone / variant Red x5 = discount
Discount applied because total is 10 but its incorrect because the variant is not the same
Thanks for the help.
Not sure if I understand the question but this should be a good start.
def apply_discount(cart, min_quantity, perc_discount)
cart.line_items.each do |line_item|
if line_item.quantity > min_quantity
new_line_price = line_item.line_price * perc_discount
line_item.change_line_price(new_line_price, message: "More than 10 same product!")
end
end
end
apply_discount(Input.cart, 10, 0.90)
We just check line by line if it's more than min_quantity we apply the discount.
If you want to apply the discount only once you can just put a return inside the if
This will not work if you have discounts already applied to a subset of items of the same variant... line items are separated by sku and price, so for that you would need something more complex. But I was not sure you were interested in that case.
I'm trying to create a matching algo in pandas that does the following with a given table:
A table contains purchases and sales of products by date, item, quantity (+ for purchases and - for sales) and price.
Conditions:
Create an algorithm that matches purchases and sales per item and the corresponding average profit for each item in total.
Matches can only be on the same date, otherwise they are not matched at all.
Remaining positive or negative inventories per day are ignored
Negative inventories are allowed.
Example with a single product:
date product quantity price
1 X +2 1
1 X -1 2
1 X -2 4
2 X +1 1
2 X +1 2
3 X -1 4
Answer:
The result would be that only on day 1 the 3 trades are matched, with a profit of -2+2+4=4. Because inventory is +2, -1, and then again -1. The remaining inventory of -1 is ignored. Day 2 and 3 have no matches because the trades are not closed on the same day.
Correct output:
product Profit
X +4
Is there any elegant way to get to this result without having to loop over the table multiple times with iterrow?
For reproducing the df:
df = pd.DataFrame({'date':[1,1,1,2,2,3],'product': ['X']*6,'quantity':[2,-1,-2,1,1,-1],'price':[1,2,4,1,2,4]})
The process that you describing could use groupby & aggregate, something like this:
df.groupby('date').sum()
But I don't fully understand your rules for matching. So in Day 1, I got a different total profit. Price * quantity is (+2*1)+(-1*2)+(-2*4)=-8, so profit seems to be 8.
Using iterrow() is a rather bad practice. Not only you're writing excessive code, but also it's likely much slower (check a comparison here).
Most of those type of jobs can be accomplished by combining groupby(), aggregate() and apply(). Check out this great tutorial.
I hope this helps you or future answers :)
For example - Column ABC number(12,4).
My value is 2.7487 and if divided by 2 it's 1.3744 (rounding with 4).
1.3744 + 1.3744 = 2.7488
How do I get a result - 2.7487 as the original.
You cannot. That's normal behavior of finite arithmetic (integer and floating point).
Even if you add large number of decimal places, you'll still lose precision.
This is a typical case of the rounding of an invoice. For example you have an invoice for a total of $100.00 that you need to divide in 3 items. You get three items of $100.00 / 3.0 each one. That is:
$33.33 Item #1.
$33.33 Item #2.
$33.33 Item #3.
For a total of... $99.99! (not the $100.00 that you expected).
The known solution for ages now is to adjust one or more of the values to add or decrease it by one cent. In this case, you could add 1 cent to the last item, to get:
$33.33 Item #1.
$33.33 Item #2.
$33.34 Item #3.
For a total of... $100.00! Perfect.
There are multiple possible combinations (all valid) to adjust those values. That's how it's done in accounting.
I have a capex table to calculate investment of a new product.
As shown in pic all numbers are not real numbers
D83 is the initial investment, meaning we invest $15,800 to buy machine and space
E85 to I85 are the variable cost each year.
E86 to I86 are the revenue each year, revenue = price * est.sold quantity
E87=E85+E86, F87=F85+F86, etc.
D91 = {INDEX($E$87:$I$87,MATCH(TRUE,$E$89:$I$89>0,0))} This is an array to find the first positive cash flow of Row 89 and return the value in row 87
D92 = {INDEX($E$83:$I$83,MATCH(TRUE,$E$89:$I$89>0,0))} This is an array to find the first positive cash flow of Row 89 and return the value in row 83
D93 = COUNTIF(E89:I89,"<0")+((-D92)/D91)
In this way, I calculated the payback years of a project.
In cell D98 I need to calculate that given a certain payback year number, other things hold still, what the price needs to be. Since D91 and D92 are arrays to lookup for the first positive cash flow and each year cash flow is not even, I can't think of a formula to put in D98 so D98 changes with C98, for example, if I put 2 in C98 meaning I need the payback period to be 2 years, and D98 will automatically give me a number to show what price it should be.
Is there any formula or VBA to do it?
I am currently setting up the shipping settings for my site www.lordswoods.co.uk
I have set up two carriers
Yodel- maximum package weight 30kg (carrier range 1kg to 75kg) price £7.75
Pallet track - for all total orders above 75kg
When I order a 1 x 20kg item such as pro master 5 - economy green. The shipping price in the cart is £7.75 as it should be. However when I increase the quantity to two, the shipping price stays the same at £7.75, whereas I want it to to be £7.75 per item until 75 kg is reached.
e.g., 3 x promaster 5 should equal £23.25 (3 x 7.75)
How do I set this up?
You can try adding the shipping cost to all the products separately instead of adding it in the carrier. That way your shipping charges will increase for each item added to the cart.