Show custom field value in cart in WooCommerce

I’m using WordPress together with WooCommerce for my shop.
I want to output the value of my custom field in my cart and in the email order confirmation.

I have created a custom field in my functions.php:

Read More
// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

function woo_add_custom_general_fields() {

  global $woocommerce, $post;

  echo '<div class="options_group">';

// Input
woocommerce_wp_text_input( 
    array( 
        'id'                => '_gram', 
        'label'             => __( 'somelabel', 'woocommerce' ), 
        'placeholder'       => '', 
        'description'       => __( 'sometext', 'woocommerce' ),
        'type'              => 'number', 
        'custom_attributes' => array(
                'step'  => 'any',
                'min'   => '0'
            ) 
    )
);

  echo '</div>';

}

function woo_add_custom_general_fields_save( $post_id ){
    // Number Field
    $woocommerce_number_field = $_POST['_gram'];
    if( !empty( $woocommerce_number_field ) )
        update_post_meta( $post_id, '_gram', esc_attr( $woocommerce_number_field ) );

}

On my pages I’m using:

get_post_meta( get_the_ID(), '_gram', true );

To show the value and that work perfect.

Now I want to show this same value under the product name in my cart and in the email confirmations.
But I cant figure out how to do this.

Does anyone knows how to do this?

Related posts

Leave a Reply

1 comment

  1. Well it’s a long process, you have to implement two filters & one action to accomplish this.

    Also there is a plugin for this exact purpose along with lot of other options related to woocommerce custom fields.

    Here is the direct solution for your question.

    /**
     * Here we are trying to add your custom data as Cart Line Item
     * SO that we can add this custom data on your cart, checkout, order and email later
     */
    function save_custom_data( $cart_item_data, $product_id ) {
        $custom_data = get_post_meta( $product_id, '_gram', true );
        if( $custom_data != null && $custom_data != ""  ) {
            $cart_item_data["gram"] = $custom_data;
        }
        return $cart_item_data;
    }
    add_filter( 'woocommerce_add_cart_item_data', 'save_custom_data', 10, 2 );
    
    /**
     * Here we are trying to display that custom data on Cart Table & Checkout Order Review Table 
     */
    function render_custom_data_on_cart_checkout( $cart_data, $cart_item = null ) {
        $custom_items = array();
        /* Woo 2.4.2 updates */
        if( !empty( $cart_data ) ) {
            $custom_items = $cart_data;
        }
        if( isset( $cart_item["gram"] ) ) {
            $custom_items[] = array( "name" => "Gram", "value" => $cart_item["gram"] );
        }
        return $custom_items;
    }
    add_filter( 'woocommerce_get_item_data', 'render_custom_data_on_cart_checkout', 10, 2 );
    
    /**
     * We are adding that custom data ( gram ) as Order Item Meta, 
     * which will be carried over to EMail as well 
     */
    function save_custom_order_meta( $item_id, $values, $cart_item_key ) {
        if( isset( $values["gram"] ) ) {
            wc_add_order_item_meta( $item_id, "Gram", $values["gram"] );
        }
    }
    add_action( 'woocommerce_add_order_item_meta', 'save_custom_order_meta', 10, 3 );