Tag Archives: WooCommerce

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

Query for WooCommerce products by global product attributes

// Set attribute name and value to search for
$attribute_name  = 'color';
$attribute_value = 'green';

$args = array(
	'post_type'      => 'product',
	'post_status'    => 'any',
	'posts_per_page' => -1,
	'orderby'        => 'title',
	'order'          => 'ASC',
	'tax_query'      => array(
		array(
			'taxonomy' => 'pa_' . $attribute_name,
			'field'    => 'name', // can be 'slug'
			'terms'    => $attribute_value,
		),
	),
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) {
	$loop->the_post();
	// do stuff here... e.g. get_the_ID()
}
wp_reset_postdata();

Query for WooCommerce products by product specific custom attribute

// Set custom attribute name and value to search for
$attribute_name  = 'color';
$attribute_value = 'green';

$serialized_value = serialize( 'name' ) . serialize( $attribute_name ) . serialize( 'value' ) . serialize( $attribute_value ); // extended version: $serialized_value = serialize( $attribute_name ) . 'a:6:{' . serialize( 'name' ) . serialize( $attribute_name ) . serialize( 'value' ) . serialize( $attribute_value ) . serialize( 'position' );
$args = array(
	'post_type'      => 'product',
	'post_status'    => 'any',
	'posts_per_page' => -1,
	'orderby'        => 'title',
	'order'          => 'ASC',
	'meta_query' => array(
		array(
			'key'     => '_product_attributes',
			'value'   => $serialized_value,
			'compare' => 'LIKE',
		),
	),
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) {
	$loop->the_post();
	// do stuff here... e.g. get_the_ID()
}
wp_reset_postdata();