Tag: WooCommerce Subscriptions

  • WooCommerce and WooCommerce Subscriptions – Automatically set order status to completed

    WooCommerce and WooCommerce Subscriptions – Automatically set order status to completed

    This post is an update onWooCommerce – Automatically set order status after payment is received

    WooCommerce is a great option to sell all type of goods, being it virtual or physical products you’ll quickly add them to your store and start selling to your visitors.

    By default when WooCommerce system processes a payment it’ll set an order with only virtual and downloadable products to completed but sometimes this isn’t enough for your store.

    In this case, the store sells physical products and subscriptions, that can be a physical product sent by mail each month or a virtual product that gives you access to restricted areas of the website, because of that we need to change WooCommerce default behavior and automatically set our virtual subscriptions as completed orders, after the payment is complete.

    For doing this we’re going to use a hook “woocommerce_order_item_needs_processing” and check if the products on the order are only virtual and subscriptions.

    /**
     * Set Orders from Subscriptions marked as Virtual Products Completed Automatically.
     *
     * @param  boolean $virtual_downloadable_item If item is virtual and downloadable.
     * @param  Object  $_product                  Product Object.
     * @param  int     $product_id                Product ID.
     * @return Boolean                            Should not be set to processing.
     */
    function rfvc_set_virtual_subscriptions_completed( $virtual_downloadable_item, $_product, $product_id ) {
    
    	if ( $_product->is_virtual() && is_a( $_product, 'WC_Product_Subscription' ) ) {
    		$virtual_downloadable_item = true;
    	}
    
    	return $virtual_downloadable_item;
    }
    add_filter( 'woocommerce_order_item_needs_processing', 'rfvc_set_virtual_subscriptions_completed', 10, 3 );
    
    

    You can also use this code to change the order status in virtual products, without them being subscriptions, removing the check for a subscription product object && is_a( $_product, 'WC_Product_Subscription' )