Joe Bloggs Joe Bloggs - 1 month ago 5
PHP Question

Get Product Permalink - My Account - Recent Orders Woocommerce

I've been trying to add a product thumbnail image linking to product on customer Recent Orders page, My Account in woocommerce.
I have managed to get the Image Thumbnail in place thanks to Anand from this question here :
Add Product Thumbnail to My Account - Recent Orders - Woocommerce,
but now I'm strugling to make this thumb a permalink that links to the actual product.

So I know that this is the code to get the image thumbnail in place :

<?php
// Get a list of all items that belong to the order
$products = $order->get_items();

// Loop through the items and get the product image
foreach( $products as $product ) {

$product_obj = new WC_Product( $product["product_id"] );

echo $product_obj->get_image();

}
?>


I've been trying to turn the thumbnail into a permalink like this :

<?php
// Get a list of all items that belong to the order
$products = $order->get_items();

// Loop through the items and get the product image
foreach( $products as $product ) {

$product_obj = new WC_Product( $product["product_id"] );

echo '<a href="'.get_permalink($product_id).'"><?php echo $product_obj->get_image();?></a>';

}
?>


Or like this :

echo '<a href="'.get_permalink($product_id).'">'echo $product_obj->get_image()'</a>';


Or this :

<a href="<?php echo $url = get_permalink( $product_id ); ?>">
<?php
// Get a list of all items that belong to the order
$products = $order->get_items();

// Loop through the items and get the product image
foreach( $products as $product ) {

$product_obj = new WC_Product( $product["product_id"] );

echo $product_obj->get_image();

}
?>


But just can't seem to be anywhere near .. ?

Answer

It is quite simple, the Product class has a get_permalink method which you can use like so:

$product_obj = new WC_Product( $product["product_id"] );

$link = $product_obj->get_permalink();

echo '<a href="'. $link .'">' . $product_obj->get_image() . '</a>';

EDIT

If you however want to use the get_permalink offered by WordPress you can do it like so

echo '<a href="'.get_permalink($product_obj->id).'"><?php echo $product_obj->get_image();?></a>';

You were using $product_id in your code below, since its not defined anywhere your code wasn't working. You were very close :)

echo '<a href="'.get_permalink($product_id).'"><?php echo $product_obj->get_image();?></a>';