Skip to content

Instantly share code, notes, and snippets.

@CodeBrauer
Last active August 29, 2015 14:04
Show Gist options
  • Save CodeBrauer/32876d92c527d7b8f2e8 to your computer and use it in GitHub Desktop.
Save CodeBrauer/32876d92c527d7b8f2e8 to your computer and use it in GitHub Desktop.
Some helper functions for CodeIgniter Cart Library. With this functions you can get all ids of your cart and also check if the id is in the cart.
<?php
/* cart helper */
function is_item_in_cart($id) {
return in_array($id, get_cart_item_ids());
}
function get_cart_item_ids() {
$CI =& get_instance();
$cart = $CI->cart->contents();
$cart_ids = array_map(function($element) {
return $element['id'];
}, $cart);
return $cart_ids;
}
function get_row_id_by_item($item_id) {
$items = get_cart_item_ids();
return array_search($item_id, $items);
}
@CodeBrauer
Copy link
Author

Tested with CI 2.1.3

@CodeBrauer
Copy link
Author

[cart_contents] => Array
        (
            [0185acd2e0e6a4e58e0b222dacf38497] => Array
                (
                    [rowid] => 0185acd2e0e6a4e58e0b222dacf38497
                    [id] => 1234
                    [qty] => 1
                    [price] => 10.00
                    [name] => My New Product
                    [options] => Array
                        (
                            [thumbnail] => http:/example.com/thumb1234.png
                            [foo] => bar
                        )

                    [subtotal] => 10.00
                )

            [total_items] => 1
            [cart_total] => 1
        )

is_item_in_cart(1234) returns true
is_item_in_cart(4321) returns false


get_cart_item_ids() returns

Array (
0185acd2e0e6a4e58e0b222dacf38497 => 1234
)


get_row_id_by_item(1234) returns 0185acd2e0e6a4e58e0b222dacf38497
get_row_id_by_item(4321) returns false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment