0

I have a mini cart in my template_header.php and I have a cart.php. I am trying to retrieve product picture, product name and price in the mini cart. When I copy the bit of code from cart into the template_header.php, I am able to retrieve the above info but whenever I am looking at an item the product name and price of the privious item added to the cart is being displayed.

Is there any way I can retrieve a php variable in my template_header from my cart without copying any code because I think that will resolve my problem.

Thanks in advance.

wazza
  • 11
  • 3

1 Answers1

0

Yes, use require_once:

/* cart.php */
function getStuff($all = true) {
   echo "Cart WAS HERE"; // Will echo to buffer;
   return [];
}

/* template_header.php */
ob_start(); // Start gathering any output to buffer;

require_once('cart.php');

$output = ob_get_contents(); // Get buffer content;
ob_end_clean(); // Erase any output in buffer;

$products = getStuff();

echo $output; // Will output "Cart WAS HERE";

Difference between include and require

Community
  • 1
  • 1
Justinas
  • 41,402
  • 5
  • 66
  • 96
  • Hello, when I try implement the code above the cart.php is being rendered above my template header and on all the pages. Can you please elaborate on your example. thanks you – wazza Sep 08 '14 at 14:30
  • @wazza Use `ob_*` functions. Updated my answer. – Justinas Sep 08 '14 at 14:43