I have the productname and obviously also the URL like so: product/productname in woocommerce, but I can't find a way to get the id of the product with this information.
Asked
Active
Viewed 1,440 times
-1
-
you can use the get_page_by_title( 'Product Title', OBJECT, 'product' ); this way you can get the product id – Amir shah Dec 30 '18 at 21:38
-
Please insert your code to your question! Without code it's hard to help you! – FZs Dec 30 '18 at 21:38
2 Answers
1
You can use this below code
$product_obj = get_page_by_path( $slug, OBJECT, 'product' );
Better explanation for your question .

Amir shah
- 317
- 2
- 7
-
please do accept the answer as accepted answer this would help others in this way – Amir shah Dec 30 '18 at 22:32
-
This is a php snippet but he is asking how to do it with JavaScript only – svelandiag Dec 05 '19 at 17:08
1
Well the accepted answer works on php file then you can embed php in your javascript using <?= ?>
however there's an easier way of getting woocommerce product_id in a standalone JavaScript file, you just simply get the data attribute of the add-to-cart DOM element:
$('[name="add-to-cart"]').val()

svelandiag
- 4,231
- 1
- 36
- 72