• Resolved qpalzm123

    (@qpalzm123)


    Hi Everyone,
    I’m setting up a shop and everytihng is going pretty nicely.
    I have about 100 products ready to go live and I am able to get live stock levels hourly from my supplier in a .csv file which I pull onto my server.
    I then parse through it and update the new stock level for my relevant SKUS (I don’t sell everything I can get from the supplier currently so some of the csv is not needed).
    I have my list of product SKUs I want to update which are held in an array which I cycle through liske this:

    require(“/home/electricoutboard/public_html/wp-load.php”); // load wordpress

    #setup list of stock sku manually here
    $skus = array(“MD1180/66″); # protruar 1hp 66
    array_push($skus,”MD1242/GPS/BL”); # cayman bow new black
    array_push($skus,”MD1134″); # mini battery switch with key
    array_push($skus,”MD1224″); # trolling motor battery power box
    array_push($skus,”MD1245″); # protruar 5
    array_push($skus,”MD1181″); # protruar 2
    array_push($skus,”MD1176″); # w20-kayak-pro motor
    array_push($skus,”MD1247/63″); #

    foreach($skus as $sku_to_test)
    {
    $query = “select stock_level from wp_alg_stocklist where sku = ‘$sku_to_test’;”;
    print “<p>query $query</p>”;
    $result = mysqli_query($link,$query);
    $row = mysqli_fetch_array($result);
    $stock_level = $row[“stock_level”];

    $product_id = wc_get_product_id_by_sku(“$sku_to_test”);

    print “$sku_to_test new stock_level $stock_level from imported file with product_id $product_id”;

    wc_update_product_stock($product_id,$stock_level,”set”,”false”);
    wc_delete_product_transients($product_id);

    If i run this in my web browser I get this output:

    file existsCSV File has been successfully Imported.
    
    query select stock_level from wp_alg_stocklist where sku = 'MD1180/66';
    MD1180/66 new stock_level 0 from imported file with product_id 996
    
    query select stock_level from wp_alg_stocklist where sku = 'MD1242/GPS/BL';
    MD1242/GPS/BL new stock_level 8 from imported file with product_id 0
    
    query select stock_level from wp_alg_stocklist where sku = 'MD1134';
    MD1134 new stock_level 13 from imported file with product_id 742
    
    query select stock_level from wp_alg_stocklist where sku = 'MD1224';
    MD1224 new stock_level 132 from imported file with product_id 737
    
    query select stock_level from wp_alg_stocklist where sku = 'MD1245';
    MD1245 new stock_level 13 from imported file with product_id 0
    
    query select stock_level from wp_alg_stocklist where sku = 'MD1181';
    MD1181 new stock_level 1 from imported file with product_id 765
    
    query select stock_level from wp_alg_stocklist where sku = 'MD1176';
    MD1176 new stock_level 184 from imported file with product_id 0
    
    query select stock_level from wp_alg_stocklist where sku = 'MD1247/63';
    MD1247/63 new stock_level 14 from imported file with product_id 0

    ########################################################################
    and the oddity is for example MD1176 – I get the new stock level from the spreadsheet but the code

    $product_id = wc_get_product_id_by_sku(“$sku_to_test”);

    does not pick up the product id for SKU MD1176, MS1245, MD1242/GPS/BL and MD1247/63 in this test scenario.

    Then the stock is not being updated using:

    wc_update_product_stock($product_id,$stock_level,”set”,”false”);

    I thought requiring wp-load.php would load these functions? Do I need to pull them in from somewhere else or am I doing something else wrong as well?

    Can anyone help me with the SQL I can run to try to see if I can pull the product ID directly from the database?

    I’ve found this:
    SELECT post_id FROM wp_postmeta WHERE meta_key=’_sku’ AND meta_value=’MD1176′;
    but it gives a blank product Id for that SKU
    while this:
    SELECT post_id FROM wp_postmeta WHERE meta_key=’_sku’ AND meta_value=’MD1177′;
    gives the post_id of 678 but the product ID shoing in the product list is 2181?

    Any help will be greatly appreciated as I’ve spent quite a while reading various posts to no avail so far and I seem to be disappearing uip my own pipe at the moment!

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Problem getting some stock id’s from SKU’ is closed to new replies.