Magento – How to change programmatically product stock status when product sku doesn’t appears in provider data feed

importmagento-1.9out-of-stockstock-status

I'm using Magento CE 1.9.1
I made an import script to import products from a provider xml feed.
This feed is update everyday at 3 a.m .
My profider send me in his xml only products where are in stock in his shop.

So how can I update my product stock statut to out of stock (by product sku) when the product sku doesn't exist in the xml feed ?

I put an example to explain what I want, data provided in the xml :

Monday :        Tuesday :

Sku   qty       Sku    qty

ABC   22        ABC    12
BDE   30        FGH    4
FGH   15

What I have to do :
Tuesday I have to set programmatically my product BDE to "out of stock" because it doesn't appear in the xml.

There is my import script :

    $file = 'feed.xml'; 

    $feed = simplexml_load_file($file);

    foreach($feed as $product){

        //some datas in xml feed
        $sku = $product->identifiant_unique;
        $prix = $product->prix;
        $titre = $product->categorie3;
        $quantiteStock = $product->quantiteStock;
        $poid = $product->poids_net;

        //Setters 
        $produit = Mage::getModel('catalog/product');
        $produit->setName($titre.' '.$sku);
        $produit->setSku($sku);
        $produit->setWeight($poid);
        $produit->setAttributeSetId(4);
        $produit->setDescription($titre.' '.$sku);
        $produit->setShortDescription($titre.' '.$sku);
        $produit->setTypeId($product['type_id'])->setWebsiteIds(array(1))->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
        $produit->setPrice($prix);

        $produit->setIsMassupdate(true);
        $produit->setExcludeUrlRewrite(true);
        $produit->save();

        // For Inventory Management
        $stockItem = Mage::getModel('cataloginventory/stock_item');
        $stockItem->assignProduct($produit);
        $stockItem->setData('is_in_stock', 1);
        $stockItem->setData('stock_id', 1);
        $stockItem->setData('store_id', 1);
        $stockItem->setData('manage_stock', 1);
        $stockItem->setData('use_config_manage_stock', 0);
        $stockItem->setData('min_sale_qty', 1);
        $stockItem->setData('use_config_min_sale_qty', 0);
        $stockItem->setData('max_sale_qty', 1000);
        $stockItem->setData('use_config_max_sale_qty', 0);
        $stockItem->setData('qty', $quantiteStock);
        $stockItem->save();
    }

Thanks

Best Answer

You can create one table in your database with all the product entry in it.

After update on Monday structure of DB.

sku   qty   created_at            flag

ABC   22    date("Y-m-d H:i:s")    0
BDE   13    date("Y-m-d H:i:s")    0
FGH   15    date("Y-m-d H:i:s")    0

On Tuesday set flag to 1 for the available sku. So after that on Tuesday structure of DB.

sku   qty   created_at            flag

ABC   12    date("Y-m-d H:i:s")    1
BDE   13    date("Y-m-d H:i:s")    0
FGH    4    date("Y-m-d H:i:s")    1

retrieve all the sku with the flag=0 and set their 'is_in_stock' to 0. after that update you table with the available sku on tuesday and set their flag to 0 for nextday.

After update on Tuesday structure of DB.

sku   qty   created_at            flag

ABC   12    date("Y-m-d H:i:s")    0
BDE   13    date("Y-m-d H:i:s")    0 (You can remove old entry as per you logic)
FGH   04    date("Y-m-d H:i:s")    0