Forum Replies Created
- AuthorPosts
-
pwnewbie
ParticipantStep 1: Add Data in a Custom Session, on ‘Add to Cart’ Button Click
For those of you who have worked with WooCommerce might know that on the click of the ‘Add to Cart’ button the product page gets refreshed and the user data is lost. Hence, we should add the custom data from our product page to a custom session created using Ajax. This code is invoked before the WooCommerce session is created.
<?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(); }
Step 2: Add Custom Data in WooCommerce Session
At this step, the WooCommerce session has been created and is now available for us to add our custom data. We use the following code to add the custom data from the session we have created into the WooCommerce session. At this step, our session is also unset since the data in it has been captured and it is not needed anymore.
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
At this stage, we have default product details along with the custom data in the WooCommerce session. The default data gets added to the cart object owing to the functionality provided by the plugin. However, we need to explicitly extract the custom data from the WooCommerce session and insert it into the cart object. This can be implemented with the following code.
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
Now that we have our custom data in the cart object all we need to do now is to display this data in the Cart and the Checkout page. This is how your cart page should look after the custom data has been added from the WooCommerce session to your Cart.
My-Cart-Page - AuthorPosts