Magento 1 – check if sku exists
For some imports it is needed to check if SKU exists. In this tutorial, I show you how to check if a given SKU is already in your system. You may run a price or stock update of a products list from an ERP program, where not all products are in shop. For this you only want to update existing products.
Magento 1 – check if sku exists
In my import model, I loop through all products from given list (array). There I check for existing SKUs with following code:
1 2 3 4 5 6 | $id = Mage::getModel('catalog/product')->getIdBySku($data['sku']); if ($id == false) { echo "skip SKU: ".$data['sku']."\n"; return; } echo "update SKU: ".$data['sku']."\n"; |
Product function getIdBySku does the trick. It returns product id or false, if no product with given SKU was found. I check for this return value and stop, if a product id was returned. This is the most useful method. If you run a find command on Mage Core, you will find many lines, where this is used.
Conclusion
I showed you an easy to use method of to check if SKU exists in your products tables. It is the fastest possible way and is used many times in Mage Core methods.