‘updated_at’을 기준으로 장바구니 항목 정렬 $product=$item->getProduct(); array_push($productArray,$product); } $productArray = $this->sortArray($productArray); protected

이것이 장바구니 항목을 얻는 방법입니다.

$quote = Mage::getModel('checkout/cart')->getQuote();
$items=$quote->getAllitems();

나는 이런 식으로하고 싶다 :

$items->sortBy('updated_at','desc');

이 작업을 수행하는 올바른 마 젠토 방법은 무엇입니까?

나는 이런 식으로 일하고 싶다 :

$productArray=array();
foreach($items as $item){
    $product=$item->getProduct();
    array_push($productArray,$product);
}
$productArray = $this->sortArray($productArray);

protected sortArray($productArray){
    ...sort by updated date;
    return $sortedArray
}

그러나이 값을 쉽게 비교하기 위해 많은 dateTime 객체를 만들어야하며 이는 계산적으로 번거로운 작업 인 것 같습니다.



답변

에 의해 정렬하는 기능이 없습니다

때문에 getAllItems에서 오는 array에 개체 및 따라서 내concept you cannot sort this by field.

  public function getAllItems()
    {
        $items = array();
        foreach ($this->getItemsCollection() as $item) {
            if (!$item->isDeleted()) {
                $items[] =  $item;
            }
        }
        return $items;
    }

이 경우 getItemsCollection().seOrder 함수를 모든 필드별로 정렬하여 사용할 수 있습니다.

$items=$quote->getItemsCollection()->setOrder('updated_at', 'desc');

foreach ($items as $item) {
    if (!$item->isDeleted()) {
    //Do your code $item
    }
}

편집하다:

클래스 Mage_Sales_Model_Quote 다시 작성

그것은 것입니다 아이디어를 더 나은 위해 다시 클래스를 Mage_Sales_Model_Quote이 호출 젠토는 .IN called getAllItems()기능과 g etItemsCollection() function모든 항목을 얻기위한 반응의 주요.

  <global>
    <models>
      <magento65343>
        <class>StackExchange_Magento65343_Model</class>
        <resourceModel>magento65343_mysql4</resourceModel>
      </magento65343>
            <sales>
                <rewrite>
                    <quote>StackExchange_Magento65343_Model_Sales_Quote</quote>
                </rewrite>
            </sales>
    </models>

클래스를 다시 작성하십시오.

<?php
class StackExchange_Magento65343_Model_Sales_Quote extends Mage_Sales_Model_Quote
{
}

정렬에 setOrder () 함수를 사용하십시오.

setOrder () 종류의 함수 필드를 기준으로 항목을 수집

<?php
class StackExchange_Magento65343_Model_Sales_Quote extends Mage_Sales_Model_Quote
{
     public function getItemsCollection($useCache = true)
    {
        if ($this->hasItemsCollection()) {
            return $this->getData('items_collection');
        }
        if (is_null($this->_items)) {
            $this->_items = Mage::getModel('sales/quote_item')->getCollection()->setOrder('updated_at', 'desc');
            $this->_items->setQuote($this);
        }
        return $this->_items;
    }

}


답변

해결책:

Mage_Sales_Model_Quote::getAllVisibleItems그것으로 메소드 를 다시 작성하십시오 .

public function getAllVisibleItems()
{
    $items = array();
    foreach ($this->getItemsCollection() as $item) {
        if (!$item->isDeleted() && !$item->getParentItemId()) {
            $timeStamp = Mage::getModel('core/date')->timestamp($item->getData('updated_at'));
            $items[$timeStamp] =  $item;
        }
        ksort($items);
    }
    return $items;
}

일찍 작성 :

아마도이 상황에서는 그다지 좋은 해결책은 아니지만 명심하십시오. (마 젠토에는 유용한 곳이 많이 있습니다)

public function getAllItems()
{
    $items = array();
    foreach ($this->getItemsCollection() as $item) {
        if (!$item->isDeleted()) {
            $items[$item->getData('...')] =  $item;
        }
    }
    ksort($items)

    return $items;
}

getData () 메소드 안에는 정렬하려는 anyfield를 넣습니다. 귀하의 경우에는 가능합니다 updated_at. 삽입의 경우 updated_at는 타임 스탬프 삽입 더 나은.


답변