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; } function UpdateStock($stockvalues){ global $wp_filesystem; $upload_dir = wp_upload_dir(); // Grab uploads folder array $dir = trailingslashit( $upload_dir['basedir'] ) . 'logs/'; // Set storage directory path //WP_Filesystem(); // Initial WP file system //$wp_filesystem->mkdir( $dir ); // Make a new folder for storing our file $countupdate = 0; $logs =''; $logs .= 'DATE:'. date("Y/m/d"). '--TIME:' . date("h:i:sa").'
'; echo '

'; $total = count($stockvalues); $x=0; $y=0; $z=0; //echo '

'; //print_r($stockvalues); //echo '

'; flush(); ob_flush(); $start = microtime(true); while($x < $total) { $sku = $stockvalues[$x]['sku']; $stockqty = $stockvalues[$x]['stock']; $kageid = $stockvalues[$x]['kageid']; if ((strpos($sku, 'MAL') !== false) ) { $prodid = wc_get_product_id_by_sku( $sku ); //$result = updating($prodid, $stockqty); if (!empty($prodid)){ // update_post_meta($prodid , '_stock', $stockqty); if ($stockqty==0){ // update_post_meta( $prodid, '_stock_status', wc_clean('outofstock') ); //$stockqty = 'zero'; }else{ // update_post_meta( $prodid, '_stock_status', wc_clean('instock') ); } //check if stock is managed or not if (isset($stockqty)){ }else{ $stockqty ="in stock"; // update_post_meta( $prodid, '_stock_status', wc_clean('instock') ); } echo $sku.' - Stock updated to ->'.$stockqty.'
'; $msg = 'ID- '.$prodid.' SKU- '.$sku.' - Stock updated to ->'.$stockqty; } else { echo $sku.' - NOT FOUND ('.$kageid.')
'; $msg = $sku.' - NOT FOUND'; } $logs .= $msg.'
'; } // end if MAL $x++; } while($y < $total) { $sku = $stockvalues[$y]['sku']; $stockqty = $stockvalues[$y]['stock']; $kageid = $stockvalues[$y]['kageid']; if ((strpos($sku, 'K2') !== false) ) { $prodid = wc_get_product_id_by_sku( $sku ); //$result = updating($prodid, $stockqty); if (!empty($prodid)){ // update_post_meta($prodid , '_stock', $stockqty); if ($stockqty==0){ // update_post_meta( $prodid, '_stock_status', wc_clean('outofstock') ); //$stockqty = 'zero'; }else{ // update_post_meta( $prodid, '_stock_status', wc_clean('instock') ); } //check if stock is managed or not if (isset($stockqty)){ }else{ $stockqty ="in stock"; // update_post_meta( $prodid, '_stock_status', wc_clean('instock') ); } echo $sku.' - Stock updated to ->'.$stockqty.'
'; $msg = 'ID- '.$prodid.' SKU- '.$sku.' - Stock updated to ->'.$stockqty; } else { echo $sku.' - NOT FOUND ('.$kageid.')
'; $msg = $sku.' - NOT FOUND'; } $logs .= $msg.'
'; } // end if MAL $y++; } while($z < $total) { $sku = $stockvalues[$z]['sku']; $stockqty = $stockvalues[$z]['stock']; $kageid = $stockvalues[$z]['kageid']; if ((strpos($sku, 'KWS') !== false) ) { $prodid = wc_get_product_id_by_sku( $sku ); //$result = updating($prodid, $stockqty); if (!empty($prodid)){ // update_post_meta($prodid , '_stock', $stockqty); if ($stockqty==0){ // update_post_meta( $prodid, '_stock_status', wc_clean('outofstock') ); }else{ // update_post_meta( $prodid, '_stock_status', wc_clean('instock') ); } //check if stock is managed or not if (isset($stockqty)){ }else{ $stockqty ="in stock"; // update_post_meta( $prodid, '_stock_status', wc_clean('instock') ); } echo $sku.' - Stock updated to ->'.$stockqty.'
'; $msg = 'ID- '.$prodid.' SKU- '.$sku.' - Stock updated to ->'.$stockqty; } else { echo $sku.' - NOT FOUND ('.$kageid.')
'; $msg = $sku.' - NOT FOUND'; } $logs .= $msg.'
'; } // end if MAL $z++; } $time_elapsed_secs = microtime(true) - $start; echo '

'; echo 'exec time: '.$time_elapsed_secs; $logs .= $time_elapsed_secs .'
'; //$wp_filesystem->put_contents( $dir . 'stockupdatelog.html', $logs, 0644 ); // Finally, store the file :D } ?>

Click this to update the list