Okay so is it as simple as creating a function in my plugin
function nfp_add_meta_code() {
$thecode = $_SESSION['code'];
if (!isset($_SESSION['code'])){
exit;
} else {
update_post_meta($post_id, '_code', $thecode);
}
}
and put a hook in the file woo commerce-hooks.php that looks like this
add_action( 'woocommerce_order_status_completed', 'nfp_add_meta_code' );
Not sure why you need to use sessions…You can create a custom form field in the checkout which will hold the identifier and you can hook into the order process ( don’t remember the name of the hook ) and from there you save the custom field identifier to the order post meta.
Using sessions is not reliable in this way.
The form is on the front page of the site.
We store the $_POST data in the $_SEsSioN.
Then we use that data for some display purposes.
So since we have the data why not use it to populate the database?
If you can’t do it via the checkout form, then use transient like I said from before.
Transients are saved in DB so it is more reliable.
what happened to hooking the update_post_meta idea?
Aren’t transients flushed eventually, I’d want to retain the data.
they work together….save transient first then when order is complete, pull the data back out of the transient instead of session…
No transients can be set to not expire or anytime you want…
Okay I save the $_POST data in a transient.
Back to the function and hook . . .
then in the function I use get_transient to retrieve the code instead of recalling the session variable in the function?
Then hook that function into the file woo commerce-hooks.php that looks like this
add_action( 'woocommerce_order_status_completed', 'nfp_add_meta_code' );
yes…if that is the correct hook…as I mentioned i don’t recall the name..
Getting back to transients.
What if I create a transient for clientA
$code=$_POST[‘code’];
set_transient( ‘transientcode’, $code, 60*60*2);
along comes clientB, puts in his code, won’t that overwrite the transient that clientA created?
No because the identifier code is unique right?
$unique_code = $_POST[‘code’];
set_transient( ‘transientcode_’ . $unique_code, $unique_code, 60*60*2 );
OR
set_transient( ‘customer_id’, $unique_code, 60*60*2 );
Assuming the customer id is known.
Sounds good, but I keep getting
Call to undefined function set_transient()
when I try to use it.
It should work fine…Test the function..
echo ( function_exists( 'set_transient' ) ) ? 'yes' : 'no';
what!? That is strange…where in the process are you doing this? Maybe you’re doing this too early in your plugin?
regarding the function_exists check I put this directly in the code that gets called by the original form submission. When does the function get loaded?
== veering off to another part of the thread ==
As a hack I added this to class-wc-checkout.php in the middle of all the other meta fields being saved.
‘$unique_code = $_SESSION[‘clientwebcode’];
update_post_meta( $order_id, ‘_webcode’, $unique_code) ;’
And this works insofar as I now have a postmeta entry with the orderid and the code.
So how to correctly do this so it doesn’t get wiped out by updating.