检查购物车商品是否具有相同的属性

时间:2012-11-15 10:41:47

标签: php codeigniter

我使用的是codeigniter购物车库,但现在客户希望用户一次只能查看一个类别的商品,因为他们的支付网关存在一些问题。 目前,该网站对所有类别都有一个结帐逻辑。 当用户将项目添加到购物车时,我有一个像这样的数组

Array
(
    [d8df18561040f3d9bd9868f5c5aaa7c2] => Array
        (
            [rowid] => d8df18561040f3d9bd9868f5c5aaa7c2
            [id] => MYU_SC1
            [qty] => 1
            [price] => 500
            [name] => WAEC Scratch Card
            [service_image] => assets/img/waec.jpg
            [service_category] => scratch_cards
            [subtotal] => 500
        )

    [99483fe03da62c9e98ce71232998f447] => Array
        (
            [rowid] => 99483fe03da62c9e98ce71232998f447
            [options] => Array
                (
                    [size] => 36
                    [colour] => N/A
                )

            [id] => 80433426a546064bf5f8d09a6e7fdabc
            [qty] => 1
            [price] => 5000
            [name] => Green Vee Jeans
            [service_image] => http://localhost/myunivacity/uploads/apparels/IMG_0425.JPG
            [service_category] => apparels
            [subtotal] => 5000
        )

)

我如何检查购物车中的商品是否具有“service_category”元素的相同值?谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

你可以选择这样的东西:

<?php 

$categories = array();

foreach($this->cart->contents() as $cart_item) {
    if(!isSet($categories[$cart_item["service_category"]]) {
        $categories[$cart_item["service_category"]] = 1;        
    }
    else {
        $categories[$cart_item["service_category"]]++;
    }
}

print_r($categories);

?>

这将使用每个类别的计数填充数组