Get product custom field values as variables in WooCommerce Get product custom field values as variables in WooCommerce wordpress wordpress

Get product custom field values as variables in WooCommerce


First $product->ID will not work any way. So your problem is how to get the product ID.

Multiple cases

1) In Archive pages like shop or in Single product pages:

  • Using get_the_id() will give you mostly always the product id (the post ID)

    $room = get_post_meta( get_the_id(), 'room', true );
  • You can also use global $product; with $product->get_id() if $product is not defined.

    global $product;$room = get_post_meta( $product->get_id(), 'room', true );
  • You can also use global $post; with $post->ID (if $post is not defined):

    global $post;$room = get_post_meta( $post->ID, 'room', true );

2) for cart items (from the cart object):

// Loop through cart itemsforeach( WC()->cart->get_cart() as $cart_item ){    // Get the WC_Product object (instance)    $product = $cart_item['data'];    $product_id = $product->get_id(); // get the product ID    $room = get_post_meta( $product->get_id(), 'room', true );    ## … / … }
  • Now for product variations, if you need to get the parent variable product ID, you will use:

    // Get the parent product ID (the parent variable product)$parent_id = $cart_item['product_id'];$room = get_post_meta( $parent_id, 'room', true );## … / … 

3) For order items (from a defined WC_Order object $order):

// Loop through cart itemsforeach( $order->get_items() as $item ){    // Get the Product ID    $product_id = $item->get_product_id();    $room = get_post_meta( $product_id, 'room', true );    ## … / … }
  • Now for product variations, if you need to get the parent variable product ID, you will use:

    // Loop through cart itemsforeach( $order->get_items() as $item ){    // Get the parent Product ID    $product = $item->get_product();    $parent_id = $product->get_parent_id(); // The parent product ID    $room = get_post_meta( $parent_id, 'room', true );    ## … / … }

So it's up to you now.