5

I have a custom plugin that allows the customer to add custom information to their order.

The item is added to cart and the custom data displayed on the cart page. However, the custom information does not get carried over to the orders page on the back end. Ideally I would also like the custom data to be added to the customers order email.

The current code is as follows:

<?php
function wcpc_save_custom_product_field( $cart_item_data, $product_id ) {
    if( isset( $_REQUEST['wcpc_custom_product'] ) ) {
        $cart_item_data[ 'wcpc_custom_product' ] = $_REQUEST['wcpc_custom_product'];
        $cart_item_data[ 'wcpc_custom_price' ] = $_REQUEST['wcpc_custom_price'];
        /* 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', 'wcpc_save_custom_product_field', 10, 2 );

function render_meta_on_cart_and_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['wcpc_custom_product'] ) &&  $cart_item['wcpc_custom_product'] != '' ) {
        $custom_items[] = array( "name" => 'Custom', "value" => $cart_item['wcpc_custom_product'] );
    }
    return $custom_items;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

function add_custom_price( $cart_object ) {

//  This is necessary for WC 3.0+
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    foreach ( $cart_object->get_cart() as $key => $value ) {
        if(isset($value['wcpc_custom_price'])) {
            $value['data']->set_price( $value['wcpc_custom_price'] );
        }
    }

}
?>

I have tried modifying a code snippet I found online and adding to the above code. However, when I implement this, the cart breaks altogether:

function wcpc_order_item_product( $cart_item, $order_item ){

    if( isset( $order_item['wcpc_custom_product'] ) ){
        $cart_item_meta['wcpc_custom_product'] = $order_item['wcpc_custom_product'];
    }

    return $cart_item;

}
add_filter( 'woocommerce_order_item_product', 'wcpc_order_item_product', 10, 2 );

Any help would be greatly appreciated. I don't have too much coding experience and I am struggling to find a way to get this to work.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Tostino
  • 73
  • 1
  • 1
  • 6

2 Answers2

5

The hook woocommerce_add_order_item_meta is going to be deprecated soon. Since Woocommerce 3 a better hook is available. Try this:

add_action( 'woocommerce_checkout_create_order_line_item', 'custom_checkout_create_order_line_item', 20, 4 );
function custom_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
    if( ! isset( $values['wcpc_custom_product'] ) ) return;

    if( ! empty( $values['wcpc_custom_product'] ) )
        $item->update_meta_data( 'Custom label', $values['wcpc_custom_product'] );

}

enter image description here

You will have to replace 'Custom label' by the label you want to be displayed with the value…

This way your custom field will be displayed everywhere, on backend and frontend orders and email notifications.

See this related thread that will give you all explanations:
Woocommerce: which hook to use instead of deprecated "woocommerce_add_order_item_meta"

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi Loic, many thanks for your reply. Your posts have helped me greatly in the past! Unfortunately when I copy/paste the above code in it results in the cart breaking completely. Do you have any thoughts as to where I'm going wrong? – Tostino May 21 '18 at 13:22
  • Hi Loic, please ignore the last post! It works now I upgraded Woocommerce. However, another issue has been created, now I am on the latest version my categories are duplicated, as can be seen here: http://aurello.co.uk/product-category/splashbacks/ Do you know how I can hide/remove one set of categories so only one is displayed? – Tostino May 21 '18 at 13:36
  • this is just on the front end of the site - no issue with duplication on the back end – Tostino May 21 '18 at 14:05
1

You can use wc_add_order_item_meta() for save the custom meta in order and backend

for example ::

add_action('woocommerce_add_order_item_meta','add_values_to_order_item_meta',1,2);
if(!function_exists('add_values_to_order_item_meta'))
{
  function add_values_to_order_item_meta($item_id, $values)
  {
        global $woocommerce,$wpdb;
        $user_custom_values = $values['user_custom_data_value'];
        if(!empty($user_custom_values))
        {
            wc_add_order_item_meta($item_id,'user_custom_data',$user_custom_values);  
        }
  }
}

From your edited code.

add_action('woocommerce_add_order_item_meta','add_values_to_order_item_meta',1,2);
if(!function_exists('add_values_to_order_item_meta')) 
{ 
    function add_values_to_order_item_meta($item_id, $values) 
        { 
            global $woocommerce,$wpdb;
            $user_custom_values = $values['wcpc_custom_product'];
            if(!empty($user_custom_values)) {
            wc_add_order_item_meta($item_id,'wcpc_custom_product',$user_custom_values); }
        } 
}
Vaibhav Kumar
  • 768
  • 4
  • 12
  • thanks for your reply. Would I need to edit this code to make it compatible with my plugin? I tried doing a quick copy/paste job. The cart is still working but the custom data is not showing on the orders page. – Tostino May 21 '18 at 10:32
  • you have to edit this according to your need , this is just an example. – Vaibhav Kumar May 21 '18 at 10:34
  • reference link https://stackoverflow.com/questions/45363969/adding-custom-meta-data-from-products-to-orders-items-in-woocommerce?rq=1 – Vaibhav Kumar May 21 '18 at 10:46
  • thanks! Ok so I modified the code to: add_action('woocommerce_add_order_item_meta','add_values_to_order_item_meta',1,2); if(!function_exists('add_values_to_order_item_meta')) { function add_values_to_order_item_meta($item_id, $values) { global $woocommerce,$wpdb; $user_custom_values = $values['wcpc_custom_product']; if(!empty($user_custom_values)) { wc_add_order_item_meta($item_id,'wcpc_custom_product',$user_custom_values); } } } – Tostino May 21 '18 at 11:16
  • It works, but when the customer reaches the Order Details page, the following is displayed:Product Total test × 1 wcpc_custom_product: Width: 50, Length: 50, Brackets: , £0.00 – Tostino May 21 '18 at 11:18
  • Do you know how to hide: wcpc_custom_product – Tostino May 21 '18 at 11:19
  • try to hide from css – Vaibhav Kumar May 21 '18 at 11:24