23

I'm working on a separate templates page, which page gets woocommece product sku using custom field of wordpress post. i need to get product id of that sku for create woocommece object and do further things, here is my code.

global $woocommerce;
//this return sku (custom field on a wordpress post)
$sku=get_field( "product_sku" );
if($sku!=''){
  //need to create object, but using sku cannot create a object, 
  $product = new WC_Product($sku); 
  echo $product->get_price_html();
}

is there way to get product id before create object, then i can pass the product id to WC_Product class constructor and create object.thank you

Suneth Kalhara
  • 1,116
  • 4
  • 16
  • 40
  • $sku=get_field( "product_sku" ); return a sku of woocommerce product from wordpress custom field,i need to get the product id of that sku – Suneth Kalhara May 16 '14 at 04:30

3 Answers3

94

WooCommerce 2.3 finally adds support for this in core.

If you are using this version, you can call

wc_get_product_id_by_sku( $sku )
Akshay Agarwal
  • 1,959
  • 1
  • 16
  • 14
  • 2
    Addition: This gets the `$product = new WC_Product(wc_get_product_id_by_sku($sku));` – optimiertes Jan 09 '22 at 20:33
  • This also works for Variable Products if each variation has its own SKU. You can then declare the class for it via `new WC_Product_Variable(wc_get_product_id_by_sku($variation_sku));` – Smithee Jan 19 '23 at 11:11
20

You can use this function (found here). Google is your friend!

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 ) );

    if ( $product_id ) return new WC_Product( $product_id );

    return null;
}
pmandell
  • 4,288
  • 17
  • 21
  • This query doesn't check to see that the product is published may send a user to a 404 page if a product is in draft or is private, or if a product with the same sku is in the trash. – rmmoul May 10 '22 at 20:19
1

WooCommerce product is a special post type. Because of this WP_Query might be used to find product by SKU and other parameters. This might be used as an alternative when you need to restrict your search by some other criterias.

For example you might want to specify language if you use Polylang (or other plugins) for site translations. Or you can restrict search by product type.

They do direct SQL query in the WooCommerce method get_product_id_by_sku which I think is perfectly fine in many cases. But might not work if you use translations, it will return random product but not the one in current language.

Example code to find product by SKU using WP_Query (with restriction by product type and language):

public function find( string $lang, string $sku ) {
    $query = [
        'lang'       => $lang,
        'post_type'  => 'product',
        'meta_query' => [
            [
                'key'     => '_sku',
                'value'   => $sku,
                'compare' => '='
            ]
        ],
        'tax_query'  => [
            [
                'taxonomy' => 'product_type',
                'terms'    => [ 'grouped' ],
                'field'    => 'name',
            ]
        ]
    ];
    $posts = ( new WP_Query() )->query( $query );

    return count( $posts ) > 0 ? $posts[0] : null;
}
Victor Smirnov
  • 3,450
  • 4
  • 30
  • 49
  • You do not need tax_query in that example, I do not quite understand what you tried to achieve by it. – NIck Oct 01 '21 at 08:38