1
votes

In woocommerce, I have maisd wome customizations and I can add a custom text field in my products, Display the values in cart and checkout (see the screenshots below).

Text field in product page:

enter image description here

Value in the cart:

enter image description here

Value in the checkout:

enter image description here

But I can not get it to appear in the purchase details or in the administration section (see the screenshots below).

Checkout details without the value:

enter image description here

Administration orders without the value:

enter image description here

In my code below, could someone tell what I am doing wrong?

The code that I use in my functions.php file:

// Add the field to the product
add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field');

function my_custom_checkout_field() {
global $product;
$id = $product->get_id();


// Get the field name of InputText1
$InputText1Name = get_post_meta($id, 'InputText1', true);

 if ((!empty(get_post_meta($id, $InputText1, true)))){  
echo '<div id="InputText1">';
    echo '<label>'.__($InputText1Name).'</label> <input type="text" name="$InputText1V">';
    echo '</div>';
 }
}


// Store custom field
function save_my_custom_checkout_field( $cart_item_data, $product_id ) {


if( isset( $_REQUEST['$InputText1V'] ) ) {
    $cart_item_data[ '$InputText1V' ] = $_REQUEST['$InputText1V'];
    /* below statement make sure every add to cart action as unique line item */
    $cart_item_data['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
add_action( 'woocommerce_add_cart_item_data', 'save_my_custom_checkout_field', 10, 2 );

// Render meta on cart and checkout
function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ){


// Get the product id inside the cart
foreach( WC()->cart->get_cart() as $cart_item ){
$product_id = $cart_item['product_id'];
} 


// Get the field name of InputText1
$InputText1Name = get_post_meta($product_id, 'InputText1', true);

$custom_items = array();
/* Woo 2.4.2 updates */
if( !empty( $cart_data ) ) {
    $custom_items = $cart_data;
}
if( isset( $cart_item['$InputText1V'] ) ) {
    $custom_items[] = array( "name" => $InputText1Name, "value" => $cart_item['$InputText1V'] );
}
return $custom_items;


}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );


// Display as order meta
function my_field_order_meta_handler( $item_id, $values, $cart_item_key ) {
if( isset( $values['$InputText1V'] ) ) {
    wc_add_order_item_meta( $product_id, "$InputText1V", $values['$InputText1V'] );
}
}



// Update the order meta with field value

add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ($_POST['$InputText1V']) update_post_meta( $order_id, '$InputText1Name', esc_attr($_POST['$InputText1V']));
}

// Update the user meta with field value
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');
function my_custom_checkout_field_update_user_meta( $user_id ) {
if ($user_id && $_POST['$InputText1V']) update_user_meta( $user_id, '$InputText1V', esc_attr($_POST['$InputText1V']) );
}

add_filter( 'woocommerce_hidden_order_itemmeta', 'hide_order_item_meta_fields' );


// Display field value on the order edit page

add_action( 'woocommerce_admin_order_data_after_billing_address',     'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta( $order ){


$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
echo '<p><strong>'.__($InputText1V).':</strong> ' . get_post_meta( $order_id, $InputText1V, true ) . '</p>';
}


function hide_order_item_meta_fields( $fields ) {
$fields[] = 'current_view';
$fields[] = 'custom_image';//Add all meta keys to this array,so that it will not be displayed in order meta box
return $fields;
}

add_action( 'woocommerce_after_order_itemmeta', 'order_meta_customized_display',10, 3 );

function order_meta_customized_display( $item_id, $item, $product ){


$order_product_id = $item['product_id'];
$field1name = get_post_meta($order_product_id, 'InputText1', true);

echo'<br>';
print_r($InputText1V);
echo'<br>';


echo  $field1name;
echo ': ';

}
1
You should ask the question at wordpress.stackexchange.comAA Shakil
@AAShakil wordpress stackexchange is just for pure wordpress (no plugins).LoicTheAztec
@LoicTheAztec Sorry, it's already undeleted. Thank youManuel Orozco Jimenez

1 Answers

4
votes

There are errors and missing things in your code. You don't need all that functions too and you should really avoid to use $ (and if you can capitals) in your custom field slugs (or in key slugs and even in function names) in your code.

I have tested and revisited your code. Here is what you need and can remove everything else:

// Add the field to the product
add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field');
function my_custom_checkout_field() {
    global $product;

    $product_id = $product->get_id();

    // Get the field name of InputText1
    $label = get_post_meta($product_id, 'InputText1', true);

    if( ! empty( $label ) ){
        echo '<div id="InputText1">
            <label>'.$label.':</label> <input type="text" name="custom_slug" value="">
        </div>';
    }
}

// Store custom field label and value in cart item data
add_filter( 'woocommerce_add_cart_item_data', 'save_my_custom_checkout_field', 10, 2 );
function save_my_custom_checkout_field( $cart_item_data, $product_id ) {
    if( isset( $_REQUEST['custom_slug'] ) ) {
        $cart_item_data['custom_data']['label'] = get_post_meta($product_id, 'InputText1', true);
        $cart_item_data['custom_data']['value'] = sanitize_text_field( $_REQUEST['custom_slug'] );
        $cart_item_data['custom_data']['ukey'] = md5( microtime().rand() );
    }
    return $cart_item_data;
}

// Display items custom fields label and value in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );
function render_meta_on_cart_and_checkout( $cart_data, $cart_item ){

    $custom_items = array();
    /* Woo 2.4.2 updates */
    if( !empty( $cart_data ) ) {
        $custom_items = $cart_data;
    }
    if( isset( $cart_item['custom_data'] ) ) {
        $custom_items[] = array(
            'name' => $cart_item['custom_data']['label'],
            'value' => $cart_item['custom_data']['value'],
        );
    }
    return $custom_items;
}


// Save item custom fields label and value as order item meta data
add_action('woocommerce_add_order_item_meta','save_in_order_item_meta', 10, 3 );
function save_in_order_item_meta( $item_id, $values, $cart_item_key ) {
    if( isset( $values['custom_data'] ) ) {
        wc_add_order_item_meta( $item_id, $values['custom_data']['label'], $values['custom_data']['value'] );
    }
}

Code goes in function.php file of your active child theme (or active theme).

Tested and works.

This way you will get the display in Order received, Order view, Edit order (admin) and email notifications…