Welcome Guest, Not a member yet? Register   Sign In
MY_Cart to use $_SESSION so no need for DB
#1

[eluser]skunkbad[/eluser]
I've been working on MY_Cart, primarily because I don't want the database activity of the standard cart library. If I edit Cart.php directly, everything works great, but when I try to make MY_Cart.php, it doesn't work. Anybody done this?

The changes are simple. But they only work if I make them directly on Cart.php:

Code:
<?php
class MY_Cart extends CI_Cart {

    function CI_Cart($params = array())
    {
        
        // Are any config settings being passed manually?  If so, set them
        $config = array();
        if (count($params) > 0)
        {
            foreach ($params as $key => $val)
            {
                $config[$key] = $val;
            }
        }
            
        // Grab the shopping cart array from the session table, if it exists
        if (isset($_SESSION['cart_contents']))
        {
            $this->_cart_contents = $_SESSION['cart_contents'];
        }
        else
        {
            // No cart exists so we'll set some base values
            $this->_cart_contents['cart_total'] = 0;
            $this->_cart_contents['total_items'] = 0;
        }
    
        log_message('debug', "Cart Class Initialized");
    }

    function _save_cart()
    {
        // Unset these so our total can be calculated correctly below
        unset($this->_cart_contents['total_items']);
        unset($this->_cart_contents['cart_total']);

        // Lets add up the individual prices and set the cart sub-total
        $total = 0;
        foreach ($this->_cart_contents as $key => $val)
        {
            // We make sure the array contains the proper indexes
            if ( ! is_array($val) OR ! isset($val['price']) OR ! isset($val['qty']))
            {
                continue;
            }

            $total += ($val['price'] * $val['qty']);
            
            // Set the subtotal
            $this->_cart_contents[$key]['subtotal'] = ($this->_cart_contents[$key]['price'] * $this->_cart_contents[$key]['qty']);
        }

        // Set the cart total and total items.
        $this->_cart_contents['total_items'] = count($this->_cart_contents);
        $this->_cart_contents['cart_total'] = $total;
    
        // Is our cart empty?  If so we delete it from the session
        if (count($this->_cart_contents) <= 2)
        {
            unset($_SESSION['cart_contents']);
            
            // Nothing more to do... coffee time!
            return FALSE;
        }

        // If we made it this far it means that our cart has data.
        // Let's pass it to the Session class so it can be stored
        $_SESSION['cart_contents'] = $this->_cart_contents;

        // Woot!
        return TRUE;
    }

    function destroy()
    {
        unset($this->_cart_contents);
    
        $this->_cart_contents['cart_total'] = 0;        
        $this->_cart_contents['total_items'] = 0;        

        unset($_SESSION['cart_contents']);
    }

}
/* End of file MY_Cart.php */
/* Location: .application/libraries/MY_Cart.php */

I ended up just replacing the class, but don't understand why it couldn't just be extended. Anyways, unless I'm overlooking something, this is pretty nice, because I really don't want the database activity.
#2

[eluser]hugle[/eluser]
Hello

I have just one suggestion, did you add,
session_start();
at the beginning of index.php file?
#3

[eluser]skunkbad[/eluser]
I actually use a hook that controls the session, it's regeneration, name, etc. That is where session_start() is called. That isn't the issue. The issue is that I can't extend the Cart class, but replacing it is no problem. The only methods changed are shown above. Nothing drastic in terms of modification, but the modifications only work as a replacement. I guess it really doesn't matter because replacing the Cart class is just as easy as extending it. I just wondered why I can't extend it with the above code, placed in /application/libraries/MY_Cart.php ???
#4

[eluser]Crimp[/eluser]
Those methods are all present in the parent. If you renamed your methods in the class extension, it should work.
#5

[eluser]skunkbad[/eluser]
[quote author="Crimp" date="1255833563"]Those methods are all present in the parent. If you renamed your methods in the class extension, it should work.[/quote]

But if I don't rename the methods, they should override the ones in Cart.php. This is how I have always extended classes in CI, but in this case it isn't working. Could it be because the constructor can't be overridden? I'm not exactly the king of OOP, so this might be an issue, I don't know. There's obviously some technical reason why my class extension above isn't working.
#6

[eluser]Crimp[/eluser]
If the constructor has "final" in front of it, it can't be overridden. It does not in CI. if the child does not have a constructor, the parent constructor is called. If the child does have a constructor, the parent constructor is not called and you have to explicity call parent::__construct(). Maybe there is something in the parent constructor that is missing? Have you made a simple test to see which method is actually called? It could be that the class extension works but the child methods return the wrong result.
#7

[eluser]skunkbad[/eluser]
[quote author="Crimp" date="1255875337"]If the constructor has "final" in front of it, it can't be overridden. It does not in CI. if the child does not have a constructor, the parent constructor is called. If the child does have a constructor, the parent constructor is not called and you have to explicity call parent::__construct(). Maybe there is something in the parent constructor that is missing? Have you made a simple test to see which method is actually called? It could be that the class extension works but the child methods return the wrong result.[/quote]

Yes, simple testing did reveal that the parent constructor is not being overridden by the child constructor.

I figured it out. The constructor of MY_Cart needs to be named MY_Cart, and not CI_Cart. It's not like one of the other methods, because its role in the class is different, and so putting the CI_Cart method in My_Cart just meant I had a standard method named CI_Cart there, and not a constructor to override the constructor in CI_Cart.

If CI was strictly php5, and __construct had been the constructor name for both parent and child, my understanding is that this would have never been an issue.

Crimp, thanks for your help.




Theme © iAndrew 2016 - Forum software by © MyBB