Set programmatically woocommerce dynamically generate coupon code usage limit per user 1 - wordpress

Please help me how to set programmatically woocommerce dynamically generate coupon code usage limit per user 1 for guest users.
Any one know solutions for this then please inform me.
https://docs.woocommerce.com/document/create-a-coupon-programatically/
Thanks.

Use following coupon code meta in your custom code:
update_post_meta( $new_coupon_id, 'usage_limit_per_user', '1' );
You can change number value as per your requirements.
Thanks,
Ketan.

Related

WooCommerce ignoring decimal price

I have a WooCommerce 7.0.0 shop for which I'm programmatically creating products.
The price of these products is calculated according to various user input values.
I've done it before on other sites without problem, but I'm currently facing an issue : when the calculated price is a float, on the review order table, the price is not displayed correctly, and I'm getting a notice : A non well formed numeric value encountered in class-wc-cart.php on line 2151
Eg. if the price is 5.24€, I get 5.00€ on the product detail, subtotal, and total of the cart.
However, the price is displayed correctly as 5.24€ in the back-office.
Also, it is stored correctly in database.
Here's 2 methods I've tried to save the price before getting to checkout :
// Method 1
$product->set_price($total);
$product->set_regular_price($total);
$product->save();
// Method 2
add_post_meta($pID, '_regular_price', $total);
add_post_meta($pID, '_price', $total);
I tried various casting and rounding methods before saving (flotval, number_format, sprintf('%.2F') ) to no avail.
I also tried both , and . separators on the WooCommerce settings.
I'm running out of ideas, I must have missed something obvious but as it is, I can't put my finger on it.
Maybe something has changed on the newer versions of WooCommerce ? The last version I've used where I created products in the same way without issue, was 6.3.1.
Any help is welcome :)
Thanks,

Creating WooCommerce placeholder for Emails sent

My client, a courier company, is using delivery software that is able to track with both WooCommerce order no. and ID, which means it requires both order no. and ID ("wc_order_abcdefg") to work, I believe this falls in the phpmyadmin database of post_password column.
Full tracking ID goes like: wc_order_abcdefg.66
So in the WooCommerce email settings, it only has {order_number}. How do I create one for {order_id}?
It looks like, your shipment tracking number is {order_key}.{order_number}.
Once you get hold of the order object, you can get its key simply with: $order->get_order_key().
{order_key} is not available as an email placeholder. So in order to include it in the email sent to the customer, you have different options depending on where you want it to be displayed. For example:
You could include the tracking number in the body of the email. In this case, you can override one of the WooCommerce email templates (e.g. email-order-details.php). See the WooCommerce docs for more info on how to override templates.
You could include the tracking number in the subject of the email. In this case, you can use the woocommerce_email_subject_customer_completed_order filter. E.g. (this snippet should be added to your functions.php):
add_filter( 'woocommerce_email_subject_customer_completed_order', 'add_tracking_number_to_email_subject', 1, 2 );
function add_tracking_number_to_email_subject( $subject, $order ) {
return sprintf( 'Thank you for your order! Your shipment tracking number is: %s.%s', $order->get_order_key(), $order->get_id() );
}

How to get invoice link for a specific order in woocommerce

we are showing user meta data and orders detail as well link to user order through $order-get_order_url(). But i can't find anything like this for invoices. Is it possible to get invoice link through order number or id.
meta data
Thanks and Best regards
Thanks to #Shaamii and his Code above, I figured it out how to display only the invoice link!
In #Shaamii code, the order and all invoices are output as a link.
If you edit the invoice in the backend then this and the final invoice are displayed as a link.
But I only need the last invoice and not the cancellation.
Here is my code:
$actions = wc_get_account_orders_actions( $order );
echo $order_invoice = end($actions)['url'];

Woocommerce - Change tax class programmatically on Order and recalcluate

I´m working on a solution to change the tax class based on the country of the customer AND if he can provide a VAT-ID or not.
The store is EU based and providing services to B2B and B2C.
So for all EU based companies that can provide a VAT-ID, no taxes will be charged unless the origin country is the same.
For all customers who cannot provide a VAT-ID, our local taxes will be added.
And so on...
All solutions I have found are only interacting with the cart, but as I´m creating the order programmatically I need another solution.
Is there a way (a hook maybe) to change the tax_class while creating the order?
For testing I tried to add this method but that did not work. It fires the filter but does not change the tax class in the order.
function wc_change_tax_class( $tax_class, $product ) {
$tax_class = 'Zero rate';
return $tax_class;
}
add_filter( 'woocommerce_product_get_tax_class', 'wc_change_tax_class', 1, 2 );
add_filter( 'woocommerce_product_variation_get_tax_class', 'wc_change_tax_class', 1, 2 );
Also I tried this method in the function that creates the order but with the same result. Nothing happend.
$woocommerce->customer->set_is_vat_exempt( true );
Thanks for any help!
After hours of trying to find the reason, the solution was quite easy:
All solutions I found suggested to write 'Zero rate' or 'Zero Rate'. Depending on what the Name was given in the Woocommerce settings. But for me, it does only work if you write it this way 'zero-rate'. So change the table class name and remove any spaces in the woocommerce settings and of course in the variable.
$tax_class = 'zero-rate';
Hope that saves someone's time!

WooCommerce checkout minimum order based on city

I am new to woocommerce so I need you help to findout the issue i am trying to solve from last 3 days. In short, I have a pizza store at 5 locations. There is a minimum order per location. In checkout I made a custom city select option field based on it total bill will be accepted or rejected. I am using checkout manager. Its really confusing.
So my question is how can I do this and achieve this result. I don't want to hard code everything because In future locations might change.
Any solution for this problem. Thanks In advance.
function check_min_order($order_id) {
global $woocommerce;
$order = new WC_Order($order_id);
$total = $order->calculate_totals();
if (!$total > 'YOUR MINIMUM AMOUNT HERE') {
wc_add_notice( 'You did not meet the minimum order total.', 'error' );
exit;
}
}
add_action( 'woocommerce_checkout_order_processed', 'check_min_order', 1, 1 );
Add this to your functions.php
I might have the $total variable being set using the wrong command. But this should give you a general idea of how to acheive this. It interupts the order process and checks the total to see if it meets your minimum amount. You can create a series of if statements or a switch statement for each case in your form fields. Whatever the name of the request object for the city is you can use $_REQUEST['your_city_field_name'] to get the value.
If the condition is met then it will return a woocommerce formatted error and stop the processing of the transaction. If not it will continue as normal.

Resources