codenox
codenox

Reputation: 51

Woocommerce - How can I get product ID when I have got the title and I am on a different page?

I´ve got a little problem :) I want to create a custom landing page additionally to my product page. This custom landing page gets the same name as the product. That way I have the post title, but not the correct ID.

With this posttitle I want to get the ID of the product. I´ve found several shortcodes to get the posttitle and ID when I´m on the product page or if I put in the product ID manually, but not automatically.

I want something that looks for "Toothbrush XY" in my $product database and returns the specific ID for that Title if it finds it :) The ID is supposed to be from the product IDs, not the actual post!

Can someone please help me? Thanks and kind regards, codenox

Upvotes: 0

Views: 1794

Answers (3)

codenox
codenox

Reputation: 51

Unfortunatly I don´t get the correct Product ID with these codes. I get the ID of the actual Page, but the thing I want is the Product ID of the product with the same title as my page / blogpost.

Upvotes: 0

Swapnali
Swapnali

Reputation: 1299

Try this code -

<?php 
   global $post,$product_name;
   $product_name=$post->post_title;
   echo get_the_ID($product_name);
  ?>

This code retrieves the Product ID from the Product name.

Upvotes: 0

mujuonly
mujuonly

Reputation: 11841

$product = get_page_by_title( 'Product Title', OBJECT, 'product' )
$product->ID 

Upvotes: 3

Related Questions