laravelshoppingcart
laravelshoppingcart copied to clipboard
Quantity as a decimal
I'm trying to adapt this very nice cart you've created on a project where the quantities are services based in time broken down by half hour. So a quantity could be 1.5 (1 and 1/2 hours). When I add 1.5 as a quantity it rounds down. I thought perhaps if I changed the validation but doing so didn't seem to have any effect.
Any tips on how I might make this adjustment?
I tried using a condition but that doesn't seem to be working as described. The condition is not included in the total, which ends up as 0 when I add the condition.
CartConditionCollection {#265 ▼ #items: array:1 [▼ "hours" => CartCondition {#224 ▼ -args: array:4 [▼ "name" => "hours" "type" => "misc" "target" => "item" "value" => "+125" ] -parsedRawValue: null } ] }
Hello, just wondering if you found a way around this? My client just informed me that certain products are sold by length, so I need to be able to set quantities as decimal points.
I ended up using attributes - with some services they charge by the hour, so I store the number of hours as an attribute then I modified getPriceSum and getPriceSumWithConditions in ItemCollection.php
public function getPriceSum()
{
if( $this->attributes->is_hourly ) {
$val = ($this->price * $this->quantity) * $this->attributes->hours;
} else {
$val = $this->price * $this->quantity;
}
return Helpers::formatValue($val, $this->config['format_numbers'], $this->config);
}
public function getPriceSumWithConditions($formatted = true)
{
if( $this->attributes->is_hourly ) {
$val = ($this->getPriceWithConditions(false) * $this->quantity) * $this->attributes->hours;
} else {
$val = $this->getPriceWithConditions(false) * $this->quantity;
}
return Helpers::formatValue($val, $formatted, $this->config);
}
Thanks for the info. I ended up just changing L632 of Cart.php to change the validator that the min value is 0.1. Thats seems to allow me to pass a decimal value and the cart still functions as it should.
Oh, there some crazy issues with the cart rounding quantities, so I had to amend the updateQuantityRelative()
and updateQuantityNotRelative()
methods with the following https://gist.github.com/kevdotbadger/1776ec2ccc8985e5ac75c5e5c61e1e1e
I think it is unnecessary to use int type casting in the metod updateQuantityNotRelative()
. In add
method there is no type casting.
I had the same inconvenience when putting a decimal quantity, since the update was equal to 0.1, since if it was put from the beginning, it could be done, but when updating, it could not, then in updateQuantityRelative() and updateQuantityNotRelative() update all the int to float