Checking a guest customer's email address before order is processed to prevent them from purchasing multiple sample products - wordpress

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.

Related

Woocommerce: clear the cart when order status changes to Processing

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?

Send selected order notification emails from admin list in Woocommerce

Would it be possible to add a custom bulk action to orders list in Woocommerce, which would allow for sending of order emails, based on the orders selected in the list? I have found very useful posts here (such as this), describing the procedure to make custom bulk actions, but I am missing the final piece of the puzzle - how to make that custom bulk action send out order notification emails.
I have looked through numerous plugins, but I haven't found anything related to my problem. Any help is appreciated.
Based on the link you supplied you will need to use wp_mail() function in the foreach loop.
This will send an email to each order in the loop.
Source: https://developer.wordpress.org/reference/functions/wp_mail/

Is it possible to pass a special query parameter value to a WooCommerce product link?

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

Trigger email to both customer and store owner when woocommerce order status is set to 'processing'

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!

Code to manually apply an Email restriction on a coupon in woocommerce

So I have an e-commerce site using wordpress and woo-commerce, now using the smart-coupon woocommerce plugin extension is there a way for me to create a credit coupon/gift-card (that initially has no email restriction) and manually assign the email of the current user as the email restriction for it the first time the gift-card is being used.
I'm trying to create a vooucher system using the giftcard as vouchers to purchase products in my store.
Since you don't have any code to show what you were using, try this.
$coupon_code = 'some-code-I-created';
$user = wp_get_current_user();//Get the current user object
$coupon = new WC_Coupon( $coupon_code );//Get the coupon object
$emails = $coupon->get_email_restrictions();//Returns an empty array or array of emails
$emails[] = strtolower($user->billing_email);//Add user's billing email address to the array
$emails = array_filter($emails);//Remove empty values
array_unique($emails);//Remove any duplicate values
$coupon->set_email_restrictions($emails);//Set the coupon's array with the updated array
$coupon->save();//Save the coupon
In case I'm adding email addresses in bulk (like using array_merge() instead of adding one address at a time), it's always good to make sure the array is clean by removing any empty values or duplicate values before updating the coupon object with it.
Also, WooCommerce compares the billing address in all lowercase but not the restricted emails in the coupon, so be sure when adding it to your coupon that you add it as all lowercase so they validate correctly. Source. This bug may be fixed in the future, but until then...

Resources