WooCommerce – saving custom data from input fields
-
Hi, after few weeks of searching & fighting with plugins and code, i find this post:
https://wisdmlabs.com/blog/add-custom-data-woocommerce-order/#comment-270770
About adding custom fields and values from them to single-product and order pages in WooAnd after reading this article i add this code to my mytheme/functions.php ;
/*
>>> ADD CUSTOM FIELDS TO SINGLE PRODUCT <<<
============================================== *//* Add the field to the checkout */
add_action( ‘woocommerce_single_product_summary’, ‘my_custom_checkout_field’ );function my_custom_checkout_field() {
global $post;
$terms = wp_get_post_terms( $post->ID, ‘product_cat’ );
foreach ( $terms as $term ) $categories[] = $term->slug;// start if > it will add field only to product category called “c”
if ( in_array( ‘c’, $categories ) )
{
// Set Width
echo ‘<div id=”setWidth”><h2>’ . __(‘Width’) . ‘</h2>’ . ‘</br>’;woocommerce_form_field( ‘setWidth’, array(
‘type’ => ‘text’,
‘class’ => array(‘my-field-class form-row-wide’),
‘label’ => __(‘Your Width (cm.mm)’) . ‘</br>’,
‘placeholder’ => __(‘40.25’)
) );
echo ‘</div>’;
}// end if} // end function
/*
>>> ADD CUSTOM DATA TO SESSION <<<
======================================== */
// Adding AJAX validation
add_action(‘woocommerce_single_product_summary’,’wdm_enque_scripts’);
function wdm_enque_scripts()
{
wp_register_script( ‘wdm_singleProductJs’, plugins_url(‘/wp-content/themes/mkplisy/js/wdm_single_product_page.js’, __FILE__), array(‘jquery’), false, true );
wp_enqueue_script(‘wdm_singleProductJs’);$array_to_be_sent = array( ‘ajax_file_path’ => admin_url(‘admin-ajax.php’));
wp_localize_script( ‘wdm_singleProductJs’, ‘wdm_object’, $array_to_be_sent);
}// Step 1: Add Data in a Custom Session, on ‘Add to Cart’ Button Click
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();
}// Step 2: Add Custom Data in WooCommerce Session
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.
}
}// Step 3: Extract Custom Data from WooCommerce Session and Insert it into Cart Object
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;
}
}// Step 4: Display User Custom Data on Cart and Checkout page
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;
}
}
}// Step 5: Add Custom Data as Metadata to the Order Items
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);
}
}
}// Step 6: Remove User Custom Data, if Product is Removed from Cart
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 ] );
}
}
}And this code should save typed values from input in my order pages,but it doesn’t work, i don’t have any errors, but there is no any values from input saved after “adding product” to cart
Maybe someone have similar problem,or can fix my code??
- The topic ‘WooCommerce – saving custom data from input fields’ is closed to new replies.