How do I use a different template based on an order variable?

In order to use a different template based on an order variable, simply add the following filter to your themes functions.php You can, for example, change the function to use a different template based on the payment method instead.

/**
 * Change template based on WPML order language.
 * Make sure to create custom templates with the correct names or the templates won't be found.
 *
 * @param string $template_name template name.
 * @param string $template_type template type like global or simple.
 * @param int    $order_id WC Order ID.
 *
 * @return string
 */
function change_template_based_on_order_language( $template_name, $template_type, $order_id ) {
    $order_language = get_post_meta( $order_id, 'wpml_language', true );

    if ( false === $order_language ) {
        return $template_name;
    }

    switch ( $order_language ) {
        case 'en':
            $template_name = 'minimal-en';
            break;
        case 'nl':
            $template_name = 'minimal-nl';
            break;
    }

    return $template_name;
}
add_filter( 'wpi_template_name', 'change_template_based_on_order_language', 10, 3 );
	

Please test the functionality on a development environment of your website. If it ceases to work, please revert back to the original state of your website and follow the tutorial again.

Still need help? Contact Us Contact Us