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.
£10-20 GBP
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' );
}
}
}
Project ID: #37683387
About the project
Awarded to:
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
14 freelancers are bidding on average £42 for this job
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
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!
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
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