3
  1. I'm adding additional fields to woocommerce, the thing I cant able to store the custom value in the session. I have tried adding the wisdmlabs.com woocommerce customisation code, but cant able to var_dump it the variables.

  2. In functions.php file var_dump is not working.

I'm using woocommerce version 3.2.5. Please somebody help me to add custom data in session and retrieve for cart.

here is the code

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
      var_dump($product_id);
      $user_custom_data_values =  $_POST['Volume']; //This is User custom value sent via AJAX
      session_start();
     $_SESSION['wdm_user_custom_data'] = $user_custom_data_values;
      die();
}

Thanks

Boopathi D
  • 361
  • 2
  • 21
  • @LoicTheAztec Please see the updation. – Boopathi D Dec 14 '17 at 12:15
  • @LoicTheAztec First of all thanks for your kind reply. The thing is I'm getting the products custom field data via modal from user, need to add the data to the cart and make visible in the cart page and mail the data. I'm doing this because need to get the extra data from user and mail to the admin for many products. or else can I use any other plugin to do the functionality. – Boopathi D Dec 14 '17 at 14:35
  • @LoicTheAztec I'm using this theme http://theemon.com/m/megahealth-wp/LivePreview/ I'm using form for getting extra data in modal. after getting all information, the user clicks add to cart button, then all the data is posted for the particular product via ajax. I need that product's info to be stored in session and retrieve for cart page, mail etc. – Boopathi D Dec 15 '17 at 07:04
  • @LoicTheAztec can you please suggest me a link, how to write woocommerce customization in wordpress child theme. – Boopathi D Dec 27 '17 at 08:07
  • There is this thread may be with a lot of links: https://stackoverflow.com/questions/47973325/use-woocommerce-functions-in-custom-php-files – LoicTheAztec Dec 27 '17 at 08:26
  • @LoicTheAztec I deleted my question. Well I dont know woocommerce customisation that too in child theme. Kindly tell me to move forward to customise. By using ajax I am adding the custom field data to cart. How can I check that my custom field data is added to cart. – Boopathi D Dec 27 '17 at 08:28
  • **Updated** - Using this testing function that will display **on cart page** each **cart items raw data**: `add_action( 'woocommerce_before_cart', function(){ foreach( WC()->cart->get_cart() as $cart_item ){ echo '
    '; print_r($cart_item); echo '
    ';} }, 987 );` … I will not be available… I go out now...
    – LoicTheAztec Dec 27 '17 at 08:35

1 Answers1

2

You can add custom data to the Woocommerce session by using the following function:

WC()->session->set( 'cart_id', 'some-example-data' );
gorodezkiy
  • 3,299
  • 2
  • 34
  • 42
  • This answer needs to be removed as it does not work and puts no effort into answering the question. Poor quality answer. – Hybrid web dev Oct 01 '20 at 21:31
  • @Hybridwebdev when I tested it with Woo 3.* it worked for me. If it doesn't work for you them you may want to add details about the environment that you have used to test. – gorodezkiy Oct 14 '20 at 15:26