Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save shameemreza/302efe311666252a38aeca6c8542f6c7 to your computer and use it in GitHub Desktop.

Select an option

Save shameemreza/302efe311666252a38aeca6c8542f6c7 to your computer and use it in GitHub Desktop.
Automatically marks renewal orders as completed if all products have one-time shipping enabled. Skips processing status and flags the order as not requiring fulfillment. Helps avoid unnecessary shipping steps for virtual subscription renewals.
/**
* Mark renewal orders as not requiring shipping for subscriptions with one-time shipping enabled.
*/
function wooninja_mark_renewal_orders_virtual( $renewal_order, $subscription ) {
// Check if this is a renewal order
if ( wcs_order_contains_renewal( $renewal_order->get_id() ) ) {
// Get all line items from the order
$items = $renewal_order->get_items();
$needs_shipping = false;
// Check if any products in the renewal order need shipping on renewal
foreach ( $items as $item ) {
$product = $item->get_product();
// Skip if product doesn't exist
if ( ! $product ) {
continue;
}
// If product doesn't have one-time shipping enabled, we need to ship it
if ( ! WC_Subscriptions_Product::needs_one_time_shipping( $product ) ) {
$needs_shipping = true;
break;
}
}
// If all products have one-time shipping, mark the order as completed
if ( ! $needs_shipping ) {
// Add a note to the order
$renewal_order->add_order_note( 'Order marked as not requiring shipping (one-time shipping products only).' );
// This will prevent the order from appearing in the fulfillment queue
$renewal_order->update_meta_data( '_wooninja_no_shipping_required', 'yes' );
$renewal_order->save();
}
}
return $renewal_order;
}
add_filter( 'wcs_renewal_order_created', 'wooninja_mark_renewal_orders_virtual', 10, 2 );
/**
* Skip the processing status for renewal orders that don't need shipping
*/
function wooninja_skip_processing_status_for_renewals( $status, $order_id ) {
$order = wc_get_order( $order_id );
if ( $order && wcs_order_contains_renewal( $order_id ) && 'yes' === $order->get_meta( '_wooninja_no_shipping_required' ) ) {
return 'completed';
}
return $status;
}
add_filter( 'woocommerce_payment_complete_order_status', 'wooninja_skip_processing_status_for_renewals', 20, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment