DarkVader
DarkVader

Reputation: 127

How to get product collection of products with no website Id in Magento?

I know that I have some products in Magento catalog that are missing websiteID totally. I am trying to find these products like this:

  $products = Mage::getModel('catalog/product')
  ->getCollection()
  ->addAttributeToSelect('*')
  ->addAttributeToFilter('website_ids', 'null');

This will however crash. What is the correct method of fetching product collection of products without website_id set?

Upvotes: 2

Views: 4479

Answers (2)

Rimšiakas
Rimšiakas

Reputation: 11

This worked for me

SELECT catalog_product_entity.entity_id
FROM catalog_product_entity
LEFT JOIN catalog_product_website
    ON catalog_product_entity.entity_id = catalog_product_website.product_id
WHERE catalog_product_website.product_id IS NULL

Upvotes: 1

Elavarasan
Elavarasan

Reputation: 2669

All product website ids are stored in catalog_product_website. If you open this table here you can see the products associated with websites.

And here is the filter,

<?php

require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();

echo '<pre>';
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addWebsiteFilter('0');   

//echo $collection->getSelect(); exit;
$collection->getFirstItem();
 // ->addAttributeToFilter('website_ids', 'null');

//echo $orders->getSelect();

//exit;

 print_r($collection);

In this $collection->addWebsiteFilter('0'); filtering the webssite ids (currently 0), so you can change the value as1,0,2 or NULL. That's it.

In your case try with NULL or empty like ''.

Update:

If you filter with '' you will get error, because the table catalog_product_website having foreign key with product entity id and website id, which means the the website_id can't be empty. So try with direct query. In magento you can get query from collection by this,

echo $collection->getSelect(); exit;

or use this,

SELECT `e`.* FROM `catalog_product_entity` AS `e`
 INNER JOIN `catalog_product_website` AS `product_website` ON product_website.product_id = e.entity_id AND product_website.website_id IN('')

If you have any doubts, please comment here.

Upvotes: 2

Related Questions