Created
August 4, 2014 09:51
-
-
Save mircobabini/3342421984051f50ddda to your computer and use it in GitHub Desktop.
Woocommerce function: Get product by SKU (with WPML support)
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
<?php | |
function wc_get_product_by_sku($sku) { | |
global $wpdb; | |
if( defined( 'ICL_LANGUAGE_CODE' ) ){ | |
$product_id = $wpdb->get_var($wpdb->prepare("SELECT pm.post_id FROM ".$wpdb->postmeta." AS pm LEFT JOIN ".$wpdb->prefix."icl_translations AS tr ON pm.post_id = tr.element_id WHERE pm.meta_key='_sku' AND pm.meta_value='%s' AND tr.language_code = '".ICL_LANGUAGE_CODE."'", $sku)); | |
} else { | |
$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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment