0

I create custom status action class to change specific order to my custom status, I created "Packing" class, that change order status to 'packing'. it's working fine on Magento 2.1.5, but if I using it on Magento 2.1.2( I have some legacy product.) it always return 500 internal error.

This is Packing class which I overrode.

class Packing extends \Magento\Sales\Controller\Adminhtml\Order {

const ADMIN_RESOURCE = 'Magento_OrderStatus::packing';

/**
 * Packing order
 *
 * @return \Magento\Backend\Model\View\Result\Redirect
 */
public function execute()
{
    $resultRedirect = $this->resultRedirectFactory->create();
    $orderId = $this->getRequest()->getParam('order_id');
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId);
    if ($order) {
        try {
            $order->setStatus('packing');
            $order->save();
            $this->messageManager->addSuccess(__('You put the order on packing.'));
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addError($e->getMessage());
        } catch (\Exception $e) {
            $this->messageManager->addError(__('You have not put the order on packing.'));
        }
        $resultRedirect->setPath('sales/order/view', ['order_id' => $order->getId()]);
        return $resultRedirect;
    }
    $resultRedirect->setPath('sales/*/');
    return $resultRedirect;

}
}

please tell me if you need more code. Thank you for answering my question.

edited

debugger mode showed me Magento\Sales\Controller\Adminhtml\Order::__construct() must be an instance of Magento\Backend\App\Action\Context

error function resides on Order.php from Sales Module.

/**
 * @param Action\Context $context
 * @param \Magento\Framework\Registry $coreRegistry
 * @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
 * @param \Magento\Framework\Translate\InlineInterface $translateInline
 * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
 * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
 * @param \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory
 * @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
 * @param OrderManagementInterface $orderManagement
 * @param OrderRepositoryInterface $orderRepository
 * @param LoggerInterface $logger
 *
 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
public function __construct(
    Action\Context $context,
    \Magento\Framework\Registry $coreRegistry,
    \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
    \Magento\Framework\Translate\InlineInterface $translateInline,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory,
    \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
    \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory,
    \Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
    OrderManagementInterface $orderManagement,
    OrderRepositoryInterface $orderRepository,
    LoggerInterface $logger
) {
    $this->_coreRegistry = $coreRegistry;
    $this->_fileFactory = $fileFactory;
    $this->_translateInline = $translateInline;
    $this->resultPageFactory = $resultPageFactory;
    $this->resultJsonFactory = $resultJsonFactory;
    $this->resultLayoutFactory = $resultLayoutFactory;
    $this->resultRawFactory = $resultRawFactory;
    $this->orderManagement = $orderManagement;
    $this->orderRepository = $orderRepository;
    $this->logger = $logger;
    parent::__construct($context);
}

1 Answer 1

0

In the case of internal server 500 kindly check system.log or exception.log in that you will find more about error.

for more do one thing. go to app/bootstrap.php in that uncomment below line approx line number 11

ini_set('display_errors', 1);

after that it will show you exact error. so you can get idea about what is wrong and you can fix that thing.

Try below code it will work for you.

public function __construct(
    Magento\Backend\App\Action\Context $context,
    \Magento\Framework\Registry $coreRegistry,
    \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
    \Magento\Framework\Translate\InlineInterface $translateInline,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory,
    \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
    \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory,
    \Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
    OrderManagementInterface $orderManagement,
    OrderRepositoryInterface $orderRepository,
    LoggerInterface $logger
) {
    $this->_coreRegistry = $coreRegistry;
    $this->_fileFactory = $fileFactory;
    $this->_translateInline = $translateInline;
    $this->resultPageFactory = $resultPageFactory;
    $this->resultJsonFactory = $resultJsonFactory;
    $this->resultLayoutFactory = $resultLayoutFactory;
    $this->resultRawFactory = $resultRawFactory;
    $this->orderManagement = $orderManagement;
    $this->orderRepository = $orderRepository;
    $this->logger = $logger;
    parent::__construct($context);
}

Hope this will help.

Thanks.

7
  • thank you for your answers, it show me Magento\Sales\Controller\Adminhtml\Order::__construct() must be an instance of Magento\Backend\App\Action\Context. I didn't override any thing about this object. Have anythings difference between 2.1.2 and 2.1.5 about this object ?
    – DrNutsu
    Commented May 29, 2017 at 7:36
  • Actually you forgot to call parent constructor that's why it is showing this error. you need to call parent controller then it will work in both version Commented May 29, 2017 at 11:05
  • thanks for reply, I already called parent construct and passed context like this parent::__construct($context);
    – DrNutsu
    Commented May 29, 2017 at 15:20
  • please update your code in question Commented May 30, 2017 at 5:05
  • I already updated my question. the error line is reside in Order.php from module Sales.
    – DrNutsu
    Commented May 30, 2017 at 7:54

Your Answer

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

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