Ecommerce Shopify WordPress Discussion

currentQuantity retaining the previous value even when variant is change

variants has rules on certain quantities. When currentQuantity matches the rule.qty it will display cancelled price and discounted price. So what happening it's for example we have two variants and both has rule for 3 qty like buy 3 get 10% off like. So when i updated the quantity to 3 its displaying cancelled price and discounted price for that varaint and when suddenly chnage to 2nd variant it's displaying cancelled price and discounted price for 2nd variant as 2nd variant also has rule on 3 qty. As currentQuantity is retaining previous value. var oscpProductTemplate = { settings: {}, init: function () { var $this = this; if (typeof oscpCPWAppConfig == "undefined") { eval( jQuery(document) .find('script[type="text/oscpAppSetting"]') .eq(0) .text() ); } //$this.settings = (typeof oscpCPWAppConfig == 'undefined') ? {} : JSON.parse(oscpCPWAppConfig); $this.settings = typeof oscpCPWAppConfig == "undefined" ? {} : oscpCPWAppConfig; jQuery(document).on("change", 'input[name="id"]', function (e) { if (typeof $this.settings.timer !== "undefined") { clearTimeout($this.settings.timer); } $this.settings.timer = setTimeout(function () { $this.setProductTemplate(); }, 200); }); document.addEventListener("cpw:product-init", function (evt) { setTimeout(function () { $this.setProductTemplate(); }, 200); }); setTimeout(function () { $this.setProductTemplate(); }, 1000); }, sortQtyAscending: function (jsonData) { jsonData.forEach((item) => { item.rules.sort((a, b) => parseInt(a.qty) - parseInt(b.qty)); }); return jsonData; }, setProductTemplate: function () { if (!this.settings.priceGrid[0].hasOwnProperty("rules")) { return; } $("#oscpPriceGrid").remove(); var url = new URL(window.location.href); var variantId = url.searchParams.get("variant"); if (variantId == null || variantId == "") { variantId = this.settings.priceGrid[0].id; } this.settings.priceGrid = this.sortQtyAscending( this.settings.priceGrid ); var variantPrice = ""; for (var v = 0; v < this.settings.product.variants.length; v++) { if (this.settings.product.variants[v].id == variantId) { variantPrice = this.settings.product.variants[v].price; } } var quantityInput = document.querySelector( "#Quantity-template--20089102827796__main" ); var updatePrice = () => { var currentQuantity = quantityInput.value; console.log("Current Quantity:", currentQuantity); //Dynamically change cancelled price and discounted price // Loop through the priceGrid array for (var i = 0; i < this.settings.priceGrid.length; i++) { // Get the current priceGrid object var priceGrid = this.settings.priceGrid[i]; // Get the id of the current priceGrid object var priceGridId = priceGrid.id; // Get the number of rules in the current priceGrid object var priceGridRuleLength = priceGrid.rules.length; // Check if the priceGrid id matches the variant id and there are rules present if (priceGridId == variantId && priceGridRuleLength > 0) { // Initialize variables for cancelled price and discounted price var cancelledPrice = 0; var discountedPrice = 0; // Loop through the rules in the current priceGrid object for (r = 0; r < priceGridRuleLength; r++) { // Get the value of the current rule var Rulevalue = priceGrid.rules[r].value; if (currentQuantity == priceGrid.rules[r].qty) { // Check the type of the current rule if (priceGrid.rules[r].type == "percent") { // If the rule type is percent, calculate cancelled and discounted prices accordingly cancelledPrice = variantPrice; discountedPrice = variantPrice * (1 - Rulevalue / 100); } else { // If the rule type is not percent, calculate cancelled and discounted prices accordingly cancelledPrice = variantPrice; discountedPrice = Rulevalue * 100; } $(".original-price del").text( this.setMoneyFormat(cancelledPrice) ); $(".discounted-price").text( this.setMoneyFormat(discountedPrice) ); break; } else { // If the quantity is not equal to the rule quantity, calculate cancelled and discounted prices accordingly $(".original-price del").empty(); $(".discounted-price").empty(); break; } } // Exit the loop break; } else if ( priceGridId != variantId && priceGrid.rules == null ) { // If the priceGrid id doesn't match the variant id and there are no rules present, // log "Rule not found" to the console and exit the loop console.log("Rule not found"); break; } else { // If none of the above conditions are met, log the variant id to the console // and empty the original-price and discounted-price elements in the UI console.log("Variant else id: ", variantId); $(".original-price del").empty(); $(".discounted-price").empty(); } } } quantityInput.addEventListener("change", updatePrice); // Call the function wherever else you need the dynamic behavior updatePrice(); //End OF Dynamically change cancelled price and discounted price for (var j = 0; j < this.settings.priceGrid.length; j++) { if ( this.settings.priceGrid[j].id == variantId && this.settings.priceGrid[j].rules.length ) { $(".cpw-product__price").html( "<s data-compare-price>" + this.setMoneyFormat(variantPrice) + '</s> <span class="" data-price=""> ' + this.setMoneyFormat( this.settings.priceGrid[j].rules[0].value * 100 ) + "</span>" ); var qtybreakHtml = "<table id='oscpPriceGrid'><tr><th>Qty</th><th>Price</th></tr>"; for (i = 0; i < this.settings.priceGrid[j].rules.length; i++) { var rowPrice = this.settings.priceGrid[j].price; var rowrules = this.settings.priceGrid[j].rules[i]; rowrules.type = rowrules.type == "discount" ? "percent" : rowrules.type; var price = this.getLineItemPrice( rowPrice * 100, rowrules, 1 ); //var price = [10,20,30]; var Pricerules = rowrules.type == "percent" ? rowrules.value + " " + "% OFF" : this.setMoneyFormat(rowrules.value * 100) + " each"; qtybreakHtml += "<tr><td>" + "Buy " + this.settings.priceGrid[j].rules[i].qty + "</td><td>" + Pricerules + "</td></tr>"; } qtybreakHtml += "</table>"; $("#oscpPriceGridContainer").append(qtybreakHtml); } } }, getLineItemPrice: function (price, rule, rules, qty) { // Return calculated (line item price, line item total price and saving amount) price = parseInt(price); rule.type = rule.type == "discount" ? "percent" : rule.type; var rowValue = rule.type == "percent" ? parseFloat(rule.value) : parseFloat(rule.value) * 100; if (rule.type == "percent") { var calulatedPrice = price - (price * rowValue) / 100; var save = rowValue.toFixed(2) + "%"; } else { var calulatedPrice = rowValue; var save = (100 * (price - rowValue)) / price; save = save.toFixed(2); save = save + "%"; } var calulatedTotalPrice = calulatedPrice * qty; return [calulatedPrice, calulatedTotalPrice, save, rules]; }, setMoneyFormat: function (amount) { // Provission to show price with proper currency and template amount = amount / 100; amount = amount.toFixed(2); return this.settings.priceTemplate.replace("0", amount); }, }; if (!window.jQuery) { var script = document.createElement("script"); script.type = "text/javascript"; script.src = "//code.jquery.com/jquery-3.6.0.min.js"; document.getElementsByTagName("head")[0].appendChild(script); script.readyState && (script.readyState == "loaded" || script.readyState == "complete") ? (script.onreadystatechange = function () { oscpProductTemplate.init(); }) : (script.onload = function () { oscpProductTemplate.init(); }); } else { oscpProductTemplate.init(); } What i want is when currentquantity matches rule qty display cancelled price and discounted price.
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.