• Hi,

    I’m trying add custom data into WooComerce orders on my site. Sadly I’m new to WordPress, WooCommerce and PHP so it’s been a struggle to say the least. I found a very good post describing exactly what I wanted here: https://wisdmlabs.com/blog/add-custom-data-woocommerce-order/#comment-261883

    I followed the process and added the following into my …./wp-content/themes/THEMENAME/functions.php

    add_action(‘wp_ajax_wdm_add_user_custom_data_options’, ‘wdm_add_user_custom_data_options_callback’);
    add_action(‘wp_ajax_nopriv_wdm_add_user_custom_data_options’, ‘wdm_add_user_custom_data_options_callback’);

    function wdm_add_user_custom_data_options_callback()
    {
    //Custom data – Sent Via AJAX post method
    $product_id = $_POST[‘id’]; //This is product ID
    $user_custom_data_values = $_POST[‘user_data’]; //This is User custom value sent via AJAX
    session_start();
    $_SESSION[‘wdm_user_custom_data’] = $user_custom_data_values;
    die();
    }

    add_filter(‘woocommerce_add_cart_item_data’,’wdm_add_item_data’,1,2);

    if(!function_exists(‘wdm_add_item_data’))
    {
    function wdm_add_item_data($cart_item_data,$product_id)
    {
    /*Here, We are adding item in WooCommerce session with, wdm_user_custom_data_value name*/
    global $woocommerce;
    session_start();
    if (isset($_SESSION[‘wdm_user_custom_data’])) {
    $option = $_SESSION[‘wdm_user_custom_data’];
    $new_value = array(‘wdm_user_custom_data_value’ => $option);
    }
    if(empty($option))
    return $cart_item_data;
    else
    {
    if(empty($cart_item_data))
    return $new_value;
    else
    return array_merge($cart_item_data,$new_value);
    }
    unset($_SESSION[‘wdm_user_custom_data’]);
    //Unset our custom session variable, as it is no longer needed.
    }
    }

    add_filter(‘woocommerce_get_cart_item_from_session’, ‘wdm_get_cart_items_from_session’, 1, 3 );
    if(!function_exists(‘wdm_get_cart_items_from_session’))
    {
    function wdm_get_cart_items_from_session($item,$values,$key)
    {
    if (array_key_exists( ‘wdm_user_custom_data_value’, $values ) )
    {
    $item[‘wdm_user_custom_data_value’] = $values[‘wdm_user_custom_data_value’];
    }
    return $item;
    }
    }

    add_filter(‘woocommerce_checkout_cart_item_quantity’,’wdm_add_user_custom_option_from_session_into_cart’,1,3);
    add_filter(‘woocommerce_cart_item_price’,’wdm_add_user_custom_option_from_session_into_cart’,1,3);
    if(!function_exists(‘wdm_add_user_custom_option_from_session_into_cart’))
    {
    function wdm_add_user_custom_option_from_session_into_cart($product_name, $values, $cart_item_key )
    {
    /*code to add custom data on Cart & checkout Page*/
    if(count($values[‘wdm_user_custom_data_value’]) > 0)
    {
    $return_string = $product_name . “<dl class=’variation’>”;
    $return_string .= “<table class=’wdm_options_table’ id='” . $values[‘product_id’] . “‘>”;
    $return_string .= “<tr><td>” . $values[‘wdm_user_custom_data_value’] . “</td></tr>”;
    $return_string .= “</table></dl>”;
    return $return_string;
    }
    else
    {
    return $product_name;
    }
    }
    }

    add_action(‘woocommerce_add_order_item_meta’,’wdm_add_values_to_order_item_meta’,1,2);
    if(!function_exists(‘wdm_add_values_to_order_item_meta’))
    {
    function wdm_add_values_to_order_item_meta($item_id, $values)
    {
    global $woocommerce,$wpdb;
    $user_custom_values = $values[‘wdm_user_custom_data_value’];
    if(!empty($user_custom_values))
    {
    wc_add_order_item_meta($item_id,’wdm_user_custom_data’,$user_custom_values);
    }
    }
    }

    add_action(‘woocommerce_before_cart_item_quantity_zero’,’wdm_remove_user_custom_data_options_from_cart’,1,1);
    if(!function_exists(‘wdm_remove_user_custom_data_options_from_cart’))
    {
    function wdm_remove_user_custom_data_options_from_cart($cart_item_key)
    {
    global $woocommerce;
    // Get cart
    $cart = $woocommerce->cart->get_cart();
    // For each item in cart, if item is upsell of deleted product, delete it
    foreach( $cart as $key => $values)
    {
    if ( $values[‘wdm_user_custom_data_value’] == $cart_item_key )
    unset( $woocommerce->cart->cart_contents[ $key ] );
    }
    }
    }

    Sadly however this didn’t appear to do anything? I’ve disabled all my other plugins I had installed. I’ve also tried changing my theme but sadly neither have worked.

    Any help/guidance anyone can provide would be very much appreciated.

    Kind regards
    Paul

    https://www.ads-software.com/plugins/woocommerce/

Viewing 1 replies (of 1 total)
  • You can add custom data on order page like this

    add_action( 'woocommerce_order_details_after_order_table', 'nolo_custom_field_display_cust_order_meta', 10, 1 );
    
    function nolo_custom_field_display_cust_order_meta($order){
        //custom data code here
    }

    to display it after customer details

    add_action( 'woocommerce_order_details_after_customer_details', 'nolo_custom_field_display_cust_order_meta', 10, 1 );
    
    function nolo_custom_field_display_cust_order_meta($order){
        //custom code here
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Adding Custom Data to WooCommerce Order’ is closed to new replies.