1

I have a custom plugin that was made by a previous developer. The client has asked for it to be modified to display all items ordered, instead of just one, and for some reason, the item name field is only displaying for the first result.

enter image description here

Here is the code. I'm sure it's something simple I'm overlooking:

    class Purchased_items {
    
    public $return_data;
    
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->EE =& get_instance();
        $this->date_id_field = 'field_id_84';
    }
    
    // ----------------------------------------------------------------

    public function order_count()
    {
        $member = $this->EE->session->userdata('member_id');
        $count = 0;
        if($member)
        {
            $query = $this->EE->db->get_where("channel_titles", array("author_id" => $member, "channel_id" => 7, "status" => "complete"));
            $count = $query->num_rows();
        }
        return $count;
    }

    public function items()
    {
        $id = $this->EE->input->post('search:purchased_id');
        $start = $this->EE->input->post('start_on');
        $end = $this->EE->input->post('stop_before');

        $this->EE->db->save_queries = TRUE;
        $this->EE->db->from('cartthrob_order_items AS oi');
        $this->EE->db->join('channel_titles AS o', 'oi.order_id = o.entry_id', 'inner');
        $this->EE->db->join('channel_data AS od', 'o.entry_id = od.entry_id', 'inner');
        $this->EE->db->join('delivery_dates AS d', 'od.'.$this->date_id_field.' = d.date_id', 'left');
        $this->EE->db->select('oi.*, o.entry_date, d.date');
        if($id !=""){
            $this->EE->db->where(array('oi.entry_id' => $id, 'o.entry_date >=' => strtotime($start), 'o.entry_date <=' => strtotime($end), 'o.status' => 'complete'));
        }else{
            $this->EE->db->where(array('o.entry_date >=' => strtotime($start), 'o.entry_date <=' => strtotime($end), 'o.status' => 'complete'));
        }
        $query = $this->EE->db->get();

        // print_r($this->EE->db->last_query());
        // exit(print_r($query->result_array()));

        $dates = array();

        $item_options = array("default" => array("option_name" => "", "count" => 0, "total" => 0, "total_plus_tax" => 0, "day_totals" => array()));
        $date_counts = array();

        if($query->num_rows() == 0)
        {
            return $this->EE->TMPL->no_results();
        }
        else
        {
            foreach( $query->result_array() as $item )
            {
                $vars[0]["title"] = $item["title"];

                if( $item['extra'] != "" ){
                    $options = unserialize(base64_decode($item["extra"]));
                    $product_option = $options['product_options_other'];
                }
                if( $item['extra'] == "" || $product_option == "" ){
                    $name = "default";
                    $item_options[$name]["count"] += $item['quantity'];
                    $item_options[$name]["total"] += $item['price'];
                    $item_options[$name]["total_plus_tax"] += $item['price_plus_tax'];
                }
                else {
                    $extra = unserialize(base64_decode($item["extra"]));
                    $name = $extra['product_options_other'];
                    if( !isset($item_options[$name]) ){
                        $item_options[$name] = array("option_name" => $name, "count" => 0, "cost" => 0, "total" => 0, "total_plus_tax" => 0, "day_totals" => array());
                    }
                    $item_options[$name]["count"] += $item['quantity'];
                    $item_options[$name]["total"] += $item['price'];
                    $item_options[$name]["total_plus_tax"] += $item['price_plus_tax'];
                }
                if( !isset($item_options[$name]["dates"]) ){
                    $item_options[$name]["dates"] = array();
                }
                if( $item['date'] != "" ){
                    if( !isset($item_options[$name]['day_totals'][$item['date']] )){
                        $item_options[$name]['day_totals'][$item['date']] = array("date"=>$item['date'], "total_for_day" => 0);
                    }
                    $item_options[$name]['day_totals'][$item['date']]['total_for_day'] += $item['quantity'];
                }

            }
            // $vars = array("item_options" => array());
            if( $item_options["default"]["count"] == 0 ){
                unset($item_options["default"]);
            }
            foreach( $item_options as $option )
            {
                $option["day_totals"] = array_values($option["day_totals"]);
                $option["total"] = "$".number_format($option["total"], 2);
                $option["total_plus_tax"] = "$".number_format($option["total_plus_tax"], 2);
                $vars[0]['item_options'][] = $option;
            }
            return $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $vars);
        }
    }
    
}

Any help is appreciated.

Thanks!

2

Your problem is here:

$vars[0]["title"] = $item["title"];

Since that's inside a foreach loop, your resulting data is always limited to a single row (row "0").

Try this, starting at the appropriate matching spot:

if($query->num_rows() == 0)
{
    return $this->EE->TMPL->no_results();
}
else
{
    $vars = array();
    $i = 0;
    foreach( $query->result_array() as $item )
    {
        $vars[$i]["title"] = $item["title"];

        if( $item['extra'] != "" ){
            $options = unserialize(base64_decode($item["extra"]));
            $product_option = $options['product_options_other'];
        }
        if( $item['extra'] == "" || $product_option == "" ){
            $name = "default";
            $item_options[$name]["count"] += $item['quantity'];
            $item_options[$name]["total"] += $item['price'];
            $item_options[$name]["total_plus_tax"] += $item['price_plus_tax'];
        }
        else {
            $extra = unserialize(base64_decode($item["extra"]));
            $name = $extra['product_options_other'];
            if( !isset($item_options[$name]) ){
                $item_options[$name] = array("option_name" => $name, "count" => 0, "cost" => 0, "total" => 0, "total_plus_tax" => 0, "day_totals" => array());
            }
            $item_options[$name]["count"] += $item['quantity'];
            $item_options[$name]["total"] += $item['price'];
            $item_options[$name]["total_plus_tax"] += $item['price_plus_tax'];
        }
        if( !isset($item_options[$name]["dates"]) ){
            $item_options[$name]["dates"] = array();
        }
        if( $item['date'] != "" ){
            if( !isset($item_options[$name]['day_totals'][$item['date']] )){
                $item_options[$name]['day_totals'][$item['date']] = array("date"=>$item['date'], "total_for_day" => 0);
            }
            $item_options[$name]['day_totals'][$item['date']]['total_for_day'] += $item['quantity'];
        }

        if( $item_options["default"]["count"] == 0 ){
            unset($item_options["default"]);
        }
        foreach( $item_options as $option )
        {
            $option["day_totals"] = array_values($option["day_totals"]);
            $option["total"] = "$".number_format($option["total"], 2);
            $option["total_plus_tax"] = "$".number_format($option["total_plus_tax"], 2);
            $vars[$i]['item_options'][] = $option;
        }
        $i++;
    }   
    return $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $vars);
}
| improve this answer | |
  • As always, Derek, you are the best! Thank you! – Ty Morton Dec 3 '13 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.