2

I have this problem in a plugin, I managed to solve all errors except this one. Here is the original code...

$products = WC()->cart->cart_contents;
$cartTitles = '';
foreach ($products as $product) {
    $cartTitles .= $product['quantity'] . '-' . $product['data']->post->post_title;
}

I'm getting here the typical notice message -

Post was called incorrectly.  Properties should not be accessed directly.

How can I get the post title? I tried with $product['data']->get_post() but it triggers an error.

Thank you.

toonice
  • 2,211
  • 1
  • 13
  • 20

1 Answers1

5

Try using

 $product['data']->get_title();
Vidish Purohit
  • 1,048
  • 11
  • 20
  • 1
    Exactly. The `$product['data']` variable is a `WC_Product` class product object. You need to use getters ex: `$product->get_id()` to get all class properties you used to access directly ex: `$product->id` – helgatheviking May 24 '17 at 15:54