A WordPress function to be added to my site that fires with each WooCommerce sale, checks if the order is one of three unique product IDs and then if it is equal to 0 pounds in price it marks the order as complete automatically.

Completed Posted 4 months ago Paid on delivery
Completed Paid on delivery

Automate WooCommerce order completion, if product is one of 3 specific products and the price of the product is 0 pounds in price.

What action should be taken if the order price is not equal to 0 pounds? Do nothing.

The plugin needs to check if every new order is one of three of our unique products, check if the price is 0 pounds and if both are true it needs to change the status to auto complete, this is the code we currently have that does not seem to be firing or working:

add_action( 'woocommerce_new_order', 'custom_autocomplete_order', 10, 1 );

function custom_autocomplete_order( $order_id ) {

if ( ! $order_id ) {

return;

}

// Get the order

$order = wc_get_order( $order_id );

// Check if order total is 0

if ( $order->get_total() === '0' ) {

// Check specific conditions for the products you want to target

$target_product_ids = array( 3388, 150, 2504 ); // Replace with the actual product IDs you want to target

$items_marked_complete = true;

foreach ( $order->get_items() as $item ) {

// Get product id

$product_id = $item->get_product_id();

// Check if the product is in the list of target products

if ( ! in_array( $product_id, $target_product_ids ) ) {

$items_marked_complete = false;

break; // Exit the loop if any item is not a target product

}

}

// Update order status if all conditions are met

if ( $items_marked_complete ) {

$order->update_status( 'completed' );

}

}

}

WordPress PHP WooCommerce Software Architecture MySQL

Project ID: #37683387

About the project

14 proposals Remote project Active 4 mos ago

Awarded to:

moonwebtech

Hello I am expert WordPress developer and I have 10 years experience in web development so please message me for more discussion. I will start work right now Thanks

£15 GBP in 1 day
(354 Reviews)
7.7

14 freelancers are bidding on average £42 for this job

abhi98041

5Hello, my name is Abhishek. I have read your project specifications and this job fits within my area of expertise. I would love to come on board and work on this with you.I am available to begin immediately. Relev More

£70 GBP in 1 day
(471 Reviews)
7.9
jahangirkhan365

Hello Sir I can do this task as listed and deliver this in the next 24 hours at max. I am ready to start right away. Please check my profile for work and experience. Regards Jahangir Khan

£20 GBP in 1 day
(95 Reviews)
6.2
blueforestsun

Hi, I am a WordPress expert. I have carefully read this project. I always do the work like this. I can do it for you. Can you let me check it? Thanks!

£25 GBP in 1 day
(57 Reviews)
6.1
MubiArshad

Hello, I would like to grab this opportunity and will work till you are 100% satisfied with my work. I am a Professional with huge experience in PHP, WordPress, Software Architecture, MySQL, WooCommerce - plus, you c More

£10 GBP in 1 day
(34 Reviews)
5.0
rahulg5466

As a Python Developer for more than 7 years, I've gained solid experience in Web Automation and Software Architecture that is directly applicable to your project. I understand your need for automating WooCommerce order More

£20 GBP in 1 day
(27 Reviews)
4.9
SeoNeha83

Dear Employer, As per project requirement, I can be your best candidate to complete this project on time. You can faith on me and give a chance to me and I will give my 100% to complete your project without any mistake More

£10 GBP in 7 days
(2 Reviews)
0.6