Good day or night,

I would like to display a custom product field value of a specific category (Shirts) on a woo commerce checkout page. I've succeeded in checking for the category, but my attempts to print the product attribute have failed. Where am I going off track?

add_action('woocommerce_before_calculate_totals', 'personalization_check_category_in_cart');
function personalization_check_category_in_cart() {
    // Set $cat_in_cart to false
    $cat_in_cart = false;

    // Loop through all products in the Cart        
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

        // If Cart has category term, set $cat_in_cart to true
        if ( has_term( 'Personalize', 'product_cat', $cart_item['product_id'] ) ) {
            $cat_in_cart = true;

    if ( $cat_in_cart ) {
        // Print product
        echo '<div class="checkout-personalization-message-container><p>';

        // === ? Print product custom field value here ? ===

        //global $product;
        //echo wc_display_product_attributes( $product );  // doesn't work

        //global $product;
        //$product->get_attribute( 'product_personalization_note' );  // doesn't work

        //get_post_meta( $order->get_id(), 'product_personalization_note', true ); // doesn't work

        echo '</p></div>';

For reference: the code that produced the custom product field value can be found here:

  • Are you getting inside your first If statement? if ( has_term( 'Personalize'... – RiddleMeThis Apr 5 at 16:14

Try something like this, I am unable to test this at the moment.

add_action('woocommerce_before_calculate_totals', 'personalization_check_category_in_cart');

function personalization_check_category_in_cart() {
    // loop through all products in the Cart        
    foreach (WC()->cart->get_cart() as $cart_item) {
        // if cart has category term, set variable $cat_in_cart to true
        if (has_term( 'Personalize', 'product_cat', $cart_item['product_id'])) {
            $product = $cart_item['data']; // get product data
            $product_id = $product->get_id(); // get product ID
            $note = get_post_meta( $product->get_id(), 'product_personalization_note', true ); // get meta
            echo '<div class="checkout-personalization-message-container"><p>' . $note . '</p></div>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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