0

Here is my code:

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel('sales/order_grid_collection')
        ->addFieldToSelect('entity_id')
        ->addFieldToSelect('increment_id')
        ->addFieldToSelect('customer_id')
        ->addFieldToSelect('customer_email')
        ->addFieldToSelect('created_at')
        ->addFieldToSelect('grand_total')
        ->addFieldToSelect('base_grand_total')
        ->addFieldToSelect('status')
        ->addFieldToSelect('order_currency_code')
        ->addFieldToSelect('store_id')
        ->addFieldToSelect('billing_name')
        ->addFieldToSelect('shipping_name')
        ->addFieldToFilter('customer_id', Mage::registry('current_customer')->getId())
        ->setIsCustomerMode(true);        

    $this->setCollection($collection);
    return parent::_prepareCollection();
}

protected function _avansFilter($collection, $column)
    {
     if (!$value = $column->getFilter()->getValue()) {
                return $this;
            }
            $orderId = $collection->getData();
            $CustomerEmail = $orderId[0]['customer_email'];
            $orders = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('customer_email', $CustomerEmail)->addFieldToSelect('*');
            //$Subtotal = Mage::getSingleton('sales/order')->loadByIncrementId($orderId)->getSubtotal();

             foreach($orders as $order){
                $OID = $order->getData();
                $OrderIncrementId = $OID['increment_id'];


                    $DiscountAmount = Mage::getSingleton('sales/order')->loadByIncrementId($OrderIncrementId)->getDiscountAmount();
                    $DiscountshippingTotal = Mage::getSingleton('sales/order')->loadByIncrementId($OrderIncrementId)->getDiscountshippingTotal();
                    $FeeAmount = Mage::getSingleton('sales/order')->loadByIncrementId($OrderIncrementId)->getFeeAmount();
                    $ShippingAmount = Mage::getSingleton('sales/order')->loadByIncrementId($OrderIncrementId)->getShippingAmount();
                    $Subtotal = Mage::getSingleton('sales/order')->loadByIncrementId($OrderIncrementId)->getSubtotal();
                    $Grandtotal = Mage::getSingleton('sales/order')->loadByIncrementId($OrderIncrementId)->getGrandTotal();
                    $AvansMethod = Mage::getSingleton('sales/order')->loadByIncrementId($OrderIncrementId)->getAvansMethod();

                    $GSubTotal = $Subtotal + $FeeAmount + $DiscountAmount + $DiscountshippingTotal;



                    if($AvansMethod == 2){
                        $GSubTotal = $GSubTotal / 2;
                        if($GSubTotal > $value){
                            $SearchID .= "$OrderIncrementId";
                        }
                    }           
                    if($AvansMethod == 3){
                        $GSubTotal = '00,00';
                        $SearchID .= "$OrderIncrementId";
                    }

                }
            Mage::log(''.$this.'', null, 'grids.log', true);    

            $this->getCollection()->getSelect()->where(
                "increment_id like ?"
            , "%$value%");


            return $this;

    }

I do not know why but after i added $orderId = $collection->getData(); in my protected function _avansFilter($collection, $column) the function seems to not return anything when i use return $this.

When i remove this line $orderId = $collection->getData(); the variable $this is starting again to be working.(ofcource returing not the data i need but returning any data)

It is somehow stopping return any data when i use $orderId = $collection->getData(); in the function, why ?

Looking forward for your response

0

I'm not sure what your _avansFilter method does, but calling $collection->getData() will load the collection. I mean it will run the sql query and retrieve all the data.
calling $collection->load() or getData after this point will not run the sql query again. It will return the same results as before even if you added more filters or orders to the collection object in the meantime.
$collection and $this->getCollection() most probably are the same thing since objects are passed by reference in PHP5.
So your changes to the collection are not taken into consideration after calling getData.

| improve this answer | |
  • I've made a lot of tests and simply when i remove $orderId = $collection->getData(); the variable $this is starting again to return any results. I do not understand why? Can you suggest any changes to my code regarding this problem ? – Venelin Vasilev Aug 16 '16 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.