Home › Forums › Backend Issues (wp-admin) › Add ACF fields as Custom Order Line Item Meta › Reply To: Add ACF fields as Custom Order Line Item Meta
Hi there,
I already made something similar but not with ACF datas.
Maybe you could try something like below.
Let me know if it helps.
Rémi
<?php
// Add custom cart item data when a product is added to cart from product page
add_filter( 'woocommerce_add_cart_item_data', 'my_custom_cart_datas', 10, 3 );
function my_custom_cart_datas( $cart_item_data, $product_id, $variation_id ) {
if( !isset( $_POST['new_post_data'] ) ) {
$cart_item_data['new_post_data'] = get_field('name', $order_id);;
}
return $cart_item_data;
}
// Displaying custom cart datas in cart
add_filter( 'woocommerce_get_item_data', 'my_display_custom_cart_datas', 10, 2 );
function my_display_custom_cart_datas( $item_data, $cart_item_data ) {
if( isset( $cart_item_data['new_post_data'] ) ) {
$item_data[] = array(
'name' => __( 'Displayed key', 'my-text-domain' ),
'value' => wc_clean($cart_item_data['new_post_data'])
);
}
return $item_data;
}
// Add custom meta to item (=product) in order (visible in order in back-end)
add_action( 'woocommerce_checkout_create_order_line_item', 'my_custom_order_line', 10, 4 );
function my_custom_order_line( $item, $cart_item_key, $values, $order ) {
if( isset( $values['new_post_data'] ) ) {
$item->add_meta_data(
'_new_item_line_meta',
$values['new_post_data'],
true
);
}
}
//Modify our custom meta key display
add_filter('woocommerce_order_item_display_meta_key', 'filter_wc_order_item_display_meta_key', 20, 3 );
function filter_wc_order_item_display_meta_key( $display_key, $meta, $item ) {
// Change displayed label for specific order item meta key
if( is_admin() && $item->get_type() === 'line_item' && $meta->key === '_new_item_line_meta' ) {
$display_key = __("Displayed key", "my-text-domain" );
}
return $display_key;
}
// Add custom cart item data to emails
add_filter( 'woocommerce_order_item_name', 'my_custom_data_in_email', 10, 2 );
function my_custom_data_in_email( $product_name, $item ) {
if( isset( $item['new_post_data'] ) ) {
$product_name .= sprintf(
'<ul><li>%s: %s</li></ul>',
__( 'Displayed key', 'my-text-domain' ),
esc_html( $item['_new_item_line_meta'] )
);
}
return $product_name;
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.