Mike Rifgin
Mike Rifgin

Reputation: 10745

Augment the Cart class

I'm trying to extend the functionality of the native cart class in CI to add shipping costs. I've begun by adding the MY_Cart.php file to application/core. At the moment it just does this:

class MY_Cart extends CI_Cart {

$CI =& get_instance();

public function __construct() {
    parent::__construct();
}


public function add_shipping() {
       echo 'this function will eventually add shipping';       
}
}

And then in my method that adds an item to the cart I've tried to call the new add_shipping method:

public function add() {
    $data['product'] = $this->Model_products->get_product_by_id($_POST['product_id']);
            $data = array(
           'id'      => $_POST['product_id'],
           'qty'     => 1,
           'price'   => $data['product'][0]->price ,
           'name'    => $data['product'][0]->product_name

        );

       $this->cart->add_shipping();
       $this->cart->insert($data);

         $data['title'] = 'Basket';

      $this->load->view('wrapper-no-cart','basket',$data);
}

But I just get a generic server error. Any ideas what I'm doing wrong? I thought extending the core library would allow me to call the new methods I've written?

Upvotes: 0

Views: 186

Answers (3)

umefarooq
umefarooq

Reputation: 4574

As you are extending the core class I found one catch in code and you can define your class variable with other methods or functions with class methods

class MY_Cart extends CI_Cart {

//    $CI =& get_instance(); worng
    private $CI;

    public function __construct() {
        parent::__construct();
        $this->CI =& get_instance();
    }


    public function add_shipping() {
//wrong        if ($CI->cart->total_items() > 0){
if ($this->total_items() > 0){
            $this->total_items();       
        }
      }
    }

Upvotes: 1

Philip
Philip

Reputation: 4592

Try something like this. Add only the shipping to the TOTAL

if (!defined('BASEPATH'))
   exit('No direct script access allowed');

class MY_Cart extends CI_Cart
{
    public function __construct ( $params = array() )
    {
        parent::__construct ( $params ) ;
    }

    public function shipping($cost='0.3')
    {
        if( !$this->cart->total_items() > 1) return;

        $this->cart->total() =+  ( $this->cart->total() + (float)$cost );
        return $this;
    }

    public function __get ( $name )
    {
        $instance =&get_instance();
        return $instance->{$name};
    }
}

Upvotes: 1

Crowlix
Crowlix

Reputation: 1269

To create your own libraries or extend a native CodeIgniter class, you need to take some steps first. To access CodeIgniter stuff you need to get an instance

$CI =& get_instance();

In your custom class, you use $CI instead of $this. You can read more about it in the link below, it should get you to where you need to go with this class

http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html

Hope this helps, don't hesitate to ask further questions =)

Upvotes: 1

Related Questions