Skip to content
Advertisement

Add and display custom cart item data in WooCommerce orders and emails

I have a custom woocommerce product type called lottery. I needed a custom select field on it (because it’s not a variable product) So I added one.

Everything is working nice i’m getting the value in the cart and in the checkout too, but I cannot get the value in the admin order or in the order mails (clients and admin).

Here is the code that I added in function.php. What am I doing wrong?

function hpplrs_display_custom_field() {
    global $post;
    // Check for the custom field value
    $product = wc_get_product( $post->ID );
    $title = $product->get_meta( 'custom_text_field_title' );
    // display in lottery.php

    
        printf(
            '<div class="col-md-12 small-gap">
                <div class="size_select">
                    <select id="hpplrs-size-select" name="hpplrs-size-select" title="Size" class="size form-control">
                        <option value="" disabled selected>' . __('Select your size', 'hpplrs') . '</option>
                        <option value="option 1">option 1</option>
                        <option value="option 2">option 2</option>
                        <option value="option 3">option 3</option>
                    </select>
                </div>
            </div>',
            esc_html( $title )
        );
        
        
        
}
add_action( 'hpplrs_before_single_product_qty', 'hpplrs_display_custom_field' );

/**
 * Add the text field as item data to the cart object
 * @since 1.0.0
 */
function hpplrs_add_custom_field_item_data( $cart_item_data, $product_id, $variation_id, $quantity ) {
    if( ! empty( $_POST['hpplrs-size-select'] ) ) {
        // Add the item data
        $cart_item_data['title_field'] = $_POST['hpplrs-size-select'];
        $product = wc_get_product( $product_id ); // Expanded function
        $price = $product->get_price(); // Expanded function
    }
    return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'hpplrs_add_custom_field_item_data', 10, 4 );


/**
 * Display the custom field value in the cart
 * @since 1.0.0
 */
function hpplrs_cart_item_name( $name, $cart_item, $cart_item_key ) {
    if( isset( $cart_item['title_field'] ) ) {
      $name .= sprintf(
            '<p class="size"><span>'. __('Size', 'hpplrs') .':</span> %s</p>',
            esc_html( $cart_item['title_field'] )
        );
    }
    return $name;
}
add_filter( 'woocommerce_cart_item_name', 'hpplrs_cart_item_name', 10, 3 );


/**
 * Add custom field to order object
 */
function hpplrs_add_custom_data_to_order( $item, $cart_item_key, $values, $order ) {
    foreach( $item as $cart_item_key=>$values ) {
        if( isset( $values['title_field'] ) ) {
            $item->add_meta_data( _e( 'Size', 'hpplrs' ), $values['title_field'], true );
        }
    }
}
add_action( 'woocommerce_checkout_create_order_line_item', 'hpplrs_add_custom_data_to_order', 10, 4 );

Advertisement

Answer

There are some mistakes in your last function… Instead replace it with the following:

/**
 * Set custom field as visible order item meta data (displayed on orders and email notifications)
 */
add_action( 'woocommerce_checkout_create_order_line_item', 'hpplrs_add_custom_data_to_order', 10, 4 );
function hpplrs_add_custom_data_to_order( $item, $cart_item_key, $values, $order ) {
    if( isset($values['title_field']) && ! empty($values['title_field']) ) {
        $item->add_meta_data( __( 'Size', 'hpplrs' ), $values['title_field'] );
    }
}

Tested and works. Now your custom field will be displayed on orders and email notifications.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement