2

The question is simple, if I know the SKU of my product and nothing else, how can I retrieve the url/permalink to that item?

This is commonly useful for third party integration.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
PraviM
  • 21
  • 1
  • 3

2 Answers2

3

You can use dedicated function wc_get_product_id_by_sku( $sku ) where $sku argument is the SKU of your product.

It will return the product ID.

Reference: Function wc_get_product_id_by_sku

Then to get the permalink:

$sku = 'Gh2563'; // SKU example to be replaced by the real SKU of the product
$product_id = wc_get_product_id_by_sku( $sku );
$link = get_permalink( $product_id );
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
0
     function get_product_by_sku( $sku ) {

       global $wpdb;

     $product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );

     $url = get_permalink( $product_id );

     return $url;
        }

hope this help.

Thanks

Bilal Hussain
  • 994
  • 1
  • 10
  • 25
  • 1
    Hi, Thanks for your help, but it's not working. Is there any direct function to get product permalink by the sku? as is we can get the product url by the product id. – PraviM Aug 29 '17 at 13:06