I am trying to create a simple bulleted list of items that have already been added to the cart, which will later be passed through a gravity form field. Here is what I have come up with so far:
<?php global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $product_id) { ?>
<li><?php echo $item; ?></li>
<?php } ?>
I have 2 items added to the cart, and get a result of:
However, I am trying to grab the title of the products rather than the key.
You need to do this way:
<?php global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) { ?>
<? // print_r($item); ?>
<? $_product = $values['data']->post; ?>
<? echo $_product->post_title; ?>
<?php } ?>
You can access then to any post value, post_title, post_date, ID, etc. Uncomment the // print_r($item); and see what i mean. Also you can play with other data changing a bit the part with: $_product = $values['data']->post; For example to access the product ID, not the post ID, change the inside of foreach function to this:
$_product = $values['data'];
echo $_product->ID;
Print $_product to see the rest of values available.
In fact, always do a print_r to see what´s inside an array.
That´s it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With