Skip to content

Instantly share code, notes, and snippets.

View dylanjhunt's full-sized avatar

Dylan Hunt dylanjhunt

View GitHub Profile
customer = Input.cart.customer
discount = 0
message = ""
if customer
if customer.accepts_marketing? #determines if the customer accepts marketing
discount = 1000 #number of Dollars to discount in cents
message = "Accepts Marketing Discount" #message to customer when they get the discount
end
end
puts discount
customer = Input.cart.customer
discount = 0
message = ""
if customer
if customer.orders_count < 1
discount = 1000 #discount amount in cents
message = "New Customer - $10 off"
end
end
puts discount
discounted_product = 1522083265
products_needed = [592406273, 4283854977, 4284984897]
products_seen = []
Input.cart.line_items.each do |line_item|
product = line_item.variant.product
products_seen << product.id if products_needed.include?(product.id)
end
Input.cart.line_items.each do |line_item|
discount = Money.new(cents: 100) * 5
discounted_product = 1522083265
products_needed = [592406273, 4283854977, 4284984897]
products_seen = []
Input.cart.line_items.each do |line_item|
product = line_item.variant.product
products_seen << product.id if products_needed.include?(product.id)
end
min_discount_order_amount = Money.new(cents:100) * 30 #minimum amount needed to have in cart to get discount
total = Input.cart.subtotal_price_was
discount = if total > min_discount_order_amount
500 #discount amount you are offering in cents
else
0
end
message = "Here's $5 off" #discount message shown to customer
Input.cart.line_items.each do |line_item|
total = Input.cart.subtotal_price_was
discount = 0.2 #discount percent in decimal format
message = "20% off Sitewide" #message to show when discount is applied
Input.cart.line_items.each do |line_item|
product = line_item.variant.product
next if product.gift_card?
line_item.change_line_price(line_item.line_price * (1-discount), message: message) unless discount == 0
end
@dylanjhunt
dylanjhunt / Shopify Javascript - Add item to cart for free
Last active December 4, 2020 17:57
This code will add any variant you want to the cart when adding another product. This is good for free giveaways and promotions
(function(){
// when I click on the add to cart button
$('.add_to_cart').on('click', function(e){
var variantId = 12275195905;
jQuery.getJSON('/cart.js', function(data){
var items = data.items;
console.log(items);
var currentVarId;
{% if cart.item_count == 0 %}
<div class="sixteen columns">
<div class="section clearfix">
<div class="six columns offset-by-five">
<p class="quote">{{ 'cart.general.continue_browsing_html' | t }}</p>
<a href="{% if cart.items.first.product.collections != blank %}{{ cart.items.first.product.collections.last.url }}{% else %}/collections/all{% endif %}" class="action_button continue-button add_to_cart">{{ 'cart.general.continue_shopping_link_html' | t }}</a>
</div>
<br class="clear" />
</div>
</div>
discounted_product = 12275195905
products_needed = [592406273]
products_seen = []
Input.cart.line_items.each do |line_item|
product = line_item.variant.product
products_seen << product.id if products_needed.include?(product.id)
end
Input.cart.line_items.each do |line_item|
<button type="submit" name="add" class="action_button add_to_cart {% if product.handle contains 'oil-pulling-pack' %}free_item{% endif %}" data-label={{ add_to_cart_label | json }}>