最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - How to display a custom product field value of a specific category on a Woo Commerce checkout page?

matteradmin7PV0评论

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;
            break;
        }
    }

    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:

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;
            break;
        }
    }

    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:

Share Improve this question edited Apr 5, 2019 at 15:10 hamburger asked Apr 5, 2019 at 15:01 hamburgerhamburger 351 silver badge8 bronze badges 1
  • Are you getting inside your first If statement? if ( has_term( 'Personalize'... – RiddleMeThis Commented Apr 5, 2019 at 16:14
Add a comment  | 

1 Answer 1

Reset to default 1

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>';
        }
    }
}
Post a comment

comment list (0)

  1. No comments so far