From 436265dcb2d34aac465c08ada210262b574d87fa Mon Sep 17 00:00:00 2001 From: Mohamed ELIDRISSI <67818913+elidrissidev@users.noreply.github.com> Date: Wed, 13 Jul 2022 14:29:45 +0200 Subject: [PATCH 1/3] Added order comment functionality to REST API (#2315) --- .../Sales/Model/Api2/Order/Comment/Rest.php | 1 + .../Api2/Order/Comment/Rest/Admin/V1.php | 63 +++++++++++++++++++ app/code/core/Mage/Sales/etc/api2.xml | 5 ++ 3 files changed, 69 insertions(+) diff --git a/app/code/core/Mage/Sales/Model/Api2/Order/Comment/Rest.php b/app/code/core/Mage/Sales/Model/Api2/Order/Comment/Rest.php index dbbea249af0..8f7d3a4c0ab 100644 --- a/app/code/core/Mage/Sales/Model/Api2/Order/Comment/Rest.php +++ b/app/code/core/Mage/Sales/Model/Api2/Order/Comment/Rest.php @@ -32,6 +32,7 @@ abstract class Mage_Sales_Model_Api2_Order_Comment_Rest extends Mage_Sales_Model * Parameters in request used in model (usually specified in route mask) */ public const PARAM_ORDER_ID = 'id'; + public const PARAM_COMMENT_ID = 'comment_id'; /**#@-*/ /** diff --git a/app/code/core/Mage/Sales/Model/Api2/Order/Comment/Rest/Admin/V1.php b/app/code/core/Mage/Sales/Model/Api2/Order/Comment/Rest/Admin/V1.php index 9246da502cd..9d05bf6cce6 100644 --- a/app/code/core/Mage/Sales/Model/Api2/Order/Comment/Rest/Admin/V1.php +++ b/app/code/core/Mage/Sales/Model/Api2/Order/Comment/Rest/Admin/V1.php @@ -27,4 +27,67 @@ */ class Mage_Sales_Model_Api2_Order_Comment_Rest_Admin_V1 extends Mage_Sales_Model_Api2_Order_Comment_Rest { + /** + * Add comment to order + * + * @param array $data + * @return string + */ + protected function _create(array $data) + { + $orderId = $this->getRequest()->getParam(self::PARAM_ORDER_ID); + $order = $this->_loadOrderById($orderId); + + $status = $data['status'] ?? false; + $comment = $data['comment'] ?? ''; + $visibleOnFront = $data['is_visible_on_front'] ?? 0; + $notifyCustomer = array_key_exists('is_customer_notified', $data) ? $data['is_customer_notified'] : false; + + $historyItem = $order->addStatusHistoryComment($comment, $status); + $historyItem->setIsCustomerNotified($notifyCustomer) + ->setIsVisibleOnFront((int) $visibleOnFront) + ->save(); + + try { + if ($notifyCustomer && $comment) { + $oldStore = Mage::getDesign()->getStore(); + $oldArea = Mage::getDesign()->getArea(); + Mage::getDesign()->setStore($order->getStoreId()); + Mage::getDesign()->setArea('frontend'); + } + + $order->save(); + $order->sendOrderUpdateEmail((bool) $notifyCustomer, $comment); + + if ($notifyCustomer && $comment) { + Mage::getDesign()->setStore($oldStore); + Mage::getDesign()->setArea($oldArea); + } + } catch (Mage_Core_Exception $e) { + $this->_critical($e->getMessage(), self::RESOURCE_INTERNAL_ERROR); + } catch (Throwable $t) { + Mage::logException($t); + $this->_critical($t->getMessage(), self::RESOURCE_UNKNOWN_ERROR); + } + + return $this->_getLocation($historyItem); + } + + /** + * Retrieve order comment by id + * + * @return array + */ + protected function _retrieve() + { + $comment = Mage::getModel('sales/order_status_history')->load( + $this->getRequest()->getParam(self::PARAM_COMMENT_ID) + ); + + if (!$comment->getId()) { + $this->_critical(self::RESOURCE_NOT_FOUND); + } + + return $comment->getData(); + } } diff --git a/app/code/core/Mage/Sales/etc/api2.xml b/app/code/core/Mage/Sales/etc/api2.xml index 260d826c761..a068bd807cc 100644 --- a/app/code/core/Mage/Sales/etc/api2.xml +++ b/app/code/core/Mage/Sales/etc/api2.xml @@ -303,6 +303,7 @@ Order Comments + 1 1 @@ -323,6 +324,10 @@ + + /orders/comments/:comment_id + entity + /orders/:id/comments collection From 57cbe082be5e08cb73d0e61813b903d3311a841e Mon Sep 17 00:00:00 2001 From: Sven Reichel Date: Sat, 19 Nov 2022 22:17:19 +0100 Subject: [PATCH 2/3] CS Fixes --- .../Mage/Sales/Model/Api2/Order/Comment/Rest/Admin/V1.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/core/Mage/Sales/Model/Api2/Order/Comment/Rest/Admin/V1.php b/app/code/core/Mage/Sales/Model/Api2/Order/Comment/Rest/Admin/V1.php index 9d05bf6cce6..2ff355a0bec 100644 --- a/app/code/core/Mage/Sales/Model/Api2/Order/Comment/Rest/Admin/V1.php +++ b/app/code/core/Mage/Sales/Model/Api2/Order/Comment/Rest/Admin/V1.php @@ -29,7 +29,7 @@ class Mage_Sales_Model_Api2_Order_Comment_Rest_Admin_V1 extends Mage_Sales_Model { /** * Add comment to order - * + * * @param array $data * @return string */ @@ -75,7 +75,7 @@ protected function _create(array $data) /** * Retrieve order comment by id - * + * * @return array */ protected function _retrieve() From c75e414c2b1c6746ba663e614fba4ec240841fbc Mon Sep 17 00:00:00 2001 From: Sven Reichel Date: Sat, 19 Nov 2022 22:45:18 +0100 Subject: [PATCH 3/3] Updated phpstan-baseline.neon --- .github/phpstan-baseline.neon | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/phpstan-baseline.neon b/.github/phpstan-baseline.neon index a09c2cbf013..450d48aaa4f 100644 --- a/.github/phpstan-baseline.neon +++ b/.github/phpstan-baseline.neon @@ -5050,6 +5050,16 @@ parameters: count: 1 path: ../app/code/core/Mage/Sales/Block/Recurring/Profiles.php + - + message: "#^Variable \\$oldArea might not be defined\\.$#" + count: 1 + path: ../app/code/core/Mage/Sales/Model/Api2/Order/Comment/Rest/Admin/V1.php + + - + message: "#^Variable \\$oldStore might not be defined\\.$#" + count: 1 + path: ../app/code/core/Mage/Sales/Model/Api2/Order/Comment/Rest/Admin/V1.php + - message: "#^Call to an undefined method Varien_Simplexml_Element\\:\\:getClassName\\(\\)\\.$#" count: 1