Q: Loop through Woocommerce cart items for a tracking code I'm trying to loop through every product on the cart page in WooCommerce in order to set some objects in a JavaScript tracking code that should print out the ID and the amount of products added to the cart in this object.
I've used some examples from StackOverflow to reach the products and also loop through them and create JavaScript objects, but I'm getting an error saying:

post was called incorrectly. Product properties should not be accessed
  directly

This is the code I'm using in the functions.php:
<?php }
    if (is_cart()) { ?>
        <?php 
   global $woocommerce;
   $items = $woocommerce->cart->get_cart();
   $product_names=array(); ?>
    <script>
        var options = [
        <?php foreach ($items as $item => $values) { 
            $_product = $values['data']->post;?> 
            {productid: "<?php echo $_product->ID; ?>"},
        <?php
        }
        ?>
        ]
    </script>
   <?php      
    }
}

I think that the way I'm reaching for the products is deprecated. Does anyone know how to do this the correct way with WooCommerce nowadays? Also if you know how to actually get the amount of the product added to the cart (i.e. how many items that are added of each product), that would be very appreciated.

A: There is some mistakes in your code… Try the following instead:
if (is_cart()) { 
    $product_names=array(); 
?>
<script>
    var options = [
    <?php foreach ( WC()->cart->get_cart() as $cart_item ) : ?> 
        {productid: "<?php echo $cart_item['data']->get_id(); ?>"},
    <?php endforeach; ?>
    ]
</script>
<?php      
}

It should better work without errors.

Note: global $woocommerce is replaced by WC() since a while. There is no anymore post object included in the WC_Product Object since Woocommerce 3.0

