簡體   English   中英

CodeIgniter購物車類:如何檢索數組上的獨立值?

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

所以我有一個在電子商務網站上使用CodeIgniter的Shopping Cart Class的控制器。

一切正常。 它加載課程,將產品添加到購物車,結帳並完成交易。 但是,當用戶在結帳時,我需要檢索一些信息(例如產品名稱,ID,價格),以將其發送到Mixpanel (這是一種分析工具)。

我已將以下代碼添加到我的結帳控制器:

// 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

有用。 在我的儀表板中,我看到一個特定的用戶激活了“添加到購物車”事件。 但是在此事件的屬性中,我看到了類似的內容(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"}}}

該購物車有2件商品。 “產品1”和“產品2”。 但實際上我應該看到類似以下內容:

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

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

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,
)); 

誰知道我該如何從CI的購物車類中檢索特定數據? 像這樣:

$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,
)); 

我認為這可能真的很簡單,但是(再次)我被卡在這里。

與您顯示購物車時沒有太大不同。 遍歷購物車數組$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'],
    ));
}

否則,請遍歷購物車並創建Mixpanel可以正確處理的新陣列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM