54 lines
2.2 KiB
PHP
54 lines
2.2 KiB
PHP
<?php
|
|
/**
|
|
*
|
|
* Copyright © Magento, Inc. All rights reserved.
|
|
* See COPYING.txt for license details.
|
|
*/
|
|
namespace Stemcell\Sales\Controller\Order;
|
|
|
|
use Magento\Framework\App\Action\HttpGetActionInterface;
|
|
use Magento\Sales\Api\OrderManagementInterface;
|
|
use Magento\Sales\Api\OrderRepositoryInterface;
|
|
use Magento\Framework\Controller\ResultFactory;
|
|
use Magento\Sales\Model\Order;
|
|
|
|
class Cancel implements HttpGetActionInterface
|
|
{
|
|
|
|
public function __construct(
|
|
protected OrderRepositoryInterface $orderRepository,
|
|
protected OrderManagementInterface $orderManagement,
|
|
protected \Magento\Framework\App\RequestInterface $request,
|
|
protected \Magento\Customer\Model\Session $customerSession,
|
|
protected ResultFactory $resultRedirectFactory,
|
|
protected \Magento\Framework\Message\ManagerInterface $messageManager
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\Result\Redirect|(\Magento\Framework\Controller\Result\Redirect&\Magento\Framework\Controller\ResultInterface)|\Magento\Framework\Controller\ResultInterface|void
|
|
*/
|
|
public function execute()
|
|
{
|
|
$resultRedirect = $this->resultRedirectFactory->create(ResultFactory::TYPE_REDIRECT);
|
|
|
|
try {
|
|
|
|
$order = $this->orderRepository->get($this->request->getParam('order_id'));
|
|
if ($order->getCustomerId() == $this->customerSession->getCustomerId() && $order->getState() == Order::STATE_NEW) {
|
|
$this->orderManagement->cancel($order->getEntityId());
|
|
$order->addCommentToStatusHistory('用户主动取消订单', Order::STATE_CANCELED, false);
|
|
$this->orderRepository->save($order);
|
|
} else {
|
|
$this->messageManager->addErrorMessage(__('您不能取消当前订单'));
|
|
}
|
|
|
|
$this->messageManager->addSuccessMessage(__('You canceled the order.'));
|
|
} catch (\Exception $e) {
|
|
$this->messageManager->addErrorMessage(__('You have not canceled the item.'));
|
|
return $resultRedirect->setPath('sales/order/history');
|
|
}
|
|
return $resultRedirect->setPath('sales/order/view', ['order_id' => $order->getEntityId()]);
|
|
}
|
|
}
|