Last active
April 28, 2023 08:39
-
-
Save mattiasghodsian/0d379a53415a3e5e4052ebdbfa714d4c to your computer and use it in GitHub Desktop.
get woocommerce product ID by sku
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Title: getProductIDBySku | |
* Author: Mattias Ghodsian | |
* Description: get woocommerce product ID by sku | |
* Donate a cup of coffee: https://www.buymeacoffee.com/mattiasghodsian | |
**/ | |
Public function getProductIDBySku( $sku ) | |
{ | |
if (empty($sku)) { return false; } | |
global $wpdb; | |
// $product_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key='_sku' AND post_id='%s' LIMIT 1", $sku ) ); | |
$data = $wpdb->get_results(" SELECT * FROM $wpdb->postmeta WHERE meta_key = '_sku' "); | |
foreach ($data as $key => $row) { | |
if ( $row->meta_value == $sku ) { | |
return $row->post_id; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment