Reputation: 21
I'm trying to get WooCommerce products loop on home page, everything works fine like product title, products id, product image but the product permalink is not working.... getting the home URL by get_permalink()
<?php
$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>
<div class="product-title">
<h6><?php echo get_the_title(); ?></h6>
</div>
<div class="product-id">
<h6><?php echo $product->get_id();; ?></h6>
</div>
<a href="<?php echo get_permalink(); ?>" class="dodo">Add to cart</a>
<?php
endwhile;
wp_reset_query();
?>
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() ) ?>">
<?php
$productId = $product->get_id();
$productUrl = get_permalink($productId);
?>
2. <a href="<?php echo $productUrl; ?>">
But none of above worked.
Upvotes: 1
Views: 435
Reputation: 11861
More precisely you can use the add to cart URL.
$product_addtocart_url = $product->add_to_cart_url();
For getting the permalink, use below code.
$product_perma_link = $product->get_permalink();
You can use this product class method.
/**
* Product permalink.
*
* @return string
*/
public function get_permalink() {
return get_permalink( $this->get_id() );
}
Upvotes: 1