0

I want to only select products that have the website id of 1 and only 1, I don't want products that has websites ids of 1,2,3,... .

I want to select only products that are assigned to only website id 1, if I do the

$websiteIds=1
    $productCollection = Mage::getResourceModel('catalog/product_collection');
    $productCollection->addWebsiteFilter($websiteIds);

it does not show me what I want, it has products with website id 1 and other websites as well.

I think the solution is to use something like

addfieldtoFilter('website_ids',array('eq'=>(1)))
addfieldtoFilter('website_ids',array('neq'=>(2,3,4)))

however we have no such thing as website_ids, if I run this:

$productCollection = Mage::getResourceModel('catalog/product_collection');
$productCollection->addWebsiteFilter($websiteIds);
foreach($productCollection as $product)
{
    echo $product->getWebsiteIds();
    echo $product->getData("website_ids")
}

we don't have "website_ids" as an attribute that I can go and do it, so the echo $product->getData("website_ids") is wrong and null, I was wondering how I can achieve this. I know that I can select all products and then get count($websiteids) and if the count was 1 and also website_id was equal to 1 I can tag the product as my selected product which I am looking, but I think it should have a better solution.

P.S, for example:

Product A has following websites 1,2,3
Product B has following websites 1,2,5
Product C has following websites 1

the program should only return C, however with the solution that I have I will have A,B,C which is not my goal.

1 Answer 1

0

Try this instead:

$storeId = 1;
$productCollection = Mage::getResourceModel('catalog/product_collection');
$productCollection->addStoreFilter($storeId);

If you don't know store id then you can grab like this:

$websiteId=1;
$website = Mage::getModel('core/website')->load($websiteId); 
foreach($website->getStoreIds() as $id){
            $storeId = $id; //we grab only one, it will be the last one if there are more than 1 stores in this website.
            exit;
    }

OR

$storeIds = $website->getStoreIds();
$storeId = $storeIds[0]; //it grabs first one

Hope this helps. Ref

6
  • so you are saying if I filter by store id, it will be showing only one store id and not products that has store id 1 and other store ids?
    – Nickool
    Commented Dec 8, 2016 at 0:22
  • yes, you are correct. Commented Dec 8, 2016 at 0:23
  • Wrong as I expected, what it does is exactly what website does, when I have web ids 1,2,3 it still shows it, but I want to just get products with one store id of 1, it still shows others.
    – Nickool
    Commented Dec 8, 2016 at 0:34
  • Did yu check this magento.stackexchange.com/questions/86229/…? I doubt your products are assigned to multiple stores. Commented Dec 8, 2016 at 0:36
  • they are assigned to multiple websites my bad
    – Nickool
    Commented Dec 8, 2016 at 0:37

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.