フロントエンドでフラットイネーブルを有効にするたびに製品コレクションを無効にする方法

magento.stackexchange https://magento.stackexchange.com//questions/69420

質問

私のシステムのフラット設定はenable for productsで、Magento System Magento exclude disable products from product collection whenever flat table is creating.のように、not get disable product collection on frontend

今私はto get product collection with disable product atフロントエンドを望みます。

はコードwith set store id as admin for storeの下にみましたが、それはdoes not works.

私を助けてください

試着

$coutPro=Mage::getModel('catalog/product')->getCollection()-
    ->addAttributeToFilter('amit_status', array(
    'nin' => array(8),
    ))->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID);
.

役に立ちましたか?

解決

この方法を見てMage_Catalog_Model_Resource_Product_Collection::_construct

protected function _construct()
{
    if ($this->isEnabledFlat()) {
        $this->_init('catalog/product', 'catalog/product_flat');
    }
    else {
        $this->_init('catalog/product');
    }
    $this->_initTables();
}
.

isEnabledFlatの結果が真の場合、コレクションはフラットテーブルを使用することを意味します。
isEnabledFlatを見ている場合は、admoneの場合はfalseが返されます。それは各ストアのフラットフラグのステータスを返します。実際にはそれ以上です。フラグが有効になっていても、フラットインデックスが最新のものではない場合はfalseが返される可能性があります。

だからあなたができることはあなたのコードの上部にこれを追加することです:

Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
.

これにより、コレクションは管理ストアを使用します。

[編集]
これをフロントエンドで使用する場合は、adminからの値が表示され、それ以外の値が表示されないため、問題が発生します。

私の2番目の考えは、製品収集モデルを書き換えて、メソッドisEnabledFlatの動作を変更して、次のように見えます。

public function isEnabledFlat()
{  
    //add this
    if (Mage::registry('use_product_eav')) {
        return false;
    }
    return parent::isEnabledFlat();
}
.

あなたのコレクションをインスタンス化する前に、このMage::register('use_product_eav', true)をコードに追加するだけです。
残りのページがeavテーブルを使用したくない場合は、それをインスタンス化した後にMage::unregister('use_product_eav')を呼び出してください。

他のヒント

私はいくつかの無効な製品の数を取得するだけでよいので、私はマリウスの最初の答えを取って、次のようにそれを修正しました:

// Temporarily set the store to the admin one, otherwise the collection below will omit disabled products
$oldStoreId = Mage::app()->getStore()->getId();
$newStoreId = Mage_Core_Model_App::ADMIN_STORE_ID;
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load($newStoreId));

// Search for disabled products
$disabledProducts = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_DISABLED));

// Reset the store to what it was before
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load($oldStoreId));
.

ライセンス: CC-BY-SA帰属
所属していません magento.stackexchange
scroll top