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?
Related
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?
I am currently using a code similar to what I need, from LoicTheAztec ( who should really be called "DaMan"). That code triggers Woo to send an email to a custom email address when order status is changed to "Processing". Without that code, a notification email only goes to the customer.
For my case I want to send a notification to both the customer, and the store owner. I was able to tweak previous code to do this - but the result was that both the customer and owner get the same email ( as in, they are both copied on exact same notice from Woo).
Here is what I am currently using in my functions.php:
// notify when order status set to processing
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'processing_order_replacement_email_recipient', 10, 2 );
function processing_order_replacement_email_recipient( $recipient, $order ) {
// Set HERE your replacement recipient email(s)… (If multiple, separate them by a coma)
$recipient .= ',example#owneremail.com';
return $recipient;
}
How could I trigger this so that the customer gets their copy, and the owner gets her copy separately? Also typically the stock emails generated by WOO have slightly different text versions between customer and store owner.
If what I am asking can't be done, I'll stick with what I have. Just wondering if there is a cleaner way to achieve it. Thanks in advance for any assistance!
[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
Hopefully there's a woocommerce veteran familiar with the checkout process that can help me with this. My store offers a sample product at a discounted price, but I want to limit this to a one-time order per customer. I've found a solution that works for registered users but I was wondering if there's another way to make this work without forcing guests to set up an account before ordering the product.
What I was thinking was if there's a way to check if the customer's billing email address has ordered this particular product at the checkout stage. So they'd fill out the checkout form and click "Proceed" but before the customer is taken through to the payment gateway their email address would be checked for ordering this particular product before, if so, direct them to a "order failed" page rather than through to the payment gateway.
Thanks in advance!
You have two of ways to achieve this functionality:
You can validate email on checkout page itself. Scan for orders with customer/guest's email address and if found with free product, then validate. This way an unnecessary order will not be recorded in the system.
Here is how you can have custom checkout validation.
/**
* Process the checkout
**/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
global $woocommerce;
// Check if set, if its not set add an error. This one is only requite for companies
if ( $_POST['billing_country'] == "NO" )
if (!$_POST['notes'])
$woocommerce->add_error( __('Please add the required information in "Order notes"') );
}
Alternatively, you can let customer place order and when order is placed, then scan for it if it has already ordered a sample product. And if found, then you can make status of that order as failed. - But this seems a bit illogical.
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.