Tag Archives: WordPress

Add WooCommerce product shipping class name to cart

/*
 * Add this to your (child) theme's functions.php file
 */
add_filter( 'woocommerce_cart_item_name', 'add_product_shipping_class', PHP_INT_MAX, 3 );
/*
 * add_product_shipping_class.
 */
function add_product_shipping_class( $cart_item_name, $cart_item, $cart_item_key ) {
	$product_id = ( 0 != $cart_item['variation_id'] ) ? $cart_item['variation_id'] : $cart_item['product_id'];
	$product_shipping_classes = get_the_terms( $product_id, 'product_shipping_class' );
	$product_shipping_class_name = ( $product_shipping_classes && ! is_wp_error( $product_shipping_classes ) ) ? current( $product_shipping_classes )->name : '';
	return $cart_item_name . ' ' . $product_shipping_class_name;
}

Remove WooCommerce payment gateway instructions from thank you page, but leave enabled in emails

/*
 * Remove WooCommerce payment gateway instructions from thank you page, but leave enabled in emails.
 * Add it to your (child) theme's functions.php file.
 * Snippet is for cheque payment gateway. You can also use:
 * woocommerce_thankyou_bacs, woocommerce_thankyou_cod, woocommerce_thankyou_paypal etc.
 */
add_action( 'woocommerce_thankyou_cheque', 'ob_start', 0, 0 );
add_action( 'woocommerce_thankyou_cheque', 'ob_end_clean', PHP_INT_MAX, 0 );

Get WooCommerce tax class name

/**
 * get_tax_class_name.
 */
function get_tax_class_name( $tax_class ) {
	$tax_classes       = WC_Tax::get_tax_classes();
	$classes_names     = array();
	$classes_names[''] = __( 'Standard', 'woocommerce' );
	if ( ! empty( $tax_classes ) ) {
		foreach ( $tax_classes as $class ) {
			$classes_names[ sanitize_title( $class ) ] = esc_html( $class );
		}
	}
	return ( isset( $classes_names[ $tax_class ] ) ) ? $classes_names[ $tax_class ] : '';
}

Check user’s role in WordPress

/**
 * Checks user role. Can be current user or user by ID.
 *
 * @return  bool
 */
function is_user_role( $user_role, $user_id = 0 ) {
	$the_user = ( 0 == $user_id ) ? wp_get_current_user() : get_user_by( 'id', $user_id );
	if ( ! isset( $the_user->roles ) || empty( $the_user->roles ) ) {
		$the_user->roles = array( 'guest' );
	}
	return ( isset( $the_user->roles ) && is_array( $the_user->roles ) && in_array( $user_role, $the_user->roles ) ) ? true : false;
}