exclude "On Hold" orders from Woo Sales reports - wordpress

I want to remove/exclude any orders with the order status "On Hold" from woocommerce sales reports. By default On hold orders are included in the sales reports.
There is a function "exclude_from_order_sales_reports" found here - https://docs.woothemes.com/wc-apidocs/function-wc_register_order_type.html - that will exclude a post type from sales reports.
Also the woo order types and order status are handled here - https://docs.woothemes.com/wc-apidocs/source-function-wc_register_order_type.html#167-221
Unfortunately my php coding is basic (im still learning) and i have no clue where to start with all this code. How can I use the above function to remove on hold order from sales reports?

i seen on woocommerce main report class WC_Admin_Report on line 67 they have filter like this $order_status = apply_filters( 'woocommerce_reports_order_statuses', $order_status );
so i think you can easily alter the default order status for reports by adding filter like this on your functions.php
add_filter( 'woocommerce_reports_order_statuses', 'my_custom_order_status_for_reports', 10, 1 );
function my_custom_order_status_for_reports($order_statuses){
$order_statuses = array('completed'); // your order statuses for reports
return $order_statuses;
}

Related

Woocommerce Get a particular coupon autogenerated over a product purchase

A particular coupon settings assume a coupon instance auto-generation if a customer purchases a certain product. Actually a new coupon instance was generated as a purchase result. I tried to get that coupon instance object, but got an initial coupon instance which was designed as a template for new coupons auto-generation.
a code snippet I used
$coupon_titles = get_post_meta( $download['product_id'], '_coupon_title', true );
for($i=0; $i<count($coupons); $i++ ){
if($coupons[$i]->post_title == $coupon_titles[0]){
$theCoupon = $coupons[$i];
}
}
When I tried to use $coupon_titles[1] array element in assumption that the first index is for the coupon template, I've got an error stating the absence of such the array element. Appreciate for any clue.

Update ACF custom fields for new WooCommerce order

I'm trying to update 2 ACF custom fields every time an order is created.
We have orders coming from our own website and orders coming from an external marketplace integration. My goal is to differentiate the 2 type of orders using custom fields in the order.
The data for the custom fields is provided by an external marketplace integration. The first field has to be filled with the ordernumber (if it it exists) and the other field is just a simple true/false field.
For website orders we're using "Free shipping". For external orders, the provided shipping details are as follows:
Shipping "Verzending voor bol(xxxxx)"
I'm trying to get the shipping method using the woocomerce_new_order action, and using $order->get_shipping_method()
However, when a new order is created, the custom fields won't update.
This is what I've tried so far:
add_action( 'woocommerce_new_order', 'vliegwerk_bolcom_order_filter', 99, 2);
function vliegwerk_bolcom_order_filter( $order_id, $order ) {
$order = wc_get_order( $order_id );
$verzendmethode = $order->get_shipping_method();
$bol_ordernummer = (int) filter_var($verzendmethode, FILTER_SANITIZE_NUMBER_INT);
if ($bol_ordernummer > 0) {
update_field('field_61fa977c8dff9', $bol_ordernummer, $order_id );
update_field('field_61fa95f594b13', 1, $order_id );
}
}
I already tested the woocommerce_new_order trigger, it works as expected.
I've also tried updating the fields using a foreach loop, and triggering the function with a shortcode. This also works as expected.
However, when combining it all together, the fields won't update. Any help would be appreciated!

Automatically Update WooCommerce Order Status after 2 days from "Order Placed" to "Processing"

Is there any way to automatically update the order status of all the "Order Placed" to "Processing" after 2 days. Currently, this can be done manually from Woocommerce < Orders < Change order status.
For example - If a user placed an order it should automatically change status to processing after 2 days.
I've tried this plugin name WunderAutomation, but unable to get the result.
Plugin link - https://wordpress.org/plugins/wunderautomation/
Is there any WooCommerce expert out there who can share a code to change order status automatically?
Thanks for your help
Create a WordPress Cron for two days.
add_filter( 'cron_schedules', 'example_add_cron_interval' );
function example_add_cron_interval( $schedules ) {
$schedules['two_days'] = array(
'interval' => 172800,
'display' => esc_html__( 'Every Two Days' ), );
return $schedules;
}
Create a hook for your function.
add_action( 'bl_cron_hook', 'bl_cron_exec' );
Schedule the hook using the WP Cron we setup above.
if ( ! wp_next_scheduled( 'bl_cron_hook' ) ) {
wp_schedule_event( time(), 'two_days', 'bl_cron_hook' );
}
Create the function that is called in your hook.
function bl_cron_exec() {
// Get list of WC orders.
// Loop over each order.
// Check if order is over two days old
// If older then two days then set status of order.
}
WordPress Cron examples taken from WordPress Cron Handbook. You might want to look at the WC_Order documentation to see how to update an orders status.
This is completely not tested and will need to be adjusted based on specific needs. I don't use WordPress Cron very often, so there might be a thing or two I'm missing.

Woocommerce - Tax optional

Is there a way for the customer to choose whether or not to include the tax in his order?
I imagine a field called "With / Without TAX"
And some code that based on that field, perform the necessary calculations.
If this is not possible, how could you add a button within the order edition to include or exclude taxes?
Thank you.
I did something similar by hooking into woocommerce_checkout_create_order when the order is placed. (Normal orders are taxed on customer's address, curbside orders are based on store address. You can simply use 0 based on if your customer checks a box)
add_action( 'woocommerce_checkout_create_order', array( $this, 'curbside_order'), 10, 1);
public function curbside_order( $order ) {
if( /** without tax - post_meta, checkbox or however you set it**/){
$order->set_cart_tax(0);
}
}

woocommerce calculate price server-side

I need do sell a product whose price depends on a complex calculation over non-discrete parameters set by the customer on the product page, and also on a custom database query result.
How can i calculate the price server-side every time the customer changes parameter-values and apply that price when the customer adds to cart?
i read a similar post whose answer suggests a WC plugin, but even that plugin doesn't satisfy my needs.
Thanks
Probably, you should try to use woocommerce_get_price filter
add_filter('woocommerce_get_price', 'get_dynamically_generated_price', 10, 2);
function get_dynamically_generated_price($price, $product) {
// ... here doing your magic with $price based on $product
// ...
return $price;
}

Resources