admin管理员组

文章数量:1434916

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

本文标签: phpHow to display a custom product field value of a specific category on a Woo Commerce checkout page