3

I wrote a script that searches for products with no active categories and disables the products. This was in regards to our customer turning off some old categories, but the products still being searchable. We figured it was best to set their status to disabled in bulk.

So in order to do this we call this line:

Mage::getModel('catalog/product_status')->updateProductStatus($prod->getId(), $store_id, Mage_Catalog_Model_Product_Status::STATUS_DISABLED);

So a week later, we need to re-enable a product. I log into the admin panel, and the status is still showing as "enabled". It took me running my script again hard coding the id to get it to show up in the store again:

Mage::getModel('catalog/product_status')->updateProductStatus(370, $store_id, Mage_Catalog_Model_Product_Status::STATUS_ENABLED);

Obvioulsy my intention was that the status in the admin panel would be disabled, and then any user with admin access would be able to turn these products back on. So I'm asking the community, what do the above lines actually do? And how can I do what I intended.

1 Answer 1

5

This is kind of a two part question.

First you are saying:

This was in regards to our customer turning off some old categories, but the products still being searchable.

Instead of "disabling", I would create a script that changes the visibility from "Catalog, Search" to just "Catalog"

This code would look like something like this:

$productId = ???;
$product = Mage::getModel('catalog/product');
$product->load($productId);
$product->setVisibility(2);
$product->save();

You would also need code to search and switch the visibility back in the event it goes back into a Category.

Note: Since you have multiple websites, you may need to add code specifically to a website ID. Also, since you have multiple admins changing product data, a admin logger would be a great idea since I have heard horror stories about random admin users changing product data.

The second part of the question is

What do the above lines actually do?

The code does disabled and enable products, however it seems that you ran into a issue. There are other ways in doing this as well, however what you posted above is the most common way (especially on SE). Things I would look at is if your "status" attribute is a global or website variable. By default is "website". I would take a look at what when you are in the admin panel is if you are on the default view or if you are in the website specific view. The code above will always appear to be "Enabled" in the default view, but it will get disabled in the website view.

I would create a custom log in your script to see write what product was changed, and when it was changed.

1
  • Thanks! You were right it disabled it on the store scope not the global scope!
    – CD Brian
    Jan 16, 2015 at 19:50

Your Answer

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

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