I have Woocommerce products with "Seat" Variants. I want to achieve the following in the single product page:

  • Check ,if the cart contains any variation already . If "YES" display "You have selected these seats already : (variation names, that are already in cart). If "NO" display "Please add a variation"

I have the following code, but I dont know where should I add this

foreach( WC()->cart->get_cart() as $cart_item ){
    $variation_id = $cart_item['variation_id'];

Please help !

1 Answer 1


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) {

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.

  • I wanted to show on single product page . If any variation is present in cart already ! Commented Sep 3, 2018 at 9:41
  • In Wordpress this is accomplished through the use of hooks. The line add_action( 'woocommerce_before_add_to_cart_form', 'products_in_cart', 5 ); sets up the hook. Whenever Woocommerce displays a single product page, the action 'woocommerce_before_add_to_cart_form' is fired and if there is code associated with the action (as through the add_action) that code is executed. Commented Sep 3, 2018 at 10:09
  • I have added this already .. But the code is silent.. Commented Sep 3, 2018 at 10:17
  • edited answer. Try that. Commented Sep 3, 2018 at 12:21
  • How can I remove the default "View Cart" message when a product is added to cart? Commented Sep 3, 2018 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.