skip to Main Content

Combining this topic Update WooCommerce product price and stock combined with fgetcsv and fopen

My goal is to update the stock of the products on a woocommerce website from a csv file. With help of cron and the REST API of wordpress, I can update stock almost realtime. If done, I may try to put it in functions.php so that stock changes every time site loads.

EXAMPLE.csv has two columns: SKU and inStock.

Step 1 is to get the csv to an array. Using fgetcsv and fopen, it’s possible to do it like this:

$csv = array();
$file = fopen('EXAMPLE.csv', 'r');

while (($result = fgetcsv($file, 0, ";")) !== false)
{
    $csv[] = $result;
}

Now, I have the csv as an array. Step 2 is to use this array to change the stock quantity in wordpress. First check if the SKU exists, if it exists then change the stock quantity of the product related to the SKU.

Continue code

$arr = print_r($csv);
        
    foreach ( $arr as $single ) {
        $product_id = wc_get_product_id_by_sku( $single['sku'] );
        if ( ! $product_id ) {
            continue;
        }
        $product = new WC_Product( $product_id );
        if ( ! $product ) {
            continue;
        }
    
        $product->set_stock_quantity( $single['inStock'] );
        $product->save();
    }
    
    
    ?>

There is one problem: $arr = print_r($csv); is not returning the proper array.
It works when I manually change it to:

$arr = array(
    array( 'SKU' => '123456787', 'inStock' => '2' ),
    array( 'SKU' => '123456788', 'inStock' => '1' ),
    array( 'SKU' => '123456789', 'inStock' => '8' )
);

So that’s where it goes wrong. $arr = print_r($csv); outputs

Array
(
    [0] => Array
        (
            [0] => EAN
            [1] => inStock
        )

    [1] => Array
        (
            [0] => 123456787
            [1] => 2
        )

    [2] => Array
        (
            [0] => 123456788
            [1] => 1
        )

    [3] => Array
        (
            [0] => 123456789
            [1] => 8
        )
)

I searched and there are various solutions for the problem, but all not combined. Maybe it’s an easy fix on the array. I tried nearly eveything.. Could you please help me? What will be the best solution?

2

Answers


  1. Chosen as BEST ANSWER

    Found the answer, got great help from a friend!

    To make proper array: replace:

    $csv[] = $result;
    

    with:

    $csv[] = array( 'SKU' => $result[0], 'inStock' => $result[1]);
    

    And

    remove $arr = print_r($csv);

    Code becomes:

    $csv = array();
    $file = fopen('example.csv', 'r');
    
    
    while (($result = fgetcsv($file, 0, ";")) !== false)
    {
        $csv[] = array( 'SKU' => $result[0], 'Stock' => $result[1]);
    }
    
        
    foreach ( $csv as $single ) {
            $product_id = wc_get_product_id_by_sku( $single['SKU'] );
            if ( ! $product_id ) {
                continue;
            }
            $product = new WC_Product( $product_id );
            if ( ! $product ) {
                continue;
            }
        
            $product->set_stock_quantity( $single['Stock'] );
            $product->save();
        }
    

    What this does, it reads a csv with two columns: SKU and Stock and then checks if SKU exists in the woocommerce. If it does, it will change the stock of the SKU.


  2. If you use $product = wc_get_product( $product_id ) instead of $product = new WC_Product( $product_id ) then it will also work with product variants (with SKU).

    Because new WC_Product( $product_id ) does not give handle if the SKU is a product variant. It gives error message, while wc_get_product( $product_id ) works correctly in both cases.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search