0

I am using Magento 1.9.1 and i am working on a extension which allows you to add more mass update statusses on the orders page.

Here take a look:

enter image description here

So here is my config file:

<?xml version="1.0"?>
<config>
  <modules>
    <VivasIndustries_MassCustomStatusses>
      <version>0.1.0</version>
    </VivasIndustries_MassCustomStatusses>
  </modules>
<admin>
    <routers>
        <masscustomstatusses>
            <use>admin</use>
            <args>
                <module>VivasIndustries_MassCustomStatusses</module>
                <frontName>masscustomstatusses</frontName>
            </args>
        </masscustomstatusses>
    </routers>
</admin>
<global>
    <models>
        <masscustomstatusses>
            <class>VivasIndustries_MassCustomStatusses_Model</class>
        </masscustomstatusses>
    </models>   
    <blocks>
        <adminhtml>
            <rewrite>
                <sales_order_grid>VivasIndustries_MassCustomStatusses_Block_Sales_Order_Grid</sales_order_grid>
            </rewrite>
        </adminhtml>
    </blocks>
<sales> 
    <order> 
        <states>
            <novichka translate="label">
                <label>Nai nova</label>
                <statuses>
                    <novichka default="1"/>
                </statuses>
                <visible_on_front>1</visible_on_front>
             </novichka>
        </states>
    </order>
</sales>    
</global>
</config>

It seems i have problem with the Model part.

Here is what i have in /app/code/local/VivasIndustries/MassCustomStatusses/Model/Order.php

<?php

class VivasIndustries_MassCustomStatusses_Model_Order extends Mage_Sales_Model_Order
{
    const STATE_NOVICHKA        = 'novichka';

    public function novichka()
    {

        $this->setNovichkaBeforeState($this->getState());
        $this->setNovichkaBeforeStatus($this->getStatus());
        $this->setState(self::STATE_NOVICHKA, true);
        return $this;
    }

}

When i place this code:

const STATE_NOVICHKA        = 'novichka';

public function novichka()
{

    $this->setNovichkaBeforeState($this->getState());
    $this->setNovichkaBeforeStatus($this->getStatus());
    $this->setState(self::STATE_NOVICHKA, true);
    return $this;
}

Here: /app/code/core/Mage/Sales/Model/Order.php Evetything is working fine.

I think i have problem with the Model or maybe not ?

Can you help me out with that?

Here is the error report: http://pastebin.com/fvDJyaVS

Here is what i have in /app/code/local/VivasIndustries/MassCustomStatusses/controllers/IndexController.php:

<?php

class VivasIndustries_MassCustomStatusses_Adminhtml_IndexController extends Mage_Adminhtml_Controller_Action
{



    protected function _initAction() {
        $this->loadLayout()
            ->_setActiveMenu('sales/order')
            ->_addBreadcrumb($this->__('Sales'), $this->__('Sales'))
            ->_addBreadcrumb($this->__('Orders'), $this->__('Orders'));
        return $this;
    }   
     protected function _initOrder()
    {
        $id = $this->getRequest()->getParam('order_id');
        $order = Mage::getModel('sales/order')->load($id);

        if (!$order->getId()) {
            $this->_getSession()->addError($this->__('This order no longer exists.'));
            $this->_redirect('*/*/');
            $this->setFlag('', self::FLAG_NO_DISPATCH, true);
            return false;
        }
        Mage::register('sales_order', $order);
        Mage::register('current_order', $order);
        return $order;
    }
    public function indexAction() {
        $this->_initAction()
            ->renderLayout();
    }

