10

Update (related to author comments):

I would like to customize WooCommerce cart.php to display some meta-data that are working just fine on the product page using Essential Grid premium plugin.

I would like to display some product attributes fields and also some custom meta fields that I have created with the meta field creator of Essential Grid plugin.

For testing, I'm using 'Height' attribute (so 'pa_height') and the custom field 'Age' which slug is 'eg-age-cal'.

Currently, I have tried using the following:

<?php echo get_post_meta($product_id, 'pa_height', true );?>

And also:

<?php echo get_post_meta($product_id, 'eg-age-cal', true );?>

But these does not seem to work.

I have managed to get the code to work using:

<?php echo get_post_meta($product_id, '_regular_price', true );?>

So I know the code is working.

I just need help working out, how can I get the values from these Custom Attribute and Custom Field.

Thanks.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Steven Wright
  • 121
  • 1
  • 1
  • 4

1 Answers1

11

Update (compatibility with WC 3+)

After your explanations in your comment below, I just discover that you are using Essential Grid premium plugin (a commercial plugin) to create some custom fields and attributes related to your wooCommerce products.

At this point, I can't help, because I have never used this plugin before, and I don't know where data is stored within this plugin, in the database.

I think that you can't use usuals WordPress/WooCommerce functions to get this data, and that is the reason that you will not get any data using get_post_meta() as usual…

The best way to get helped is:
- to search/explore your database for that custom fields data.
- to search/ask in Essential Grid plugin authors support threads.


My original answer:

For attributes defined in your products, using get_post_meta() function with the $product_id variable, you need to use it this way to get the data you want (this is an array of values):

// getting the defined product attributes
$product_attr = get_post_meta( $product_id, '_product_attributes' );

// displaying the array of values (just to test and to see output)
echo var_dump( $product_attr );

You can also use the function get_attributes() (more recommended), this way:

// Creating an object instance of the product
$_product = new WC_Product( $product_id );

// getting the defined product attributes
$product_attr = $_product->get_attributes();

// displaying the array of values (just to test and to see output)
echo var_dump( $product_attr );

All code is tested and working.

NOW CART DATA IS SET IN COOKIES AND SESSIONS and you will need to use WC()->cart syntax to get cart data and items

So you can use this kind of code to get the items (products) in cart:

foreach ( WC()->cart->get_cart() as $cart_item ) {
    $product = $cart_item['data'];
    if(!empty($product)){

        // getting the defined product attributes
        $product_attr = $_product->get_attributes();

        // displaying the attributes array of values (just to test and to see output)
        echo var_dump( $product_attr ) . '<br>';
    }
}

This will display the attributes array of values for each product in CART.


A solution based on this thread, using wc_get_product_terms() inside the same code snippet to get your attribute:

foreach ( WC()->cart->get_cart() as $cart_item ) {
    $product = $cart_item['data'];
    if(!empty($product)){

        // compatibility with WC +3
        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

        // Getting "height" product attribute
        $myAttribute = array_shift( wc_get_product_terms( $product_id, 'pa_height', array( 'fields' => 'names' ) ) );
        echo $myAttribute . '<br>';
    }
}

References:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Amazing, thank you! I will check this code out now and see if its what I need :). Apologies for my delayed reply, only just logged back on – Steven Wright Aug 08 '16 at 09:41
  • @StevenWright No problem. If you just can tell me how you get this custom field and how you save it to DB… Thanks – LoicTheAztec Aug 08 '16 at 09:44
  • Sorry, I am a little confused with your code above (I'm only a PHP Beginner) - I added the snippets of code you provided, however, both var_dumps are outputting the exact same arrays, no actual values for the fields (eg height being 123, for example). With the custom field, we ended up having to create this with 'Essential Grid' plugin and use their meta field creator so it would play nicely with their plugin. Thanks for your help so far :) – Steven Wright Aug 08 '16 at 10:08
  • @StevenWright Updated your **question** and my **answer**… As you are using a commercial plugin, it's going to be very difficult for you to get helped here, **because only few people have and use this commercial plugin**. See my updated answer for solutions. – LoicTheAztec Aug 08 '16 at 20:07
  • Same problem with wc_get_product_terms() and custom attributes without commercial plugin. Some products with comma separated attribute value returns empty value. $product->get_attributes() and foreach loop works. Is a bug in this function. – jMike Dec 08 '16 at 17:44
  • What if the attribute has multiple values and you only want the one the customer chose from a dropdown on the product page? For example, I have this as a custom attribute "pa_color" for a simple product: "Black | Brown | Yellow" and the customer chose black and submitted to cart... how do you display that the custom chose **black** in the cart, checkout, and the order emails as the color chosen from the product page? – mayvn May 02 '17 at 22:10