Jump to content

Need help outputting shopping cart form a foreach loop


eldan88
Go to solution Solved by Zane,

Recommended Posts

Hey Guys. I crearted a class that goes loops through the session cart and displays the items. The problem that I am facing is when a return the values from a method it only brings back the first item that was added to the cart. It doesn't out out all the values.

 

However when I echo the the values straight from inside the method it shows all the items it looped through. I have tried every possibly thing to have all the items be displayed but I couldn't find to seem a solution.

 

Please note that I skipped a lot of the code in the example below for breviety. Such as instantiation, and other things. There maybe some syntax errors as well. I just did a breif copy and paste

 

 

Any help would be really appreciated!!!

class coreCartFunctions {

protected function DisplayMenuItems($menu_item_id_session){
	  $qty_output =  "<span style=\"font-size:18px\">X{$this->item_qty}</span>";
      $this->menu_item_name = $this->getItems($this->menu_item_id,"menu_item_name");

           if($this->DisplayPrice()){


             $update_order_url ="item-detail.php?update_order=true&menu_item_id={$menu_item_id_session}
             &callback_url={$this->url}&store={$_SESSION['store_name']}";

             $output =  "<a class='{$this->menu_items_class_prop_a}' href='{$update_order_url}'>
             <div class='{$this->menu_items_class}'>{$qty_output} {$this->menu_item_name}</div>";
             $output .= "<div class='menu_item_price'>\${$this->menu_item_price}</div></a>";
             return $output;
             } else {
             $output = "<a class='{$this->menu_items_class_prop_a}' href='#'><div class='menu_item_name'>"
              .$qty_output." ".$this->menu_item_name ."</div></a>";
             return  $output;
          }



}


class displayCartItems extends coreCartFunctions {

public function GetMenuItems() {

foreach ($this->items as $menu_item_id_session) {
 //assign all the properties that belong to core class

 $output = $this->DisplayMenuItems($menu_item_id_session);
return $output
}


}




$display_items =  $display_menu_item->GetMenuItems();
echo $display_items
Link to comment
Share on other sites

Hey Zane! Never mind i got it.  What I did is I initiated the $output  with a $output = "" variable outside the the foreach loop and returned it also outside the foreach loop. Thanks alot man!

P.S i replied back to your email.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.