Woocommerce list variations that are added already to cart in Single Product

Step 1 is going to be getting the parent ID of the product.

Next step is to loop through the cart and check if each product in the cart is a variation of the parent (will show up as $cart_item->product_id) and save the attribute information in another array. There is a question in the attributes as it changes by installation and depends on how the product is set up. For example: if you have an attribute ‘color’, it will show up in the cart item in $cart_item->variation->attribute_pa_color. You’ll need to query your database in the wp_postmeta table for the right meta key. Another option is to just save everything in the variation element of the cart item.

Finally, loop through the results and display them.

Now as for where to put it, technically, it goes into the functions.php file of your theme. You should probably be using a child theme which is another discussion. WooCommerce uses hooks to add or extend functionality and which hook you use depends on where you want this to display. Business Bloomer has an excellent visual hooks guide with a post for the product page. As an example, if you wanted to put this list under the short description, you would need this in functions.php (I like to put these near the top)…

add_action( 'woocommerce_before_single_product_summary', 'products_in_cart', 5 );

The code refers to a function products_in_cart(). This can go anywhere in functions.php (except inside another code block!) and I usually put these on the bottom…

function products_in_cart() {
  $parent = get_the_id(); // the parent product
  $products_in_cart = array();
  $cart_items =  WC()->cart->get_cart();
  foreach( $cart_items as $cart_item ){
    if ($cart_item['product_id'] == $parent) {
      $products_in_cart[] = $cart_item['variation']; // possibly array('attribute_pa_color'=>'black')
    }
  }
  if (count($products_in_cart) > 0) {
    echo('You have selected these seats already:<br>');
    foreach($products_in_cart as $product) {
      foreach($product as $key=>$val) {
        echo($val.'<br>');
      }
    }
  }
}

DISCLAIMER: I’m sharing this code as is. I’ve just done something similar so it works in theory but will almost certainly need to be adjusted to your installation and your needs.