I have created a separate mySQL database in my wordpress/woocommerce server which holds separate data. I use this table for basic accounts history for clients.
One of those columns has a string data that is identical to the SKU of products in my woocommerce store. What I would like to do is place a thumbnail of the product beside the SKU in a dynamic php table. My custom table looks like this;
item | SKU | Amount
dog | SKU1 | 3
cat | SKU2 | 4
and when I call it in a php table; it looks like this:
SKU | Item | Amount
SKU1 | dog | 3
SKU2 | cat | 4
I would like the product image to come before the SKU1
; so:
(image)SKU1 | dog | 3
This is what I have tried (grabbing post id to fetch product image) but it doesnt seem to work...
$result = mysqli_query($con,"SELECT * FROM wp_payout_history WHERE user=$userid ORDER BY date DESC");
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
$SKU = $row['SKU']
$post_id = wc_get_product_id_by_sku($SKU);
$product_meta = get_post_meta($post_id);
$image = wp_get_attachment_image( $product_meta['_thumbnail_id'][0]), 'full' );
echo "<td> ". $image . "" . $row['SKU'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
First, try to use $wpdb
, it's better practice IMHO.
Change:
$product_meta = get_post_meta($post_id);
$image = wp_get_attachment_image( $product_meta['_thumbnail_id'][0]), 'full' );
Into:
$thumb_id = get_post_thumbnail_id($post_id);
$image = wp_get_attachment_image( $thumb_id, 'full' );
If this doesn't work, check if the $post_id
exists and it has a post-thumbnail.
Regards, Bjorn