WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
[resolved] custom order data into database (44 posts)

  1. jimlongo
    Member
    Posted 1 year ago #

    I have a custom $_SESSION variable.
    I want to store it in the database upon order completion.

    Either as a new column in the posts table, or an additional row in the posts_meta table (any thoughts on which is better). Or I suppose it could be an additional table in the database.

    How do I insert that data from my session variable into the database?

    Thanks for any help, I'm pretty new to WooCommerce.

    http://wordpress.org/extend/plugins/woocommerce/

  2. Roy Ho
    Member
    Posted 1 year ago #

    I can tell you the best approach however I would first need to know what you're doing and why you're doing it. What is in the data...etc. Once I know that, I can better give you my suggestion.

  3. jimlongo
    Member
    Posted 1 year ago #

    Thanks,

    Each product is unique to that client.

    Let's say it's a photo of them. They are given a Number at the time it's taken which they enter into a form on the website.

    I capture the number into a variable from a form, and they can now see it on the website and buy it. (the number is parsed and let's us get the photo from a CDN to display on the website)

    After they buy it I need to identify which photo they purchased. Hence the need to capture the session variable into the database for further use in the order emails and admin order details, etc.,

  4. Roy Ho
    Member
    Posted 1 year ago #

    hmmmm...bare with me as I try to understand this...each order is already unique to a customer so why do you need an extra number?

  5. jimlongo
    Member
    Posted 1 year ago #

    To identify which photo belongs to their order. The only identifier is the session variable I've captured. Otherwise it could be any of 1000's of photos they just purchased.

  6. Roy Ho
    Member
    Posted 1 year ago #

    Ok well I still don't understand what you need that for because why would they buy 1000 photos that is not their own?

    But anyways, to answer your question in general, use the transient API instead of sessions.

  7. jimlongo
    Member
    Posted 1 year ago #

    Of course they wouldn't. But I need to be able to identify which picture they purchased. Right now all I know is that Joe Brown, address, etc., bought a photo. Which one did he buy?

  8. Roy Ho
    Member
    Posted 1 year ago #

    I thought the photo IS the product....

  9. jimlongo
    Member
    Posted 1 year ago #

    Yes but there is only 1 product for sale right now. A printed photo (otherwise there would be 10 of 1000's of products and growing by the minute).

    What is unique is that your photo is different from mine.

    So if you buy the product, I need to know the variable that identifies it as your photo.

    BTW, thanks for your patience in trying to help.

  10. Roy Ho
    Member
    Posted 1 year ago #

    I know what you're saying but to me 1 photo is 1 product. So it is already unique. so even if you have 10,000 photos, that is 10,000 unique products...

    And you can purge them after a certain time, so your product base will not get too big...

    Or am I missing something? Perhaps you didn't fully explain how these photos come about and how they are entered into the system.

  11. jimlongo
    Member
    Posted 1 year ago #

    The products aren't ever entered into my system. There's no process for that.

    I use the identifier to grab the image from the CDN to display on the product page. But there is only 1 product page.

  12. Roy Ho
    Member
    Posted 1 year ago #

    Ok...Then the easier way is to use that identifier and save that along with the order as a post meta. Because an order in WooCommerce is a custom post type as "shop_order". This way the order will always be tied to that unique identifier and you won't need to deal with sessions.

  13. jimlongo
    Member
    Posted 1 year ago #

    Yes that's what I was thinking - add it to wp_post meta.
    My question is how to go about that. I'm not that familiar with WP, do I need to create an action? And I can't see any examples of how to insert data into the database.

    i was looking at the woocommerce_order_status_completed hook, I'm not sure how best to proceed.

  14. Roy Ho
    Member
    Posted 1 year ago #

    In the hook just find the unique post id from the order and from there you can use the update_post_meta function.

    So something like:

    update_post_meta( $order_id, '_unique_identifier', $photo_id );

  15. jimlongo
    Member
    Posted 1 year ago #

    Are you talking about the function http://codex.wordpress.org/Function_Reference/add_post_meta

    Not sure where it goes or how to use it, but it looks promising

  16. Roy Ho
    Member
    Posted 1 year ago #

    Use update_post_meta instead...You use it in the hook of the completed order.

  17. jimlongo
    Member
    Posted 1 year ago #

    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' );

  18. Roy Ho
    Member
    Posted 1 year ago #

    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.

  19. jimlongo
    Member
    Posted 1 year ago #

    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?

  20. Roy Ho
    Member
    Posted 1 year ago #

    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.

  21. jimlongo
    Member
    Posted 1 year ago #

    what happened to hooking the update_post_meta idea?

    Aren't transients flushed eventually, I'd want to retain the data.

  22. Roy Ho
    Member
    Posted 1 year ago #

    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...

  23. jimlongo
    Member
    Posted 1 year ago #

    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' );

  24. Roy Ho
    Member
    Posted 1 year ago #

    yes...if that is the correct hook...as I mentioned i don't recall the name..

  25. jimlongo
    Member
    Posted 1 year ago #

    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?

  26. Roy Ho
    Member
    Posted 1 year ago #

    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.

  27. jimlongo
    Member
    Posted 1 year ago #

    Sounds good, but I keep getting
    Call to undefined function set_transient()
    when I try to use it.

  28. Roy Ho
    Member
    Posted 1 year ago #

    It should work fine...Test the function..

    echo ( function_exists( 'set_transient' ) ) ? 'yes' : 'no';
  29. jimlongo
    Member
    Posted 1 year ago #

    it returns 'no'.

  30. Roy Ho
    Member
    Posted 1 year ago #

    what!? That is strange...where in the process are you doing this? Maybe you're doing this too early in your plugin?

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic