Products url outside wocommerce shop page

im trying to get woocommerce products loop in home page, everything works fine like product title, products id, product image but product permalink is not working…
getting home url

$args = array(
'post_type'             => 'product',   
'posts_per_page'        => 6,
'post_status'           => 'publish',
'taxonomy' => 'product_cat',
'hide_empty' => true,
'parent'   => 0

$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post();
global $product;
<div class="product-image">
    <?php the_post_thumbnail(); ?>

<div class="product-title">
    <h6><?php echo get_the_title(); ?></h6>

<div class="product-id">
    <h6><?php echo $product->get_id();; ?></h6>

<a href="<?php echo get_permalink(); ?>" class="dodo">Add to cart</a>

But Add to cart(permalink) is not working, it’s not getting product url, have tried different methods:

1. <a href="<?php echo esc_url( $product->get_product_url() ) ?>">

   $productId  = $product->get_id();
   $productUrl = get_permalink($productId);
2. <a href="<?php echo $productUrl; ?>">

But none of above worked.

Have you tried this? First try to echo the product Id. If you are getting the product id then you can try this.

$product = wc_get_product( $product_id );
$permalink = $product->get_permalink();

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.