I am building a PHP $_SESSION array as a user goes thru the shopping process, collecting data about their actions. I want to insert the current cart ID or order ID into the session array so I can tie the two together.
How do I get the cart ID or order iD before checkout, like from inside of woocommerce_ajax_add_to_cart() or something, so as soon as the first product is added to the cart, it also adds the cart ID to the session?
TL;DR how do I get the woocommerce cart ID after the first product is added, and use that to get the final order ID after payment?
Related
I have developed a Wordpress plugin which adds products programmatically using this code:
WC()->cart->empty_cart();
foreach ($productos as $producto) {
WC()->cart->add_to_cart($producto->post_id, $cantidad);
}
After I add products to cart I need to save the cart id, because after the user proceed to checkout and do the payment, I need to save the order id together with the cart id in a custom database table.
Any help, please?
On the other hand, in what database table are the carts and the orders stored?
EDIT (more details)
The actual problem is this. My plugin allows the customer to do some choices before proceeding with the purchase. All those choices are stored in a custom database table I created together with the default WordPress tables.
When the payment was made, I need to save the order id in that custom table in the same record of those choices, so that, in a later time, I will be able to know which choices were set for determined purchase.
Thinking in other way to accomplish this. The table record of choices has an ID (the primary key). Is it possible to save that ID together with the order? If that is possible, that way I can do the match between the order and the choices.
I need to clear the cart when order status changes to Processing. I mean to clear the cart for the customer who made the order.
I've got to create this function:
add_action('woocommerce_order_status_processing','force_clear_the_cart');
function force_clear_the_cart(){
global $woocommerce;
$woocommerce->cart->empty_cart();
}
But I think that this function will clear the cart for the user who update the Order (the shop manager user) and not for the user who made the order (the customer).
Am I right? If so, how can I fetch the user/customer and clear its cart?
I am looking to use WooCommerce in a bit of a strange way, and I'm wondering if there is any way to make this possible. Here's my desired workflow:
Step #1: From a different site subdomain, provide a link to a certain virtual product, but with a query parameter with a unique user id number.
Explanation: The user is at othersite.example.com, and they get a link to buy a product in a WooCommerce store set up with a wordpress site at https://example.com/product/virtual-product
However, this product will be a payment to unlock something on the othersite.example.com site which has its own user and authentication system. (Firebase)
Would it be possible to pass a user id from the othersite.example.com by way of a url query parameter and then have that included in the order info?
ie. From the othersite.example.com someone could be given a link to the product like this https://example.com/product/virtual-product?userid=00000000000000000, with 00000000000000000 being their user id at othersite.example.com.
Then if that userid query value could be included in the order, the following steps should be doable.
Step #2: Have a webhook that fires when the product is purchased, telling a server managing the users for othersite.example.com that the user with userid 00000000000000000 has made a successful purchase of that product.
Is there a way to accept custom values like this to the order? Or is this totally out of the scope of WooCommerce's functionality?
Thanks so much.
I believe that it can be done by using a redirection/link directly to the checkout page, like: "http://yourdomain.com/checkout/?add-to-cart=PRODUCTID&000000" (tested)
In the example, "checkout" stands for the name of your checkout page, "PRODUCTID" stands for the id of desired product, "000000" stands for the user id of redirected person in the other website.
Then you can add a hidden input field to the checkout page (into checkout form, so you will see this value in order) and using the URL you can assign the "000000" (user id) to the value of this field.
I hope that works. If you have any problems with implementation, you can ask me.
- Useful link for hidden input field addition: https://stackoverflow.com/a/42614905/11003615
- Useful link for getting value from current URL using JS: https://web-design-weekly.com/snippets/get-url-with-javascript/
- Useful link for getting value from current URL using PHP/Wordpress Core: https://wordpress.stackexchange.com/questions/274569/how-to-get-url-of-current-page-displayed
- Useful link (contains 4 parts) for adding a custom field to checkout & validating this field & saving this field to order fields & displaying the value of this field in order page: https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-7
[cart] => a:1:{s:32:"**c9f0f895fb98ab9159f51fd0297e236d**";a:10:{s:3:"key";s:32:"**c9f0f895fb98ab9159f51fd0297e236d**";s:10:"product_id";i:8;s:12:"variation_id";i:0;s:9:"variation";a:0:{}s:8:"quantity";i:1;s:13:"line_tax_data";a:2:{s:8:"subtotal";a:0:{}s:5:"total";a:0:{}}s:13:"line_subtotal";i:180;s:17:"line_subtotal_tax";i:0;s:10:"line_total";i:180;s:8:"line_tax";i:0;}}
In woocommerce session cart can the cart key be parsed to get product add to cart time or any other details from this key.
Details can be parsed from this cart key by using php unserialize function. You can view this function documentation here http://php.net/manual/en/function.unserialize.php
I am building a subscription service that will add certain items to a user's cart automatically once per week, without their needing to log in. The problem is that WooCommerce seems to carry cart data in multiple spots, and I'm not sure which can serve as a "master" cart that will take precedence. The persistent cart held in user meta appears to be subservient to session cart data. However, I cannot figure out how to get/set session cart data without actually logging in as the user through a browser.
Should I try to somehow spoof a user login to get access to session variables? Or is there a way to do this directly through the WooCommerce API?
So I figured out that session data is stored as a site option in the options meta, and if I set both the persistent cart AND the session to the same thing, then it will always load the proper information. Here's a snippet that shows how to do this with serialization:
function add_products_programmatically($user_id) {
// Get the current session data and saved cart
$wc_session_data = get_option('_wc_session_'.$user_id);
// Get the persistent cart
$full_user_meta = get_user_meta($user_id,'_woocommerce_persistent_cart', true);
// Create a new WC_Cart instance and add products programmatically
$cart = get_new_cart_with_products();
// If there is a current session cart, overwrite it with the new cart
if($wc_session_data) {
$wc_session_data['cart'] = serialize($cart->cart_contents);
update_option('_wc_session_'.$user_id, $wc_session_data);
}
// Overwrite the persistent cart with the new cart data
$full_user_meta['cart'] = $cart->cart_contents;
update_user_meta($user_id, '_woocommerce_persistent_cart', $full_user_meta);
}
The get_new_cart_with_products() function is just creating a new WC_Cart() object and adding items, then returning the cart object.