1
votes

So I have this controller using CodeIgniter's Shopping Cart Class on an e-commerce website.

It's working fine. It loads the class, adds products to cart, goes to checkout and completes the transaction. But I need to retrieve some information (such as product name, ID, price) when the user is at the checkout to send it to Mixpanel (which is an analytics tool).

I've added to my checkout controller this code:

// Sends subscription information to mixpanel
$this->mixpanel_wrapper->people_set($aluno[0]->aluno_id, array(
        '$first_name'    =>    $student[0]->student_first_name,
        '$last_name'     =>    $student[0]->student_last_name,
        '$email'         =>    $student[0]->student_email,
        ));
$this->mixpanel_wrapper->identify($student[0]->student_id);
$this->mixpanel_wrapper->track_something('Added to cart',  array ($this->cart->contents()));
// Ends mixpanel

It works. In my dashboard I see that a specific user activated the event "Added to Cart". But in the properties of this event I see something like this (the "property" number is added automatically by mixpanel:

Property: 0
{"ee55c5260c7d5fe7fe9bc73b0e0cc82c":{"name":"Product 1","price":"99.00","qty":"1","rowid":"ee55c5260c7d5fe7fe9bc73b0e0cc82c","id":"8","subtotal":99,"options":{"category":"business","teacher":"La Gracia","image":"cozinhando.png","type":"course","description":"Montar uma apresentação é como cozinhar. Se você faz um “catadão” e coloca tudo na panela, sem ordem ou critério, sai uma gororoba. Uma experiência saborosa exige cuidado e atenção na seleção e preparo dos ingredientes. Nesse curso, aprenda"}},"1bebb39e8f44062ff10639f452ea8f8f":{"name":"Product 2","price":"59.00","qty":"1","rowid":"1bebb39e8f44062ff10639f452ea8f8f","id":"7","subtotal":59,"options":{"category":"creativity","teacher":"Pedro Maciel Guimarães","image":"cover_almodovar.png","type":"course","description":"Conheça a evolução das obras de Almodóvar por duas matrizes únicas: a imitação e o intercâmbio de gêneros. Passando por suas comédias e dramas, veremos como Almodóvar pensou e produziu seus diversos trabalhos, desde suas primeiras referências"}}}

There were 2 items on this cart. The "product 1" and "product 2". But in fact I should see something like this:

Property: 0
Name: Product 1
Price: 99.00
Qty: 1
ID: 8

Property: 1
Name: Product 2
Price: 59.00
Qty: 1
ID: 7

What Mixpanel needs is that I convert it into an array like this one to set a new user:

$this->mixpanel_wrapper->people_set($aluno[0]->aluno_id, array(
    '$first_name'       => $aluno[0]->aluno_primeiro_nome,
    '$last_name'        => $aluno[0]->aluno_sobrenome,
    '$email'            => $aluno[0]->aluno_email,
)); 

Anybody knows how can I retrieve specific data from the CI's Shopping Cart Class? Something like this:

$this->mixpanel_wrapper->track_something('User Logged In', array(
    'Name'             => $this->cart->contents->name,
    'Product ID'       => $this->cart->contents->id,
    'Price'            => $this->cart->contents->price,
    'Quantity'         => $this->cart->contents->qty,
)); 

I think it might be really simple, but I'm stucked here (again).

1

1 Answers

2
votes

It would be not much different than when you display the cart. Loop through the cart array, $this->cart->contents(), and handle each item.

foreach ($this->cart->contents() as $item)
{
    $this->mixpanel_wrapper->track_something('User Logged In', array(
        'Name'             => $item['name'],
        'Product ID'       => $item['id'],
        'Price'            => $item['price'],
        'Quantity'         => $item['qty'],
    ));
}

Otherwise, loop through the cart and create a new array that Mixpanel can properly handle.