获取产品永久链接 - 我的帐户 - 最近的订单 Woocommerce

Get Product Permalink - My Account - Recent Orders Woocommerce

我一直在尝试在客户最近的订单页面上添加链接到产品的产品缩略图,我在 woocommerce 中的帐户。 多亏了 Anand 从这里的这个问题,我已经成功地得到了图像缩略图: , 但现在我正在努力让这个拇指成为链接到实际产品的永久链接。

所以我知道这是获取图像缩略图的代码:

<?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();

    }
?>

我一直在尝试将缩略图变成这样的固定链接:

<?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>';

   }
 ?>

或者像这样:

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

或者这个:

<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();

        }
     ?>

但似乎无法靠近..?

这很简单,产品 class 有一个 get_permalink 方法,您可以像这样使用它:

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

$link = $product_obj->get_permalink();

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

编辑

但是,如果您想使用 WordPress 提供的 get_permalink,您可以这样做

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

您在下面的代码中使用了 $product_id,因为它没有在您的代码无法运行的任何地方定义。你非常亲密:)

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