• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find all open carts..

#1
[eluser]Lorren[/eluser]
What would be the best way to pull all sessions that have open carts?

My thought currently is to search the user_data column of the session table with a LIKE to find a portion of the serialized array that reads as containing cart contents with > 0 items.

Does anybody have an better ideas?

- Lorren Smile

#2
[eluser]markup2go[/eluser]
If you're referring to the CI Cart library you can do something like:

Code:
if($this->cart->total_items() > 0){
  // Do something with the contents
}

#3
[eluser]Lorren[/eluser]
Thanks for your reply. I am in fact referring to the CI Cart Library.

Unfortunately the code you posted (as I understand it) will only retrieve the contents of the active session's cart. What I'm looking to do is find and output ALL sessions in the db that have a cart associated with them, and that contain > 0 items. I've read the documentation thoroughly and there doesn't seem to be a native CI way to do this yet (which surprised me, honestly.. this seems like it would be desired functionality for most any website which uses a custom shopping cart.)

#4
[eluser]darkhouse[/eluser]
If you're using the database to store session data, you could just do "SELECT userdata FROM ci_sessions WHERE userdata LIKE '%_cart_contents%'" then unserialize userdata, and you've got all of the carts.

I think the misconception is that this cart class is an online store system. It is only the cart portion of an online store system. It's meant for quick cart development on the front end of your site, nothing more. I just used it last week for the first time, and although I extended it to better suit my needs, it's not bad. It certainly made things a little quicker (I got hung up on dealing with paypal's IPN though... but that's another story).

#5
[eluser]Lorren[/eluser]
Doesn't the cart class require that you use the db to store sessions? (Assumed this was due to the low data storage of cookies..)

In any case, that's probably going to be what I'll do. Was just looking for any other, more elegant solutions.. Not sure there's a more elegant solution avaialable.

There was no misconception that it is an online store system. I merely assumed that most any online retailer deploying a legitimate shopping cart to the public would have the desire to review open carts. With that in mind I feel this library should offer a native way to do so. Difficult to determine where the layer of abstraction should end, I suppose.

In any case, I'm thoroughly satisfied with CI and feel Ellis Labs has done a great job laying the ground work for a lot of very nice implementations. Smile

#6
[eluser]darkhouse[/eluser]
Well, I think storing session data in cookies is a bad idea anyways, but reading the documentation it says you need to use a database to store your session data, so that's good.

I agree it's not the prettiest solution, but I don't see any other way around it. Maybe you should extend the cart library to include this functionality. I extended it so if you hit add to cart and the item is already in it, it adds 1 to the current quantity rather than the default behaviour which is removing it entirely and re-adding it with a quantity of 1. I also removed the restrictions on special characters in product names. I still don't get what that's there for. It's just a name.

For the project I needed the cart library for, I did not need much functionality (actually the client was pretty sure nobody would even be using the store, just needed it to be able to say they do retail online). It's a nice little library for getting something up quickly, but I don't think it's meant to be used in any major application.

I too think CI is great and Ellis Labs has certainly helped speed up my production, both big and small projects.

#7
[eluser]Lorren[/eluser]
I agree.. this is probably the best available method. Will be sure to post here if I find another one.

I'll def. be extending the library for my needs. The other changes you made seem great as well.. care to post them? Smile

(By the way, I hate the smileys on this forum.. it always seems like I'm rolling my eyes or something.. lol)

#8
[eluser]darkhouse[/eluser]
No problem. It's just the _insert function I changed. You'll notice all I did was comment out the part where it checks the product name, and then at the end I created $old_qty, and applied it after the item is re-added.

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Cart extends CI_Cart {
    
    /**
     * Insert
     *
     * @access    private
     * @param    array
     * @return    bool
     */
    function _insert($items = array())
    {
        // Was any cart data passed? No? Bah...
        if ( ! is_array($items) OR count($items) == 0)
        {
            log_message('error', 'The insert method must be passed an array containing data.');
            return FALSE;
        }
    
        // Does the $items array contain an id, quantity, price, and name?  These are required
        if ( ! isset($items['id']) OR ! isset($items['qty']) OR ! isset($items['price']) OR ! isset($items['name']))
        {
            log_message('error', 'The cart array must contain a product ID, quantity, price, and name.');
            return FALSE;
        }
    
        // Prep the quantity. It can only be a number.  Duh...
        $items['qty'] = trim(preg_replace('/([^0-9])/i', '', $items['qty']));
        // Trim any leading zeros
        $items['qty'] = trim(preg_replace('/(^[0]+)/i', '', $items['qty']));

        // If the quantity is zero or blank there's nothing for us to do
        if ( ! is_numeric($items['qty']) OR $items['qty'] == 0)
        {
            return FALSE;
        }
    
        // Validate the product ID. It can only be alpha-numeric, dashes, underscores or periods
        // Not totally sure we should impose this rule, but it seems prudent to standardize IDs.
        // Note: These can be user-specified by setting the $this->product_id_rules variable.
        if ( ! preg_match("/^[".$this->product_id_rules."]+$/i", $items['id']))
        {
            log_message('error', 'Invalid product ID.  The product ID can only contain alpha-numeric characters, dashes, and underscores');
            return FALSE;
        }
    
        // Validate the product name. It can only be alpha-numeric, dashes, underscores, colons or periods.
        // Note: These can be user-specified by setting the $this->product_name_rules variable.
        // if ( ! preg_match("/^[".$this->product_name_rules."]+$/i", $items['name']))
        //         {
        //             log_message('error', 'An invalid name was submitted as the product name: '.$items['name'].' The name can only contain alpha-numeric characters, dashes, underscores, colons, and spaces');
        //             return FALSE;
        //         }

        // Prep the price.  Remove anything that isn't a number or decimal point.
        $items['price'] = trim(preg_replace('/([^0-9\.])/i', '', $items['price']));
        // Trim any leading zeros
        $items['price'] = trim(preg_replace('/(^[0]+)/i', '', $items['price']));
        
        // Is the price a valid number?
        if ( ! is_numeric($items['price']))
        {
            log_message('error', 'An invalid price was submitted for product ID: '.$items['id']);
            return FALSE;
        }

        if (isset($items['options']) AND count($items['options']) > 0)
        {
            $rowid = md5($items['id'].implode('', $items['options']));
        }
        else
        {
            // No options were submitted so we simply MD5 the product ID.
            // Technically, we don't need to MD5 the ID in this case, but it makes
            // sense to standardize the format of array indexes for both conditions
            $rowid = md5($items['id']);
        }        

        // Now that we have our unique "row ID", we'll add our cart items to the master array
        $old_qty = 0;
        if(isset($this->_cart_contents[$rowid])){
            $old_qty = $this->_cart_contents[$rowid]['qty'];
        }
        
        // let's unset this first, just to make sure our index contains only the data from this submission
        unset($this->_cart_contents[$rowid]);        
        
        // Create a new index with our new row ID
        $this->_cart_contents[$rowid]['rowid'] = $rowid;
    
        // And add the new items to the cart array            
        foreach ($items as $key => $val)
        {
            $this->_cart_contents[$rowid][$key] = $val;
        }
        
        $this->_cart_contents[$rowid]['qty'] += $old_qty;

        // Woot!
        return TRUE;
    }

}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.