简体   繁体   English

CodeIgniter购物车类:如何检索数组上的独立值?

[英]CodeIgniter Shopping Cart Class: How to retrieve independent values on an array?

So I have this controller using CodeIgniter's Shopping Cart Class on an e-commerce website. 所以我有一个在电子商务网站上使用CodeIgniter的Shopping Cart Class的控制器。

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). 但是,当用户在结帐时,我需要检索一些信息(例如产品名称,ID,价格),以将其发送到Mixpanel (这是一种分析工具)。

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: 但是在此事件的属性中,我看到了类似的内容(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. 该购物车有2件商品。 The "product 1" and "product 2". “产品1”和“产品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: Mixpanel需要的是将其转换成这样的数组以设置新用户:

$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? 谁知道我该如何从CI的购物车类中检索特定数据? 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). 我认为这可能真的很简单,但是(再次)我被卡在这里。

It would be not much different than when you display the cart . 与您显示购物车时没有太大不同。 Loop through the cart array, $this->cart->contents() , and handle each item. 遍历购物车数组$this->cart->contents() ,并处理每个项目。

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. 否则,请遍历购物车并创建Mixpanel可以正确处理的新阵列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM