-1

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Simon S.
  • 563
  • 4
  • 21
  • 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 Answers2

1

You can use this below code

$product_obj = get_page_by_path( $slug, OBJECT, 'product' );

Better explanation for your question .

https://wordpress.stackexchange.com/questions/206886/get-product-details-by-url-key-in-wordpress-woocommerce

Amir shah
  • 317
  • 2
  • 7
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