3

This really does not seem like it should be difficult. I seem to be able to change a product's status for a given store, but not read it.

Below is a test script I've been using,

<?php
// Make this a Magento app
require_once '/var/www/html/magento/app/Mage.php';
Mage::app();

echo "Loading product ".$argv[1]."\n";
$product = Mage::getModel('catalog/product')->setStore(2)->setStoreId(2)
->load($argv[1]);

echo "Product id is ".$product->getId()."\n";
echo "Product status is now ".print_r(Mage::getModel('catalog/product_status')->getProductStatus($product->getId(),2),true)."\n";
// This always prints:
// Product status is now Array
// (  
//    [498] => -1
// )

echo "enabling product... ";
Mage::getModel('catalog/product_status')->updateProductStatus($product->getId(), 2, Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
echo "done\n";
// This actually seems to work, and change the status of the product in that store's view.

echo "Product status is now ".print_r(Mage::getModel('catalog/product_status')->getProductStatus($product->getId(),2),true)."\n";
// This always prints the same -1 response as the first time
?>

And that's it. Pretty short and simple, and changing the product status in store 2 does appear to work, just not reading it.

Interestingly enough, both things do work if I use this:

echo "Product status is now ".$product->getStatus()."\n";

echo "enabling product... ";
$product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)->save();
echo "done\n";

echo "Product status is now ".$product->getStatus()."\n";

Except, after running that, in the store view all the attributes that had been set to "use default" now aren't. As a last resort that might work, but definitely not what I had in mind.

2 Answers 2

3

So, I have a solution that does the job, but I definitely still would love to hear from anyone that can explain why I'm having to do it the way I'm doing it, and why the the two ways I have in my question don't work entirely right.

What I did, though, was simply to take the part from each that did work, and use them together:

echo "Loading product ".$argv[1]."\n";
$product = Mage::getModel('catalog/product')->setStore(2)->setStoreId(2)->load($argv[1]);


echo "Product status is now ".$product->getStatus()."\n";

echo "enabling product... ";
Mage::getModel('catalog/product_status')->updateProductStatus($product->getId(), 2, Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
echo "done\n";

$product->load($argv[1]);
echo "Product status is now ".$product->getStatus()."\n";

Seems strange that I needed to, but it appears to do the job, so it's good enough for this one-off script.

2

I came across the same issue today. I found that this is because the getProductStatus method in Mage_Catalog_Model_Resource_Product_Status is bugged.

Look at this code:

foreach ($productIds as $productId) {
    if (isset($rows[$productId])) {
        $statuses[$productId] = $rows[$productId];
    } else {
        $statuses[$productId] = -1;
    }
}

It checks if the query used to get the status found it. The query result is an array (because it's using fetchPairs()) with status as key and product ID as value. BUT in the code above you can see that it expects the product ID to be the array's key, not value.

Obviously the if condition fails and the product gets the status -1.

The solution would be to rewrite this function and insert $rows = array_flip($rows); before the foreach block.

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.