<?php
function getproductdetail($product_id){
	
	$src = wp_get_attachment_image_src( get_post_thumbnail_id($product_id ), 'thumbnail_size' );
	$url = $src[0];?>

	<img src="<?php echo $url;?>" width="200px" />

	<?php
	$product = wc_get_product( $product_id );


	$handle=new WC_Product_Variable($product_id);
	$variations1=$handle->get_children();?>
	<p><a href="<?php echo get_permalink($product_id );?>" target="new"><strong><?php echo $product->get_name();?></strong></a></p>

	<?php
	$ct_selectsizeguide = 0;
	$ct_selectsizeguide = get_post_meta( $product_id , '_ct_selectsizeguide', true );
	$ctg = get_post_meta( $ct_selectsizeguide , '_ct_sizeguide', true );
	$row_count = count ($ctg[0]['table']); ?>
	<div id ="sg<?php echo $product_id; ?>" align="center" style="display: none;">
		<?php print_r ($ctg[0]['title']); ?>
		<table border="1" cellspacing="0" cellpadding="4" align="center" style="text-align: center;">
		<?php
		for ($i = 0; $i < $row_count; $i++) { // total rows
			$col_count = count ($ctg[0]['table'][$i]);?>
			<tr>
			<?php for ($j = 0; $j < $col_count; $j++) { ?>
				<td><?php echo ($ctg[0]['table'][$i][$j]);?> </td>	
			<?php } ?>
		</tr>
		<?php
		}
		?>
		</table>
		<?php print_r ($ctg[0]['caption']); ?>
		<br />
	</div>
		
	<?php
	$product = new WC_Product($product_id);?>
	<strong>Price:</strong> Rs. <?php echo $product->get_price();?> |
	<?php	
	if (!empty($ctg)){ ?>
		<span onClick="javascript:sizeguide(<?php echo $product_id; ?>);" class="sglink">Size Guide</span>
	<?php } ?>

	<?php
	$user = wp_get_current_user();
	if ( in_array( 'administrator', (array) $user->roles ) ) {?>
		
		| <strong>SKU:</strong> <a href="<?php echo site_url();?>/fa/inventory/manage/items.php?stock_id=<?php echo $product->get_sku();?>" target="new"><?php echo $product->get_sku();?></a> |
		
	
		<a href="<?php echo site_url();?>/wp-admin/post.php?post=<?php echo $product_id; ?>&action=edit" target="new">Edit</a>

	<?php
	}	
	?>

	<table cellpadding="4" cellspacing="1" align="center" border="1">

	<tr><td ><strong>Variations</strong></td><td align="center" style="width:1%;white-space:nowrap;"><strong>Stock</strong></td><td nowrap align="center" style="width:1%;white-space:nowrap;"><strong>Add to cart</strong></td></tr>
	<?php
	$stock = 0;
	foreach ($variations1 as $value) {
		$single_variation=new WC_Product_Variation($value);
			$image_id = $single_variation->image_id;
			$var_id = $single_variation->variation_id;						
		?>
			<tr <?php if ($single_variation->stock == 0 ){ ?> style='color:red;' <?php } ?> >
			<?php
			echo "<td nowrap>";
			//print_r ($single_variation);
			$attributes = $single_variation->get_variation_attributes();
			$add_to_cart = "";
			$add_to_cart = $product_id;			
			foreach($attributes as $key => $value) {
				$add_to_cart = $add_to_cart . "&$key=$value";
			}
			// print_r ($single_variation->get_variation_attributes() );?>
			<div onClick="javascript:varimg(<?php echo $var_id; ?>);" class="imglink">
				<?php // echo implode(" / ", $single_variation->get_variation_attributes()); ?>
				<?php  $vars = $single_variation->get_attribute_summary();
						$vars = str_replace("Color: ","",$vars);
						$vars = str_replace("Size: ","",$vars);
						$vars = str_replace("Shade: ","",$vars);
						$vars = str_replace("Unit: ","",$vars);
						echo $vars = str_replace(","," /",$vars);
				?>
				
			</div>
				
			<div class="imglink" id="varimg<?php echo $var_id; ?>" style="display:none;cursor: n-resize;" onClick="javascript:varimg(<?php echo $var_id; ?>);">
				<?php echo wp_get_attachment_image( $image_id, 'thumb' ); ?>
			</div>
							
			<?php
			echo "</td>";
			echo "<td nowrap style='text-align: center;'>";
			if (!empty($single_variation->stock))
				echo $single_variation->stock;
			else
				echo "0";
			$stock = $stock + $single_variation->stock;
			echo "</td>";
			echo "<td nowrap style='text-align: center;'>";?>
			<?php if ($single_variation->stock > 0 ){ ?>
			<a href="<?php echo site_url();?>/cart/?add-to-cart=<?php echo $add_to_cart;?>" target="new" >Add to cart</a>
			<?php } else 
				echo "Out of stock";
			echo "</td>";	
			echo "</tr>";
	}
	 ?>
	<tr><td><strong>Total Stock</strong><td style='text-align: center;'><strong><?php echo $stock;?></strong></td><td>&nbsp;</td></tr>
</table>
<?php }
?>