Tag Archives: WordPress

Get current filter priority in WordPress

if ( ! function_exists( 'get_current_filter_priority' ) ) {
	/*
	 * get_current_filter_priority.
	 */
	function get_current_filter_priority() {
		global $wp_filter;
		$current_filter_data = $wp_filter[ current_filter() ];
		if ( class_exists( 'WP_Hook' ) ) {
			// since WordPress v4.7
			return $current_filter_data->current_priority();
		} else {
			// before WordPress v4.7
			return key( $current_filter_data );
		}
	}
}

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 ] : '';
}