Subtract a 5% to the total if a field has more than 20 products

Hi, im working in a Wcart and im trying to achieve this: when a text-input named “cantidad” has a value equal or more than 10, the text-input “total” with the sum of all prices as value, should subtract a 5% of the “total” value.
I think this should be done with ternary operation, but im not very good with it yet.
I’ll apreciate any help. Thnks in advance :slight_smile:

Hi Max,

Here is one way to achieve…Just replace the value of var_total with your calculated total; I’ve just used a static 100.

<div class="container">
	<div class="row">
		<div class="col">
			<input id="inp_total" name="total" type="number" class="form-control" dmx-bind:value="(inp_cantidad.value >= 10 ? var_discounted_total.value : var_total.value)">
			<input id="inp_cantidad" name="cantidad" type="number" class="form-control">
		</div>
	</div>
	<dmx-value id="var_discounted_total" dmx-bind:value="(var_total.value * .95)"></dmx-value>
	<dmx-value id="var_total" dmx-bind:value="100"></dmx-value>
</div>

–Ken

Hi mebeingken, it works pefectly! thnks!
Now i took your code and im trying to use it with more variables like: if the value of “cantidad” is >=25 then subtract 15%.
I made a new variable:

<dmx-value id="var_discounted_total1" dmx-bind:value="(var_total.value * .85)"></dmx-value>

and now im trying to add the new variable to your code without success:

<div class="col">
		<input id="inp_cantidad" name="cantidad" type="number" class="form-control"><input id="inp_total" name="total" type="number" class="form-control" dmx-bind:value="(inp_cantidad.value >= 10 ? var_discounted_total.value : var_total.value) && (inp_cantidad.value >= 25 ? var_discounted_total1.value : var_total.value)">
		
	</div>

I’d probably tackle it differently then

	<div class="container">
		<div class="row">
			<div class="col">
				<input id="inp_total" name="total" type="number" class="form-control" dmx-bind:value="(var_total.value * var_discount.value)">
				<input id="inp_cantidad" name="cantidad" type="number" class="form-control">
				<h1>Title</h1>
			</div>
		</div>
		<dmx-value id="var_total" dmx-bind:value="100"></dmx-value>
		<dmx-value id="var_discount" dmx-bind:value="inp_cantidad.value >= 25 ? .85 : inp_cantidad.value >= 10 ? .75 : 1"></dmx-value>
	</div>

1 Like

Thanks! :clap: :grin:

1 Like