skus を含む別の配列に基づいて製品コレクションを並べ替えることはできますか?

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

  •  16-10-2019
  •  | 
  •  

質問

現在のカテゴリに属する​​いくつかの SKU を含む配列があります。

その配列に基づいてコレクションの順序を変更し、順序配列に存在する SKU が最初になるようにしたいと考えています。

このために、list.phtmlファイルを編集しようとしています

$category = Mage::registry('current_category');
$inArray = explode(',', $category->getData('top_skus'));

usort($_productCollection, function($a, $b) use ($inArray){ 
    print_r($a);
    $aAge = $a['sku'];
    $bAge = $b['sku'];
    $aWeight = 0;
    $bWeight = 0;
    if (in_array($aAge, $inArray)) 
        $aWeight++;

    if (in_array($bAge, $inArray)) 
        $bWeight++;

    if ($aWeight != $bWeight) {
        return $aWeight > $bWeight ? -1 : 1;
    } else if ($aWeight > 0) {
        // need to sort by order which specified in array
        $aIndex = array_search($aAge, $inArray);
        $bIndex = array_search($bAge, $inArray);
        return ($aIndex == $bIndex ? 0 : ($aIndex > $bIndex ? 1 : -1));
    } else {
        // just compare age values
        return ($aAge == $bAge ? 0 : ($aAge > $bAge ? 1 : -1));
    }
});

このようなエラーが発生します

Warning: usort() expects parameter 1 to be array, object given  in C:\wamp\www\magento1\app\design\frontend\base\default\template\catalog\product\list.phtml on line 74

製品を含む配列を usort に提供するにはどうすればよいですか?その配列はどこから取得できますか?

役に立ちましたか?

解決

次のようにコレクションを構築していると仮定します。

$_productCollection = Mage::getModel('catalog/product')->getCollection()->...;

そして電話をかける

usort($_productCollection, ...)

それは機能しません、なぜなら $_productCollection は配列ではありませんが、エラー メッセージからそれがわかりました (私は単にキャプテン オブビウスを言っているだけです)。
代わりにこれを試してみることもできます。

$productArray = (array)$_productCollection->getIterator();
usort($productArray, ...);

他のヒント

私はしばらく前に、製品コレクション内の各項目のオブジェクトに存在する特定の値による並べ替えが必要なショップでこれを実行しました。

このために、カスタム拡張機能を作成し、 Mage_Catalog_Block_Product_List 以下に示すようなクラス。

class [Namespace]_[Module]_Block_Mage_Catalog_Product_List extends Mage_Catalog_Block_Product_List
{

    protected function _getProductCollection()
    {

        if( is_object(Mage::registry('current_category')) ) {
            $collection = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter(Mage::registry('current_category'));
        } else {
            $collection = Mage::getResourceModel('catalog/product_collection');
        }
        Mage::getModel('catalog/layer')->prepareProductCollection($collection);

        $collection->addAttributeToSelect('category_sort_value');


        // Do some fancy sorting
        $collectionReflection = new ReflectionObject($collection);
        $itemsPropertyReflection = $collectionReflection->getProperty('_items');
        $itemsPropertyReflection->setAccessible(true); // Make it accessible

        $collectionItems = $itemsPropertyReflection->getValue($collection);

        usort($collectionItems, array("[Namespace]_[Module]_Block_Mage_Catalog_Product_List", "cmp"));

        $itemsPropertyReflection->setValue($collectionReflection, $collectionItems);

        $itemsPropertyReflection->setAccessible(false); // Return restriction back
        // End them fancyness


        $this->_productCollection = $collection;

        return $this->_productCollection;
    }


    public function cmp($a, $b) 
    {
        $a_sort = (int)$a->getData('category_sort_value');
        $b_sort = (int)$b->getData('category_sort_value');

        if ($a_sort == $b_sort) return 0;
        return ($a_sort < $b_sort) ? -1 : 1;
    }
}

これは基本的にはの派手なバージョンです usort オブジェクト用。ちょっとした補足...これには PHP 5.3 が必要です。

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