my system is not perfect but it works for me, but not on variant product
i use :
{% assign prixTTC = product.price | times: 1.2 %}
{% if product.compare_at_price_max > 0 %}
{% assign baseTTC = product.compare_at_price | times: 1.2 %}
{% assign pourcentage = product.price | times: 1000 | divided_by: product.compare_at_price_max %}
{% assign pourcentageReduction = 1000 | minus: pourcentage | divided_by: 10 %}
{% assign eurosReduction = baseTTC | minus: prixTTC %}
{% endif %}
<script type="text/javascript">
// Auto Discount
let pvfID = {{ product.variants.first.id }},
priceShopify = {{ product.price }},
taxes = 1.2,
totalValueTTC = 0,
discounts= [],
discountAllocation = function (type, title, value, valueType, allocationMethod, targetSelection, targetType, totalAllocatedAmount) {
this.type = type;
this.title = title;
this.value = value;
this.valueType = valueType;
this.allocationMethod = allocationMethod;
this.targetSelection = targetSelection;
this.targetType = targetType;
this.totalAllocatedAmount = totalAllocatedAmount;
},
pvfIDPOST = function() {
jQuery.post('/cart/add.js', {quantity: 1, id: pvfID})
},
pvfIDREMOVE = function(newQty){
jQuery.post('/cart/change.js', {quantity: newQty, id: pvfID})
};
// Check Auto Discount
function checkAutoDiscount(){
jQuery.getJSON('/cart.js', function(cart) {
for(let i = 0; i < cart.items.length; i++){
if(pvfID === cart.items[i].id){
if(cart.items[i].variant_options.length >= 1 ){
let currentQty = cart.items[i].quantity;
if(cart.items[i].line_level_discount_allocations.length > 0){
for(let it = 0; it < cart.items[i].line_level_discount_allocations.length; it++){
let currentItem = cart.items[i].line_level_discount_allocations[it].discount_application;
discounts[it] = new discountAllocation(currentItem.type, currentItem.title, currentItem.value, currentItem.value_type, currentItem.allocation_method, currentItem.target_selection, currentItem.target_type, currentItem.total_allocated_amount);
}
showDiscount();
pvfIDREMOVE(currentQty-1);
}else{
console.log('automatic discount : no');
pvfIDREMOVE(currentQty-1);
}
}
}
}
});
}
// YOU NEED CUSTOM THIS FOR YOUR THEM :
function showDiscount(){
$('.detail-price .price-sale').fadeOut();
for(let i = 0; i < discounts.length; i++){
let value = parseInt(discounts[i].value, 10);
// Percent Discount
if(discounts[i].valueType === 'percentage'){
let valueTTC = (((priceShopify * value) / 10000)*taxes).toFixed(2);
$('#product_v2_allDiscountBox').append(
'<div class="product_v3_badge4">'+
'<i class="demo-icon icon-electro-cart-icon"></i>'+
' '+discounts[i].title+' : -'+value+'%'+' vous économisez : '+valueTTC+'€'+
'</div>'
);
if($(window).width() < 457 ){
$('.detail-price').append(
'<div class="price-sale" style="color: #5bb300!important">'+
((priceShopify / 100) * taxes - valueTTC).toFixed(2)+'€'+
'</div>'
);
// Product Discount
{% if product.compare_at_price_max > 0 %}
$('.product_v2_boxContent .product-price').append(
'<div class="product_v2_reductionValue" style="background-color: rgb(194, 222, 244);">'+
'<div class="sale-text">'+
'Remise sur le produit : <strong>-'+{{pourcentageReduction}}+'%</strong>'+
'</div>'+
'</div>'
)
{% endif %}
// Cart Discount
$('.product_v2_boxContent .product-price').append(
'<div class="product_v2_reductionValue" style="background-color: rgb(194, 222, 244);">'+
'<div class="sale-text">'+
'Offre spéciale : <strong>-'+value+'%</strong>'+
'</div>'+
'</div>'
)
}else{
$('.product_v2_reduction').append(
'<div class="product_v2_reductionValue">'+
'<div class="sale-text" style="background-color: #5bb300">'+
'-'+value+'%'+
'</div>'+
'</div>'
);
$('.detail-price').append(
'<div class="price-sale" style="color: #5bb300!important">'+
((priceShopify / 100) * taxes - valueTTC).toFixed(2)+'€ TTC'+
'</div>'
);
}
totalValueTTC += valueTTC;
// Remise fixe
}else{
let valueTTC = (value * taxes).toFixed(2);
$('#product_v2_allDiscountBox').append(
'<div class="product_v3_badge4">'+
'<i class="demo-icon icon-electro-cart-icon"></i>'+
' '+discounts[i].title+' : -'+valueTTC+'€ TTC'+
'</div>'
);
if($(window).width() < 457 ){
$('.detail-price').append(
'<div class="price-sale" style="color: #5bb300!important">'+
((priceShopify / 100) * taxes - valueTTC).toFixed(2)+'€'+
'</div>'
);
// Product Discount
{% if product.compare_at_price_max > 0 %}
$('.product_v2_boxContent .product-price').append(
'<div class="product_v2_reductionValue" style="background-color: rgb(194, 222, 244);">'+
'<div class="sale-text">'+
'Remise sur le produit : <strong>-'+{{pourcentageReduction}}+'%</strong>'+
'</div>'+
'</div>'
)
{% endif %}
// Cart Discount
$('.product_v2_boxContent .product-price').append(
'<div class="product_v2_reductionValue" style="background-color: rgb(194, 222, 244);">'+
'<div class="sale-text" >'+
'Offre spéciale : <strong>-'+value * taxes +'€ TTC</strong>'+
'</div>'+
'</div>'
)
}else{
$('.product_v2_reduction').append(
'<div class="product_v2_reductionValue">'+
'<div class="sale-text" style="background-color: #5bb300">'+
'-'+value+'€'+
'</div>'+
'</div>'
);
$('.detail-price').append(
'<div class="price-sale" style="color: #5bb300!important">'+
((priceShopify / 100) * taxes - valueTTC).toFixed(2)+'€ TTC'+
'</div>'
);
}
totalValueTTC += valueTTC;
}
// Add style mobile
if($(window).width() < 457 && {{product.compare_at_price_max}} != null ){
for (let ite =0; ite < $('.product_v2_reduction').length; ite++){
$('.detail-price').css('width','75%');
$('.detail-price').css('margin','auto');
$('.detail-price').css('flex-flow','row-reverse');
$('.product_v2_reduction').css('margin','10px 0');
$('.price-compare').css('flex','1');
$('.price-compare s').css('margin','0 5px 3px 5px');
$('.price-compare s').css('font-size','22px');
$('.detail-price .product_v2_reductionValue').css('display','none');
$('.price-sale').css('font-size','30px');
$('.price-sale').css('flex','1');
}
}
}
}
// Start checkAutoDiscount if variant is not available
{% unless variant.available %}
$.when( pvfIDPOST() ).done(setTimeout (function() {
checkAutoDiscount()
}, 1000 ));
{% endunless %}
</script>
I hope this will inspire you.