将十进制数量提供给php中的类购物车

问题描述 投票:0回答:1

我正在使用页面上的this cart class

我的问题是,现在有了这个课堂,我不能从产品到购物车中提供十进制数量。

如果我可以订购的产品的最小数量为15,78平方米,则程序会将其添加到购物车,但是数量为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;
}
php decimal shopping-cart
1个回答
0
投票

如果我正确阅读了文档,则可以在购物车中添加一个项目和一个数量,并在需要时添加一些其他信息。该信息可能是您的15,78平方米。因此,客户从您商店中具有ID的商品中订购了1x 15.78平方米。

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

© www.soinside.com 2019 - 2024. All rights reserved.