在php中为类cart指定十进制数量

在php中为类cart指定十进制数量,php,decimal,shopping-cart,Php,Decimal,Shopping Cart,我正在我的页面上使用 我的问题是,现在有了这个类,我不能给出从产品到购物车的十进制数量 如果,我可以订购的产品的最小数量是15,78 m2,程序会将其添加到购物车中,但15,78数量值将“转换”为1 如何在此类中修改add函数?现在,代码是原创的,我没有修改它 public function add($id, $quantity = 1, $attributes = array()) { $quantity = (preg_match('/^\d+$/', $quantity)) ? $

我正在我的页面上使用

我的问题是,现在有了这个类,我不能给出从产品到购物车的十进制数量

如果,我可以订购的产品的最小数量是15,78 m2,程序会将其添加到购物车中,但15,78数量值将“转换”为1

如何在此类中修改add函数?现在,代码是原创的,我没有修改它

public function add($id, $quantity = 1, $attributes = array())
{
    $quantity = (preg_match('/^\d+$/', $quantity)) ? $quantity : 1;
    $attributes = (is_array($attributes)) ? array_filter($attributes) : $attributes = array();
    $hash = md5(json_encode($attributes));

    if (count($this->items) >= $this->cartMaxItem && $this->cartMaxItem != 0) 
    {
        return false;
    }

    if (isset($this->items[$id]))
    {
        foreach ($this->items[$id] as $index => $item) 
        {
            if ($item['hash'] == $hash) 
            {
                $this->items[$id][$index]['quantity'] += $quantity;
                $this->items[$id][$index]['quantity'] = ($this->itemMaxQuantity < $this->items[$id][$index]['quantity'] && $this->itemMaxQuantity != 0) ? $this->itemMaxQuantity : $this->items[$id][$index]['quantity'];

                $this->write();
                return true;
            }
        }
    }

    $this->items[$id][] = array
    (
        'id'         => $id,
        'quantity'   => ($quantity > $this->itemMaxQuantity && $this->itemMaxQuantity != 0) ? $this->itemMaxQuantity : $quantity,
        'hash'       => $hash,
        'attributes' => $attributes,
    );

    $this->write();
    return true;
}
公共函数add($id,$quantity=1,$attributes=array())
{
$quantity=(preg_match('/^\d+$/',$quantity))?$quantity:1;
$attributes=(is_数组($attributes))?数组过滤器($attributes):$attributes=array();
$hash=md5(json_encode($attributes));
如果(计数($this->items)>=$this->cartMaxItem&&$this->cartMaxItem!=0)
{
返回false;
}
if(设置($this->items[$id]))
{
foreach($this->items[$id]作为$index=>$item)
{
如果($item['hash']=$hash)
{
$this->items[$id][$index]['quantity']+=$quantity;
$this->items[$id][$index]['quantity']=($this->itemMaxQuantity<$this->items[$id][$index]['quantity']&&&$this->itemMaxQuantity!=0)?$this->itemMaxQuantity:$this->items[$id][$index]['quantity'];
$this->write();
返回true;
}
}
}
$this->items[$id][]=数组
(
'id'=>$id,
“数量”=>($quantity>$this->itemMaxQuantity&&$this->itemMaxQuantity!=0)?$this->itemMaxQuantity:$quantity,
“哈希”=>$hash,
“属性”=>$attributes,
);
$this->write();
返回true;
}

如果我正确阅读了文档,您可以向购物车添加物品和数量,并在需要时添加更多信息。此信息可以是您的15,78平方米。因此,客户从您商店的ID处订购了1x 15.78 m2

“cartMaxItem”和“itemMaxQuantity”来自数据类型int,因此您可以更改此项或更改您的店铺逻辑,以销售的不是m^2而是cm^2,并参考客户可以购买的cm^2数量。

此行
(预匹配('/^\d+$/',$quantity))$数量:1
是一个三元运算符,它将给定的数量与正则表达式模式相匹配,正则表达式模式表示“如果给定的数量是至少整数或更多的任何序列(即
1为true
432为true
84835为true
2.78为false
),然后按原样传递。如果给定的数量与正则表达式模式不匹配(即
2.78,为false
),则将值1分配给该变量”

在您的情况下,您可以创建一个正则表达式模式,如
/^\d+\.\d+$/
,它将接受所有类型的数字,并用点分隔(即
2.78为真
22.78为真
84728774.87923587为真
),或者您可以将正则表达式模式最小化到所需的小数,如
/\d+.\d{2,3}/
。此模式只接受点后的
2或3位
,这意味着接受
2.78、22.789或287398724.32等数字
,但不接受
2.8或2.9084等数字
。这就是正则表达式的全部内容

在修复正则表达式模式后,您可以始终对结果使用类型转换方法,如
$this->items[$id][$index]['quantity']+=(float)$quantity
将其添加到您的总数中

确保您的数量始终被视为浮点数,即使使用整数也是如此。例如,数量
1
必须为
1.00

我建议修改regex的原因是因为您的函数已经使用了regex。否则,只能使用值的长度、大于0的值和其他验证


希望有帮助

15,78
不是数字,而是逗号分隔的列表
15.75
是一个数字,但这一行
$quantity=(preg_match('/^\d+$/',$quantity))$数量:1个
会将任何内容转换为整数或
1
是的,这就是问题所在。我不建议将“cartMaxItem”更改为其他内容。如我所述更改您的店铺逻辑,并将m^2的数量作为参数传递给中所示的item->add()方法ad。