8

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

3
  • I have the exact same problem. You have 2 options here. Either keep both scripts (on a 2 days basis) and compare them and put the missing products out of stock. Or tell your data provider to provide you with the same file with stocks=0 for the products that are out of stock (this is the cleanest method in my opinion). Commented Apr 21, 2016 at 13:48
  • Sorry for the late reply. Apparently with the contract whe have with our provider, he can't provide us out of stock products so i need to use your 1st option
    – JayD
    Commented Apr 25, 2016 at 7:48
  • Well, good luck to you, won't be an easy task. Commented Apr 25, 2016 at 8:00

2 Answers 2

1

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
0

Do you have info (attribute for example "source") with which you can get all products imported from the feed?

If you can identify products which are related too feed every time when you update the feed you need to do:

  1. Disable all products which source is current feed.
  2. Enable only products (with proper qty) which are still in the feed. All other products which are already gone, will stay with status "disabled".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.