O
O
Oleg Batishchev2014-07-31 19:02:31
Magento
Oleg Batishchev, 2014-07-31 19:02:31

Re-indexing in Magento does not work programmatically. Everything is fine on the web. What could it be?

There is a product that is out of stock, it is displayed in the catalog. If you reindex programmatically, it still remains visible. But if you re-index through the web interface, then it disappears as it should, because the "Show products out of stock" option is set to NO. Version 1.7.0.2
I looked in the changelog, I did not find such a fix.
Reindexing code.

foreach (Mage::getResourceModel('index/process_collection')->getItems() as $p) {
                    $this->log('Reindexing '.$p->getIndexerCode());
                    try{
                        $p->reindexAll();
//                        $p->reindexEverything(); // такое же поведение
                    } catch( Exception $e ) {
                        $this->log('Error during reindex '.$e->getMessage());
                    }
                    $this->log($p->getIndexerCode().' was reindexed');
            }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Fedyuk, 2014-08-19
@dfediuk

One of the reasons for incorrect work in Magento of programmatic rebuilding of calculation tables (reindexing) is the execution of this operation outside the context of the administrative part of the store.
All programming operations related to changing products must be performed only in the context of the administrative part of the store .
The Russian assembly of Magento has Df_Admin_Model_Mode::begin() and Df_Admin_Model_Mode::end() methods and their short analogs: rm_admin_begin() and rm_admin_end() functions to enable/disable the administrative mode .
The Df_Admin_Model_Mode::begin() method , in particular, is implemented like this:

/** @return void */
public function begin() {
    $this->_counter++;
    if (1 === $this->_counter) {
        $this->_currentStore = Mage::app()->getStore();
        $this->_updateMode = Mage::app()->getUpdateMode();
        /**
         * Очень важный момент!
         * Если Magento находится в режиме обновления,
         * то Mage_Core_Model_App::getStore()
         * всегда будет возвращать Mage_Core_Model_App::getDefaultStore(),
         * даже для такого кода: Mage_Core_Model_App::getStore(999).
         * Это приводит к весьма некорректному поведению системы в некоторых ситуациях,
         * когда мы обновляем товарные разделы своим установочным скриптом:
         * @see Mage_Catalog_Model_Resource_Abstract::_saveAttributeValue():
         * $storeId = (int)Mage::app()->getStore($object->getStoreId())->getId();
         * Этот код заведомо вернёт неправильный результат!
         */
        Mage::app()->setUpdateMode(false);
        Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
    }
}

Df_Admin_Model_Mode::end() method :
/** @return void */
public function end() {
    df_assert_gt0($this->_counter);
    $this->_counter--;
    if (0 === $this->_counter) {
        Mage::app()->setCurrentStore($this->_currentStore);
        Mage::app()->setUpdateMode($this->_updateMode);
        unset($this->_currentStore);
        unset($this->_updateMode);
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question