Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Multi tool use
Clash Royale CLAN TAG#URR8PPP
Store custom data using WC_Cart add_to_cart() method in Woocommerce 3
I am creating a membership site and totally created static pages for each Membership plans (have only 3 plans). However, I have added products for each plan and when I hit SELECT PLAN button I redirect to some custom form where I ask users range of info we are going to use to fulfil the plan (same as sneakertub.com).
I have written code into the PHP page which will handle SUBMIT action of the form. This PHP file, infopage.php
, will process POST data I sent via POST call and stores these all data into WC session.
infopage.php
$customer_name = $_POST["customer_name"];
$customer_email = $_POST["customer_email"];
$customer_sex = $_POST["customer_sex"];
$customer_age = $_POST["customer_age"];
$product_id = $_POST["product_id"];
global $wp_session;
$data = array(
'customer_name' => $customer_name,
'customer_email' => $customer_email,
'customer_sex' => $customer_sex,
'customer_age' => $customer_age);
$wp_session['custom_SESSION_child']=$data;
WC()->session->set('custom_data_child', $data);
//Add product to WooCommerce cart.
WC()->cart->add_to_cart( $product_id )
However, I don't think the above code works. As I don't find values into session with any of the above technique. I have used wp_session
, WC()->session
and $_SESSION
but no approach is working.
wp_session
WC()->session
$_SESSION
I am trying to access these values into functions.php
this way,
functions.php
add_action( 'woocommerce_before_calculate_totals', 'twf_additional_price', 1, 3 );
function twf_additional_price( $cart_object )
global $wpdb;
global $wp_session;
$session_data_2 = $wp_session['custom_SESSION_child'];
$session_data = WC()->session->get('custom_data_child');
var_dump($session_data);
var_dump($session_data2);
foreach ( $cart_object->cart_contents as $key => $value )
$extra_charge = 0;
if(isset($value['twf_user_custom_datas']))
$extra_charge = 100;
$value['data']->set_price($value['data']->price + $extra_charge);
For now ignore the for
loop. Main thing is
for
var_dump($session_data);
var_dump($session_data2);
both dumps only NULL
.
NULL
My main goal is to add the all above fields into Woocommerce checkout and order pages.
Please let me know what is wrong here. I know I might be working on very bad approach but I want Plan selection to checkout process same as sneakertub.com. Please let me know if there is any tutorial on this or proper way to do this. I prefer doing this without plugins but I am ready to use plugins as well.
I appreciate your attention.
1 Answer
1
Updated - Instead of using sessions, you should use the last available argument in WC_Cart
add_to_cart()
method, which will allow you to add any custom cart item data.
WC_Cart
add_to_cart()
For cart item price change based on calculations, is better to make the new price calculation before and to set it in that custom cart item data.
Try the following instead:
1) For your code in the php page:
$custom_data = array(); // Initializing
// Set the posted data as cart item custom data
if( isset($_POST['customer_name']) && ! empty($_POST['customer_name']) )
$custom_data['custom_data']['name'] = sanitize_text_field( $_POST['customer_name'] );
if( isset($_POST['customer_email']) && ! empty($_POST['customer_email']) )
$custom_data['custom_data']['email'] = sanitize_text_field( $_POST['customer_email'] );
if( isset($_POST['customer_sex']) && ! empty($_POST['customer_sex']) )
$custom_data['custom_data']['sex'] = sanitize_text_field( $_POST['customer_sex'] );
if( isset($_POST['customer_age']) && ! empty($_POST['customer_age']) )
$custom_data['custom_data']['age'] = sanitize_text_field( $_POST['customer_age'] );
// Set the calculated item price as custom cart item data
if( isset($custom_data['custom_data']) && sizeof($custom_data['custom_data']) > 0 && $product_id > 0 )
// Get an instance of the WC_Product object
$product = wc_get_product( $product_id );
// Save the new calculated price as custom cart item data
$custom_data['custom_data']['new_price'] = $product->get_price() + 100;
// Add product to cart with the custom cart item data
WC()->cart->add_to_cart( $product_id, '1', '0', array(), $custom_data );
Code goes in function.php file of your active child theme (or active theme). Tested and works.
2) Your revisited function that will change the cart item price:
add_action( 'woocommerce_before_calculate_totals', 'custom_cart_item_price', 30, 1 );
function custom_cart_item_price( $cart )
if ( is_admin() && ! defined('DOING_AJAX') )
return;
foreach ( $cart->get_cart() as $cart_item )
if( isset($cart_item['custom_data']['new_price']) )
$cart_item['data']->set_price( $cart_item['custom_data']['new_price'] );
Code goes in function.php file of your active child theme (or active theme). Tested and works.
All other custom cart item data is available under the cart item key 'custom_data'
as an indexed array… So you will be able to get that data easily from the cart object, to save it in the order.
'custom_data'
one thing I face is I don't get reference for WC() means I can't call the add_to_cart(). I have created .php file somewhere else in my project folder. Would you tell me how can I include these Woocommerce functionalities in my custom .php file?
– Paresh Thakor
Aug 6 at 13:38
@PareshThakor
WC()->cart
is similar to old global $woocommerce;
+ $woocommerce->cart
… So I don't know what to say more than that. I have tested the code of this answer and it works perfectly for me. If you don't get the Woocommerce object trying for example var_dump( WC() );
on your php file project, that means that there is another problem that has nothing to do with this question.– LoicTheAztec
Aug 6 at 13:48
WC()->cart
global $woocommerce;
$woocommerce->cart
var_dump( WC() );
Thank you very much for your help. You really saved me. I created a separate PHP page which was out of Woocommerce sector, now
require_once("../wp-load.php");
made everything working like a charm. Your code was also working fine. Now targeting order Page. I have printed all values on Checkout page, now I need to pass these values to order when user hits Place Order button. Do you have any idea on how to add these values into Order? I am looking some videos or tutorial but I am getting paid Extensions.– Paresh Thakor
Aug 6 at 16:47
require_once("../wp-load.php");
Hey @LoicTheAztec..! I already accepted the answer. Atleast that much credit is obvious. However, let me know if you can help me regarding anything to pass these data into Order form and Admin area. However, I can post question so where I can accept the answer. Let me know about this. Thanks again for the help.
– Paresh Thakor
Aug 6 at 17:14
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thanks @loictheaztec for the response. I am doing it and will respond to you about the results. However, I don't want to update price with my custom data. My custom data will only be informatory things from customers.
– Paresh Thakor
Aug 6 at 11:56