    public function deleteAction() {
        if($order = $this->_initOrder()) {
            try {
                $order->delete()->save();
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Order was successfully deleted'));
                $this->_redirectUrl(Mage::helper('adminhtml')->getUrl('adminhtml/sales_order/index'));
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('order_ids')));
            }
        }
        $this->_redirectUrl(Mage::helper('adminhtml')->getUrl('adminhtml/sales_order/index'));
    }

    public function massDeleteAction() {
        $deleteorderIds = $this->getRequest()->getParam('order_ids');
        if(!is_array($deleteorderIds)) {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select item(s)'));
        } else {
            try {
                foreach ($deleteorderIds as $deleteorderId) {
                    Mage::getModel('sales/order')->load($deleteorderId)->delete();
                }
                Mage::getSingleton('adminhtml/session')->addSuccess(
                    Mage::helper('adminhtml')->__(
                        'Total of %d record(s) were successfully deleted', count($deleteorderIds)
                    )
                );
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
        }
        $this->_redirectUrl(Mage::helper('adminhtml')->getUrl('adminhtml/sales_order/index'));
    }   

    public function NewMassAction()
    {
        $orderIds = $this->getRequest()->getPost('order_ids', array());
        $countHoldOrder = 0;

        foreach ($orderIds as $orderId) {
            $order = Mage::getModel('sales/order')->load($orderId);
            if ($order->canHold()) {
                $order->novichka()
                    ->save();
                $countHoldOrder++;
            }
        }

        $countNonHoldOrder = count($orderIds) - $countHoldOrder;

        if ($countNonHoldOrder) {
            if ($countHoldOrder) {
                $this->_getSession()->addError($this->__('%s order(s) were not put on hold.', $countNonHoldOrder));
            } else {
                $this->_getSession()->addError($this->__('No order(s) were put on hold.'));
            }
        }
        if ($countHoldOrder) {
            $this->_getSession()->addSuccess($this->__('%s order(s) have been put on hold.', $countHoldOrder));
        }

        $this->_redirect('*/*/');
    }

}

Thanks in advance!

2
  • Please paste the code of the block and controller too. But most probably the problem is that you call method novichka() directly on order but if you like to do it this way you first have to rewrite Mage_Sales_Model_Order. But it's better not to rewrite block and model but to call an observer.
    – michael
    Nov 30, 2014 at 17:48
  • I've posted the controller. There is no need to post the block. Can you please help me create this observer of which you are talking about ? Nov 30, 2014 at 18:02

1 Answer 1

0

As I wrote in my comment - the problem is that you didn't rewrite the model Mage_Sales_Model_Order but you call $order->novichka(). You have to add this code in config.xml:

<models>
    <sales>
        <rewrite>
            <order>VivasIndustries_MassCustomStatusses_Model_Order</order>
        </rewrite>
    </sales>
</models>

But it's better to call an observer, not to do so many rewrites because if there are some other modules that rewrites this classes - you will have problems. :) This is an example code:

<config>
    <global>
        <models>
            <company_modulename>
                <class>Company_Modulename_Model</class>
            </company_modulename>
        </models>
    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <Company_Modulename before="Mage_Adminhtml">Company_Modulename_Adminhtml</Company_Modulename>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    <adminhtml>
        <events>
            <adminhtml_block_html_before>
                <observers>
                    <company_modulename>
                        <type>singleton</type>
                        <class>company_modulename/observer</class>
                        <method>addMassAction</method>
                    </company_modulename>
                </observers>
            </adminhtml_block_html_before>
        </events>
    </adminhtml>
</config>

class Company_Modulename_Model_Observer
{
    public function addMassAction($observer)
    {
        $block = $observer->getEvent()->getBlock();

        if ($block instanceof Mage_Adminhtml_Block_Sales_Order_Grid) {
            $block->getMassactionBlock()->addItem('item1', array(
                'label' => Mage::helper('company_modulename')->__('Item 1'),
                'url' => $block->getUrl('*/sales_classname/methodName'),
            ));
        }
    }
}

class Company_Modulename_Adminhtml_Sales_ClassnameController extends Mage_Adminhtml_Controller_Action
{
    public function methodName()
    {

        $this->_redirect('*/sales_order/');
    }
}

You can search for add mass action with an observer (adminhtml_block_html_before).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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