Redirect after login to My Account

29. March 2021

function twstudio_customer_login_redirect( $redirect, $user ) {
    if ( wc_user_has_role( $user, 'customer' ) ) {
        $redirect = '/meine-seite/edit-account/'; 
    }
    return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'twstudio_customer_login_redirect', 9999, 2 );

Add extra field to check-out

9. March 2021

/**
 * Add an extra field in checkout (VAT)
 */
function twstudio_custom_override_checkout_fields( $fields ) {
    $fields['billing']['billing_vat'] = array(
        'label'     => __('VAT', 'woocommerce'),
        'placeholder'   => _x('', 'placeholder', 'woocommerce'),
        'required'  => false,
        'class'     => array('form-row-wide'),
        'clear'     => true
    );
    return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'twstudio_custom_override_checkout_fields' );

// Display field value (VAT) on the order edit page
function custom_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('VAT').':</strong> ' . get_post_meta( $order->get_id(), '_billing_vat', true ) . '</p>';
}
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
Add coupon code on order confirmation mail (to admin)

5. March 2021

/**
 * Add used coupons to the order confirmation email
 */
function twstudio_add_payment_method_to_admin_new_order( $order, $is_admin_email ) {

    if ( $is_admin_email ) {
        if( $order->get_used_coupons() ) {
            $coupons_count = count( $order->get_used_coupons() );
            $i = 1;
            $coupons_list = '';
            foreach( $order->get_used_coupons() as $coupon) {
                $coupons_list .=  $coupon;
                if( $i < $coupons_count )
                    $coupons_list .= ', ';
                $i++;
            }
            echo '<p><strong>Kuponger brukt (' . $coupons_count . ') :</strong> ' . $coupons_list . '</p>';
        } 
    } 
}

add_action( 'woocommerce_email_after_order_table', 'twstudio_add_payment_method_to_admin_new_order', 15, 2 );
Add text string in front of subject on New Order mail on local pickup.

11. February 2021

/*
 * Add text string in front of subject on New Order mail.
 */
function twstudio_change_admin_email_subject( $subject, $order ) {
    global $woocommerce;
    $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
    $subject = sprintf( '[%s] Ny kundeordre (# %s) - %s', $blogname, $order->id, $order->order_date );

    $shipping_method = @array_shift($order->get_shipping_methods());
    if ( $shipping_method ) {
        $shipping_method_id = $shipping_method['method_id'];

        // local_pickup
        if ( strpos($shipping_method_id, 'local_pickup' ) !== false ) {
            $subject = sprintf( 'CUSTOM TEXT [%s] Ny kundeordre (%s) - %s', $blogname, $order->id, $order->get_date_created()->format ('d.m.Y') );
        }
    }

    return $subject;
}
add_filter('woocommerce_email_subject_new_order', 'twstudio_change_admin_email_subject', 1, 2);
Related products useful functions

6. January 2021

/**
 * Change number of related products output
 */
function woo_related_products_limit() {
    global $product;

    $args['posts_per_page'] = 6;
    return $args;
}
function twstudio_related_products_args( $args ) {
    $args['posts_per_page'] = 5; // 5 related products
    $args['columns'] = 2; // arranged in 2 columns
    return $args;
}
add_filter( 'woocommerce_output_related_products_args', 'twstudio_related_products_args', 20 );



/**
 * Remove category from Related products
 */
function twstudio_exclude_product_category_from_related_products( $related_posts, $product_id, $args  ){
    $term_slug = 'ukategorisert';

    $exclude_ids = wc_get_products( array(
        'status'    => 'publish',
        'limit'     => -1,
        'category'  => array($term_slug),
        'return'    => 'ids',
    ) );

    return array_diff( $related_posts, $exclude_ids );
}
add_filter( 'woocommerce_related_products', 'twstudio_exclude_product_category_from_related_products', 10, 3 );

Change ‘landingpage’ in My Account

13. August 2020

**
 * Change landingpage on My account page
 */
function twstudio_subscriptions_as_myaccount_home(){
	if ( is_account_page() && empty( WC()->query->get_current_endpoint() ) ){
		wp_safe_redirect( wc_get_account_endpoint_url( 'subscriptions' ) );
		exit;
	}
}
add_action( 'template_redirect', 'twstudio_subscriptions_as_myaccount_home' );

Goes into functions.php. Change ‘subscriptions’ to your endpoint of choise.

Replace ‘Dismiss’ with an X in Woo store notice

2. April 2020

function twstudio_demo_store_filter($text) {

	$text = str_replace(array('<p class="woocommerce-store-notice demo_store">', '</p>', 'Dismiss'), array('<div class="woo-notice-container"><p class="woocommerce-store-notice demo_store">', '</p></div>', 'X'), $text);
	return $text;
}
add_filter('woocommerce_demo_store', 'twstudio_demo_store_filter', 10, 1);
Remove Uncategorized from widget

20. March 2020

function remove_uncategorized_category( $terms, $taxonomy, $query_vars, $term_query ) {
if ( is_admin() )
return $terms; if ( $taxonomy[0] == 'product_cat' ) {
foreach ( $terms as $k => $term ) {
if ( $term->term_id == get_option( 'default_product_cat' ) ) {
unset( $terms[$k] );
}
}
} return $terms;
}
add_filter( 'get_terms', 'remove_uncategorized_category', 10, 4 );
Add a empty cart button to cart page

6. February 2020

// Adds a EMPTY CART button to cart
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
	global $woocommerce;

	if ( isset( $_GET['empty-cart'] ) ) {
		$woocommerce->cart->empty_cart();
	}
}

add_action( 'woocommerce_cart_actions', 'tws_add_clear_cart_button', 20 );
function tws_add_clear_cart_button() {
	echo "<a class='button empty-button' href='?empty-cart=true'>" . __( 'Tøm handlekurv', 'woocommerce' ) . "</a>";
}
Remove surname from comments

2. December 2019

Change of this field: .woocommerce-review__author

/**
 * Change the comment name to show only Firstname.
 */
function twstudio_my_comment_author( $author = '' ) {

	$comment = get_comment( $comment_ID );
	if ( ! empty( $comment->comment_author ) ) {
		if ( $comment->user_id > 0 ) {
			$user   = get_userdata( $comment->user_id );
			$author = $user->first_name . ' ' . substr( $user->last_name, 0, 0 ) . '';
		} else {
			$author = __( 'No name' );
		}
	}

	return $author;
}

add_filter( 'get_comment_author', 'twstudio_my_comment_author', 10, 1 );