Ruby script to calculate discount using Shopify Scripts - javascript

I am using a script to calculate an addtional 5% off when the cart total is greater than $1000, but the variant price is already discounted by 25% and has a was/now pricing setup that is $29.95 was $39.95.
The problem is - i need for the script to discount the orginal item value of $39.95, not the discounted rate which is the new retail price.
Here is the script - i have tried to add 25% back to the line item price without success and there is no line item method that Shopify provides to use the orginal cart line item price in the dev docs - https://help.shopify.com/en/manual/checkout-settings/script-editor/shopify-scripts#line-item-methods
# Tiered Discounts by Spend Threshold 5% off when you spend $1000 or more
min_discount_order_amount_extra = Money.new(cents:100) * 1000 #number of dollars needed in cart to get the discount
DISCOUNT_PERCENT = 25
# calculate the discount to be applied
percent = Decimal.new(DISCOUNT_PERCENT/100) + 1
total = Input.cart.subtotal_price_was * percent
discount = if total > min_discount_order_amount_extra
0.05 #discount percentage in decimal form
else
0
end
message = "Another 5% saved"
# 30% off products excluding commercial items
Input.cart.line_items.each do |line_item|
product = line_item.variant.product
next if product.gift_card?
next if line_item.variant.product.tags.any?{|x| ['commercial'].include?(x)};
line_item.change_line_price(line_item.line_price * (1-discount), message: message) unless discount == 0
end
Output.cart = Input.cart

Related

Passing Stripe Fee to Customer: Unaccounted Profit with Formula Usage

Referring to when you pass the Stripe fee to your customer by offsetting the final total charge to them using the given formula.
Considering the following scenario for a Payment Intent:
Buyer is purchasing a $1,000.00 item (Pgoal)
Buyer is charged Australian Stripe fees of 1.75% on the item (Fpercent)
Buyer is charged a fixed fee of $0.30 AUD (Ffixed)
Buyer is charged a 10% tax only on the Stripe fees component (Ftax, i.e. 10% of whatever is 1.75% of the amount + 30c fixed)
When I check Stripe Dashboard for this Payment Intent using the formula prescribed by Stripe, the following is calculated:
Amount: A$1,019.92
Fee: A$18.15
Stripe Processing Fees: A$16.50
Tax: A$1.65
Net: A$1,001.77
The problem is why am I profiting $1.77?? It should be $1,000.00 flat. Not sure what I'm doing wrong, so any help is appreciated.
The formula Stripe provides is pretty straight forward:
Pcharge = ( Pgoal + Ffixed )
------------------
( 1 - Fpercent )
- Pgoal is $1,000.00 or 100000 (in cents)
- Ffixed is 0.30 or 30 (in cents)
- Fpercent is 1.75% or 0.0175
The only issue with above formula is that it doesn't account for Stripe's taxes it charges the Buyer, which in Australia is 10% on the total Stripe fees. So I've gone ahead and accounted for it in the numerator of the formula:
Pcharge = ( Pgoal + Ffixed + Ftax )
-------------------------
( 1 - Fpercent )
- Pgoal is $1,000.00 or 100000 (in cents)
- Ffixed is 0.30 or 30 (in cents)
- Fpercent is 1.75% or 0.0175
- Ftax is 10% on the Stripe fees, or [ 0.1 * ( Pgoal * Fpercent + Ffixed ) ]

How to get total weight of cart items in BigCommerce

I am currently using this graphQL query within the cart to retrieve each products weight
` query productWeights {
site {
products (entityIds: [${productIDs}]) {
edges {
node {
name
sku
weight {
value
}
}
}
}
}
}`
This brings back the weight of each individual item so I now need to calculate the weight of the amount of items in the cart. How can I get the quantity of items within this query to enable a calculation of the total weight added to cart?

Displaying 2 custom Data attribute items within a div. Ex: Currency & Amount

