laravelshoppingcart icon indicating copy to clipboard operation
laravelshoppingcart copied to clipboard

Quantity as a decimal

Open dylanglockler opened this issue 8 years ago • 7 comments

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?

dylanglockler avatar Jul 09 '16 23:07 dylanglockler

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 } ] }

dylanglockler avatar Jul 18 '16 06:07 dylanglockler

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.

kevinruscoe avatar Jun 12 '17 10:06 kevinruscoe

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);
}

dylanglockler avatar Jun 12 '17 21:06 dylanglockler

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.

kevinruscoe avatar Jun 21 '17 09:06 kevinruscoe

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

kevinruscoe avatar Jun 21 '17 14:06 kevinruscoe

I think it is unnecessary to use int type casting in the metod updateQuantityNotRelative(). In add method there is no type casting.

erisrayanesh avatar Sep 25 '17 16:09 erisrayanesh

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

DiegoAlonso27 avatar Mar 21 '22 06:03 DiegoAlonso27