3

For example, I have catalog with SKU's:

ABCDE-3
HIJKL-6
ABCDE-5
VWXYZ-6
ABCDE-21

I want to fetch products that have SKU that starts from ABCDE-, so it'll return:

ABCDE-3
ABCDE-5
ABCDE-21

Is there any way to fetch the products by part of SKU in Magento? Or I'll have to add similar products manually?

Thank you.

2
  • For internal Magento search use the code provided, if you need higher accuracy you need Sphinx (60-80% accurate) or Solr (98% accurate) which perform partial searches.
    – user2935
    Mar 6, 2014 at 17:29
  • I see that you have accepted Marius answer. Have you used his code on a list or on product's page?
    – kanenas
    Jun 17, 2016 at 8:37

2 Answers 2

3
$skuPart = 'ABCDE-';

$collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes()) //add default attributes to collection
            ->addMinimalPrice() // add minimal price to collection
            ->addFinalPrice() //add final price to collection
            ->addTaxPercents() //add tax percents to collection
            ->addAttributeToFilter('sku', array('like'=>$skuPart.'%')); //filter by sku part
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection); //filter only active products
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection); //filter only products visible in catalog
1
  • @Marvin3. Thanks for the edit. Damn copy/paste. I forgot to check twice.
    – Marius
    Mar 6, 2014 at 12:52
3

Try this one:

$productCollection = Mage::getModel('catalog/product')->getCollection()
    ->addFieldToFilter('sku', array('like' => 'ABCDE-%'));

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.