title: WooCommerce get product custom meta from cart
body: <p>In my WooCommerce shop customers can only buy one product. 
  They can add this single product multiple times to the cart, but with different custom meta values. 
  Customers are able to edit their products from the cart (WooCommerce default functionality) and change the meta they initially filled in.</p>
  
  <p>In my <code>functions.php</code> I have the following code:</p>
  
  <pre><code>function add_names_on_tshirt_field() {   
      global $post;
      $name_one_on_tshirt = "";
      $name_two_on_tshirt = "";
      $name_three_on_tshirt = "";
  
      foreach ( WC()-&gt;cart-&gt;cart_contents as $key =&gt; $value ) {
          if( $value["product_id"] == $post-&gt;ID &amp;&amp; isset( $value["name_one_on_tshirt"] ) ) {
              $name_one_on_tshirt = $value["name_one_on_tshirt"];
          }
          if( $value["product_id"] == $post-&gt;ID &amp;&amp; isset( $value["name_two_on_tshirt"] ) ) {
              $name_two_on_tshirt = $value["name_two_on_tshirt"];
          }
          if( $value["product_id"] == $post-&gt;ID &amp;&amp; isset( $value["name_three_on_tshirt"] ) ) {
              $name_three_on_tshirt = $value["name_three_on_tshirt"];
          }
      }
  
      echo '&lt;table class="variations" cellspacing="0"&gt;
        &lt;tbody&gt;
            &lt;tr&gt;
              &lt;td class="label"&gt;&lt;label for="color"&gt;Name 1 on T-Shirt&lt;/label&gt;&lt;/td&gt;
              &lt;td class="value"&gt;
                  &lt;input type="text" name="name-one-on-tshirt" value="'. esc_attr( $name_one_on_tshirt ) .'" /&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td class="label"&gt;&lt;label for="color"&gt;Name 2 on T-Shirt&lt;/label&gt;&lt;/td&gt;
              &lt;td class="value"&gt;
                  &lt;input type="text" name="name-two-on-tshirt" value="'. esc_attr( $name_two_on_tshirt ) .'" /&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td class="label"&gt;&lt;label for="color"&gt;Name 3 on T-Shirt&lt;/label&gt;&lt;/td&gt;
              &lt;td class="value"&gt;
                  &lt;input type="text" name="name-three-on-tshirt" value="'. esc_attr( $name_three_on_tshirt ) .'" /&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
        &lt;/tbody&gt;
      &lt;/table&gt;';
  }
  add_action( 'woocommerce_before_add_to_cart_button', 'add_names_on_tshirt_field' );
  </code></pre>
  
  <p>The problem lies in when my customers try to edit a product from the cart. Because all products have the same ID (I only have one product), the meta from the last product added to the cart is loaded into the text fields. I currently retrieve the meta in my <code>foreach</code> using the product id, but the ID is the same as all other products in my cart. Is there another way to retrieve the meta without using the <code>product_id</code>/<code>$post-&gt;ID</code>, like the <code>cart_item_key</code> or using sessions?</p>