Woocommerce get Product id using product SKU

后端 未结 3 1463
孤街浪徒
孤街浪徒 2020-12-18 23:12

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 wooco

3条回答
  •  臣服心动
    2020-12-18 23:44

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

提交回复
热议问题