Category: WooCommerce Development

  • 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' )

  • 4 Useful WooCommerce Functions

    4 Useful WooCommerce Functions

    A fast and reliable way to change WooCommerce behaviours

    While working on WooCommerce websites, I’ve found that I was using (most of the times) 4 simple functions that allowed me to have a superior control on the created e-commerce solution.

     

    In this post you’ll find functions for:

    • Add WooCommerce CSS classes to any template you want
    • Change WooCommerce products per page and product columns on listings
    • Add BCC to all emails sent by WooCommerce
    • Remove WooCommerce hooked actions and create your own function with it

     

    Add WooCommerce CSS classes to a page template or a single template from custom post type

    
    add_filter('body_class','add_this_classes');
    
    function add_this_classes($classes) {
            //if is single from CPT "post_type" or page template "template.php"
    	if(is_singular('post_type') || is_page_template('template.php')){
    
    		// add 'class-name' to the $classes array
    		$classes[] = 'woocommerce';
    		$classes[] = 'woocommerce-page';
    
    	}
    
    	// return the $classes array
    	return $classes;
    }
    

     

    Change WooCommerce products per page and columns

    
    //Change products per page to 20
    add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 20;' ), 20 );
    add_filter('loop_shop_columns', 'loop_columns');
    
    if (!function_exists('loop_columns')) {
    
    	function loop_columns() {
    
    		return 8; //change to 8 columns
    
    	}
    
    }
    

     

    Add BCC in all emails sent by WooCommerce

    
    add_filter( 'woocommerce_email_headers', 'add_bcc_all_emails', 10, 2);
    
    function add_bcc_all_emails($headers, $object) {
    
    	$headers = array();
    	$headers[] = 'Bcc: Name <[email protected]>';
    	$headers[] = 'Content-Type: text/html';
    
    	return $headers;
    }
    

     

    Remove WooCommerce hooked action and use the function from it

    //Remove add to cart from single product summary
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    
    woocommerce_template_single_add_to_cart();
    
  • WooCommerce – Automatically set order status after payment is received

    WooCommerce – Automatically set order status after payment is received

    This post has an updated version on WooCommerce and WooCommerce Subscriptions – Automatically set order status to completed

    Using Filter: woocommerce_payment_complete_order_status

    Sometimes when you’re developing an e-commerce website you’ll need to automatically check the payment status and mark an order as completed.

    This usually implies that you’re selling a “virtual good” but not always, I’ve been developing a store that sells “virtual goods” that aren’t marked as such in WooCommerce due to some internal functionalities.

    WooCommerce does this behavior automatically on virtual goods but not in general, also I’ve other payment methods that have a callback and I need this behavior in those as well.

    To achieve this, I’ve used the function:

    add_filter( 'woocommerce_payment_complete_order_status', 'rfvc_update_order_status', 10, 2 );
    
    function rfvc_update_order_status( $order_status, $order_id ) {
    
     $order = new WC_Order( $order_id );
    
     if ( 'processing' == $order_status && ( 'on-hold' == $order->status || 'pending' == $order->status || 'failed' == $order->status ) ) {
    
     return 'completed';
    
     }
    
     return $order_status;
    }
    

    This function does 2 simple things, first using the filter it’s hooked to the payment status change to complete, when it happens, it checks the order status, and if it has changed to “processing” and has previously set as “on-hold”, “pending” or “failed” the new status will be “completed”, allowing all the actions set to this status to occur automatically.

    Using this hook will allow you to set this behavior to all payment methods at once, but if you wish you could also check the payment method from the order, and filter the change accordingly.

  • WooCommerce – Customizing Checkout Fields

    Adding a new field to checkout and Order info on WooCommerce Admin

    Working on my latest project I’ve needed to add a new field to WooCommerce checkout, a “NIF” (Portuguese equivalent of VAT number) field, that isn’t available by default in WooCommerce checkout.

    After checking a couple of websites I’ve ended up creating the following snippet:

    // Hook in WooCommerce checkout fields and add new field
    add_filter( 'woocommerce_checkout_fields' , 'add_field_to_checkout' );
    
    // Our hooked in function - $fields is passed via the filter!
    function add_field_to_checkout( $fields ) {
    
    $fields['billing']['billing_fieldname'] = array(
        'label' => __('Field Name', 'woocommerce'),
        'placeholder' => _x('Field Label as placeholder', 'placeholder', 'woocommerce'),
        'required' => false,
        'class' => array('form-row-wide'),
        'clear' => true
      );
    
    return $fields;
    }
    
    //Add info on Admin
    function add_field_to_admin($order){
      echo "<p><strong>Field Name:</strong> " . $order->order_custom_fields['_billing_fieldname'][0] . "</p>";
    }
    
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'add_field_to_admin', 10, 1 );

    With this little snippet you can add your new field to “billing” or “shipping” information.