Skip to main content

Woocommerce Custom: Related Products & Variation Prices

November 16, 2016
|

Here at Eyesore, we work for our customers. And if the customer wants a specific look on their site that theme templates cannot deliver, then by golly, we’ll load up a Blank theme and make it for them! So when development was approved for a large eCommerce site, and the design came through to the developers,  it became apparent that the single product page mockup had a custom layout. This required us to undo and repurpose most of the hooks in that Woocommerce page template, reorganizing them and adding our own HTML in the appropriate spots.

In doing so, one of the challenges we came across was pulling in the prices for products in the Related section, the area below the main content for the single product. Variable products were not displaying the correct range of prices but simple products were showing up correctly. So, we decided to write up the following, which you can just place wherever the price needs to show up.

//check if this is a variable product. if so, grab minimum and maximum prices for variations. 
//else, display the regular price.
global $product;
if($product->is_type('variable')) {
	$pid = $product->id;

	$args = array(
		'post_type' => 'product_variation',
		'post_status' => 'publish',
		'numberposts' => -1,
		'orderby' => 'menu_order',
		'order' => 'asc',
		'post_parent' => $pid
	);

	$variations = get_posts( $args );

	$variation_prices = array();

	foreach($variations as $key => $obj) {
		$variation_prices[]= get_post_meta( $obj->ID, '_regular_price', true);
	}

	$min = min($variation_prices); 
	$max = max($variation_prices); 

	if ( $min == $max ){ 
		echo "$" . $min; 
	} 
	else{	
		echo "$" . $min . " - " . "$" . $max; 
	}

} else {

	$sale_price = get_post_meta( get_the_ID(), '_price', true);
	echo '$' . $sale_price;
	
}

And there you go! Eyesore Inc, working to accomplish your custom web needs!