I have a simple checkbox form which has three options. I want to calculate the price dynamically and i am not storing these values in a database.
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="option1" wire:click="test" wire:model="additions" value="option1">
<label class="custom-control-label" for="option1">Option 1</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="option2" wire:click="test" wire:model="additions" value="option2">
<label class="custom-control-label" for="option1">Option 2</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="option3" wire:click="test" wire:model="additions" value="option3">
<label class="custom-control-label" for="option1">Option 3</label>
</div>
output in the blade file where i show the value
Each option has different prices and i am adding to the $checkout to calculate the total.
public $additions = [];
public $checkout = 0;
public function test()
{
// if (in_array('option1', $this->additions)) {
// $this->checkout =+ 10;
// Log::info($this->checkout);
// }
// if (in_array('option2', $this->additions)) {
// $this->checkout += 10;
// Log::info($this->checkout);
// }
// if (in_array('option3', $this->additions)) {
// $this->checkout += 10;
// Log::info($this->checkout);
// }
if (!empty($this->additions)) {
foreach ($this->additions as $key => $value) {
if ($value == 'option1') {
$this->checkout += 10;
}
if ($value == 'option2') {
$this->checkout += 10;
}
else {
$this->checkout -= 10;
}
if ($value == 'option3') {
$this->checkout += 10;
}
else {
$this->checkout -= 10;
}
}
Log::info($this->checkout);
}
}
the prices adds but is not correct. For the example i have 10 for each option so when all three options are checked, the total would be 30 and when two options are checked 20 and so on. What i am getting is all over 60 which is no where near 30.
Any help would be appreaciated.
source https://stackoverflow.com/questions/68535169/add-and-substract-values-based-on-checkbox-in-php
Comments
Post a Comment