I have an E-Commerce website built on HTML, JavaScript & PHP.
On product details page user can add product to cart thus I'm displaying total amount of cart value.
I want to display decimal number always (10,2) format.
Currently my code works with minimal thing. On clicking "Add to cart" if product price is 12.00 the Counter div displays 12 only.
<span>
<a href="cart.php" class="text-white">
<i class="fa fa-shopping-cart p1" data-count="10" data-currency="€" id="total"></i>
</a>
</span>
.p1[data-count]:after{
position:absolute;
right:1%;
top:1%;
content: attr(data-count);
font-size:10px;
padding:.2em;
line-height:1em;
color: white;
background:#21468b;
text-align:center;
width: 100%;
font-weight:normal;
}
<script>
var theTotal = '0';
var ProductSellingPrice = '12.00'
$('#insert').click(function(){
theTotal = Number(theTotal) + Number(ProductSellingPrice);
$('#total').attr('data-count', theTotal);
});
</script>
So on clicking insert, the existing TheTotal & current product Price gets added. If there are no products on cart then p1 doesn't display any value, thus want to display zero always if empty/zero. If product price is 12.00 then shows 12 only. If product price is 12.50 then 12.50 is displayed.
I want it to display decimal always & also currency symbol using data attribute.
Displaying decimal problem is solved by #Simone, i m not able to find answer for displaying currency before value using data attribute.
If you want 12.00 and not 12 you have to use Number.prototype.toFixed()
So you have to convert all the single product total (quantity * price is the single total ) into Float number and when you do the total sum, take the number and do this:
Number.parseFloat(total).toFixed(2); // two decimal
Example:
var quantity = 10;
var price = 11;
var tot = parseFloat(quantity * price).toFixed(2);
console.log(tot); // "110.00"

JavaScript Validation for Balance Amount

I have MySQL Table that consist of initial_amount, used_amount & balance as follows.
fin_code initial_amount used_amount balance
1 500,000.00 275,000.00 225,000.00
2 212,000.00 125,000.00 87,000.00
3 455,000.00 455,000.00 -
02) If I enter to issue 300,000.00 as used_amount through the Bootstrap view under fin_code 1, there need to control over issues vs balance. That is 225,000.00.
03) I used the following JavaScript code to perform this task
$(document).on('keyup', '#used_amount', function () {
var bal = parseFloat($('#balance').val());
var amount = parseFloat($('#used_amount').val());
if (bal < amount) {
$('#save').prop('disabled', true);
$('#used_amount_div').addClass('danger');
alert("Can not proceed your request ..!!. Issue amount is higher than the available balance..");
} else {
$('#save').prop('disabled', false);
}
04) But it is properly working for the minus values / balances only. I can not understand what I am going wrong. Can any one help me on this task ?

Convert text to integer and if higher than 50 alert

On our current website that is hosted offsite via another company it is all done with .NET, I simply have access to HTML, JS, and CSS files to edit. A lot of data is output on the page via tokens. On our web page we have a weight token, it grabs the items weight and outputs it between a span tag. So if you're viewing the source it'll show the following:
<span id="order_summary_weight">78.000000 lbs</span>
The token by default outputs the lbs. What I need to do is have javascript grab the 78.000000, convert it to an integer I'm assuming and if that integer, in this case 78.000000 is greater than 50.000000 I'd like it append a line after the 78.000000 lbs to say "Your weight total is over 50 lbs, we will contact you directly with a shipping charge." Understand some weight totals may be as small as 0.010000
I'm coming to you fine folks here because I am at a complete lost where to start in this endeavor.
Something like this ? :
html :
<div class="wrap">
<span class="price" id="order_summary_weight">78.000000 lbs</span>
</div>
<hr>
<div class="wrap">
<span class="price" id="order_summary">50.000000 lbs</span>
</div>
JS :
$('.wrap').each(function(){
var price = $(this).find('.price').text();
price = price.replace(' lbs', '');
price = parseInt(price);
if(price > 50){
$(this).append('<div class="alert">Your weight total is over 50 lbs, we will contact you directly with a shipping charge.</div>');
}
});
DEMO : http://jsfiddle.net/w3qg4/1/
function getWeight()
{
var x=document.getElementById("order_summary_weight");
if(x > 50){
alert("Your weight total is over 50 lbs, we will contact you directly with a shipping charge. Understand some weight totals may be as small as 0.010000");
}
}

Categories