i try to slightly modify with +1 check for plugin located here So , for all Virtual Downloadable Free (price=0,00) & on Backorder products I want Woocommerce to set order status ‘Processing’
the result I have with code below – Woocommerce to set order status ‘Pending Payment’ Are there any ideas how to switch it to ‘Processing’:
add_action('woocommerce_checkout_order_processed', 'handmade_woocommerce_order'); function handmade_woocommerce_order( $order_id ) { $order = wc_get_order($order_id); foreach ($order->get_items() as $item_key => $item_values): $product_id = $item_values->get_product_id(); //get product id //get prodct settings i.e virtual $virtual_product = get_post_meta($product_id,'_virtual',true); $downloadable_product = get_post_meta($product_id,'_downloadable',true); $product_backordered=backorders_allowed($product_id,'_backorders',true); $price = get_post_meta($product_id,'_regular_price',true); $virtuald=get_option('hmade_vd'); if($virtuald=='yes' && $downloadable_product=='yes' && $virtual_product=='yes' && $product_backordered=='yes') { if($price=='0.00') { $order->update_status( 'processing' ); } } endforeach; }
Advertisement
Answer
Note 1: use
woocommerce_thankyou
hook instead
Note 2: To know whether a product is virtual or downloadable you can use the following functions
$product->is_virtual();
and$product->is_downloadable();
oppositeget_post_meta();
More info: https://docs.woocommerce.com/wc-apidocs/class-WC_Product.html
Note 3: It is better not to perform operations in a foreach loop, use a check afterwards
function handmade_woocommerce_order( $order_id ) { if( ! $order_id ) return; // Get order $order = wc_get_order( $order_id ); // get order items = each product in the order $items = $order->get_items(); // Set variable $found = false; foreach ( $items as $item ) { // Get product id $product = wc_get_product( $item['product_id'] ); // Is virtual $is_virtual = $product->is_virtual(); // Is_downloadable $is_downloadable = $product->is_downloadable(); // Backorders allowed $backorders_allowed = $product->backorders_allowed(); if( $is_virtual && $is_downloadable && $backorders_allowed ) { $found = true; // true, break loop break; } } // true if( $found ) { $order->update_status( 'processing' ); } } add_action('woocommerce_thankyou', 'handmade_woocommerce_order', 10, 1 );