From e5c397966a2269c266c3625dc59fa4cda3508007 Mon Sep 17 00:00:00 2001 From: createit-ru Date: Mon, 29 Sep 2025 22:21:55 +0300 Subject: [PATCH 1/2] Expanded msCartHandler responses with information about changes in product quantity (add "changes") --- .../handlers/mscarthandler.class.php | 54 +++++++++++++------ 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/core/components/minishop2/handlers/mscarthandler.class.php b/core/components/minishop2/handlers/mscarthandler.class.php index 5818600b5..8685fb2a6 100644 --- a/core/components/minishop2/handlers/mscarthandler.class.php +++ b/core/components/minishop2/handlers/mscarthandler.class.php @@ -56,7 +56,7 @@ public function __construct(miniShop2 $ms2, array $config = []) * * @return bool */ - public function initialize($ctx = 'web') + public function initialize($ctx = 'web') : bool { $ms2_cart_context = (bool)$this->modx->getOption('ms2_cart_context', null, '0', true); if ($ms2_cart_context) { @@ -164,12 +164,15 @@ public function add($id, $count = 1, $options = []) return $this->error($response['message']); } + $changes = $this->getChanges('add', $count, 0); + return $this->success( 'ms2_cart_add_success', $this->status([ 'key' => $key, 'cart' => $this->cart, - 'row' => $this->cart[$key] + 'row' => $this->cart[$key], + 'changes' => $changes ]), ['count' => $count] ); @@ -186,6 +189,8 @@ public function remove($key) return $this->error('ms2_cart_remove_error'); } + $product = $this->cart[$key]; + $response = $this->ms2->invokeEvent('msOnBeforeRemoveFromCart', ['key' => $key, 'cart' => $this]); if (!$response['success']) { return $this->error($response['message']); @@ -198,11 +203,14 @@ public function remove($key) return $this->error($response['message']); } + $changes = $this->getChanges('remove',0, $product['count']); + return $this->success( 'ms2_cart_remove_success', $this->status([ 'cart' => $this->cart, - 'row' => $row + 'row' => $row, + 'changes' => $changes ]) ); } @@ -215,9 +223,8 @@ public function remove($key) */ public function change($key, $count) { - $status = []; if (!array_key_exists($key, $this->cart)) { - return $this->error('ms2_cart_change_error', $this->status($status)); + return $this->error('ms2_cart_change_error', $this->status()); } if ($count <= 0) { @@ -237,6 +244,9 @@ public function change($key, $count) } $count = $response['data']['count']; + + $changes = $this->getChanges('change', $count, $this->cart[$key]['count']); + $this->cart = $this->storageHandler->change($key, $count); $response = $this->ms2->invokeEvent( 'msOnChangeInCart', @@ -245,10 +255,14 @@ public function change($key, $count) if (!$response['success']) { return $this->error($response['message']); } - $status['key'] = $key; - $status['cost'] = $count * $this->cart[$key]['price']; - $status['cart'] = $this->cart; - $status['row'] = $this->cart[$key]; + + $status = [ + 'key' => $key, + 'cost' => $count * $this->cart[$key]['price'], + 'cart' => $this->cart, + 'row' => $this->cart[$key], + 'changes' => $changes + ]; return $this->success( 'ms2_cart_change_success', @@ -282,7 +296,7 @@ public function clean() * * @return array */ - public function status($data = []) + public function status($data = []): array { $status = [ 'total_count' => 0, @@ -316,7 +330,7 @@ public function status($data = []) /** * @return array */ - public function get() + public function get() : array { $cart = []; foreach ($this->cart as $key => $item) { @@ -331,7 +345,7 @@ public function get() /** * @param array $cart */ - public function set($cart = []) + public function set($cart = []) : void { $this->cart = $this->storageHandler->set($cart); } @@ -362,7 +376,7 @@ protected function storageInit() * * @return array|string */ - public function error($message = '', $data = [], $placeholders = []) + public function error(string $message = '', array $data = [], array $placeholders = []) { return $this->ms2->error($message, $data, $placeholders); } @@ -376,7 +390,7 @@ public function error($message = '', $data = [], $placeholders = []) * * @return array|string */ - public function success($message = '', $data = [], $placeholders = []) + public function success(string $message = '', array $data = [], array $placeholders = []) { return $this->ms2->success($message, $data, $placeholders); } @@ -389,7 +403,7 @@ public function success($message = '', $data = [], $placeholders = []) * @return string * */ - protected function getProductKey(array $product, array $options = []) + protected function getProductKey(array $product, array $options = []): string { $key_fields = explode(',', $this->config['cart_product_key_fields']); $product['options'] = $options; @@ -407,4 +421,14 @@ protected function getProductKey(array $product, array $options = []) return 'ms' . md5($key); } + + protected function getChanges(string $method, int $count, int $oldCount): array + { + return [ + 'method' => $method, + 'count' => $count, + 'old_count' => $oldCount, + 'delta' => $count - $oldCount + ]; + } } From f14cf1c5b1b04b12e1507b760c60c0313e5773d1 Mon Sep 17 00:00:00 2001 From: createit-ru Date: Mon, 13 Oct 2025 14:37:59 +0300 Subject: [PATCH 2/2] Add strict typing --- .../handlers/interfaces/msCartInterface.php | 14 +++++++------- .../minishop2/handlers/mscarthandler.class.php | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/core/components/minishop2/handlers/interfaces/msCartInterface.php b/core/components/minishop2/handlers/interfaces/msCartInterface.php index 1867744df..240ac2d68 100644 --- a/core/components/minishop2/handlers/interfaces/msCartInterface.php +++ b/core/components/minishop2/handlers/interfaces/msCartInterface.php @@ -11,7 +11,7 @@ interface msCartInterface * * @return boolean */ - public function initialize($ctx = 'web'); + public function initialize(string $ctx = 'web'): bool; /** * Adds product to cart @@ -22,7 +22,7 @@ public function initialize($ctx = 'web'); * * @return array|string $response */ - public function add($id, $count = 1, $options = []); + public function add(int $id, int $count = 1, array $options = []); /** * Removes product from cart @@ -31,7 +31,7 @@ public function add($id, $count = 1, $options = []); * * @return array|string $response */ - public function remove($key); + public function remove(string $key); /** * Changes products count in cart @@ -41,7 +41,7 @@ public function remove($key); * * @return array|string $response */ - public function change($key, $count); + public function change(string $key, int $count); /** * Cleans the cart @@ -57,14 +57,14 @@ public function clean(); * * @return array $status */ - public function status($data = []); + public function status(array $data = []): array; /** * Returns the cart items * * @return array $cart */ - public function get(); + public function get(): array; /** * Set all the cart items by one array @@ -73,5 +73,5 @@ public function get(); * * @return void */ - public function set($cart = []); + public function set(array $cart = []): void; } diff --git a/core/components/minishop2/handlers/mscarthandler.class.php b/core/components/minishop2/handlers/mscarthandler.class.php index 8685fb2a6..23ed22eae 100644 --- a/core/components/minishop2/handlers/mscarthandler.class.php +++ b/core/components/minishop2/handlers/mscarthandler.class.php @@ -56,7 +56,7 @@ public function __construct(miniShop2 $ms2, array $config = []) * * @return bool */ - public function initialize($ctx = 'web') : bool + public function initialize(string $ctx = 'web') : bool { $ms2_cart_context = (bool)$this->modx->getOption('ms2_cart_context', null, '0', true); if ($ms2_cart_context) { @@ -74,7 +74,7 @@ public function initialize($ctx = 'web') : bool * * @return array|string */ - public function add($id, $count = 1, $options = []) + public function add(int $id, int $count = 1, array $options = []) { if (empty($id) || !is_numeric($id)) { return $this->error('ms2_cart_add_err_id'); @@ -183,7 +183,7 @@ public function add($id, $count = 1, $options = []) * * @return array|string */ - public function remove($key) + public function remove(string $key) { if (!array_key_exists($key, $this->cart)) { return $this->error('ms2_cart_remove_error'); @@ -221,7 +221,7 @@ public function remove($key) * * @return array|string */ - public function change($key, $count) + public function change(string $key, int $count) { if (!array_key_exists($key, $this->cart)) { return $this->error('ms2_cart_change_error', $this->status()); @@ -296,7 +296,7 @@ public function clean() * * @return array */ - public function status($data = []): array + public function status(array $data = []): array { $status = [ 'total_count' => 0, @@ -345,7 +345,7 @@ public function get() : array /** * @param array $cart */ - public function set($cart = []) : void + public function set(array $cart = []) : void { $this->cart = $this->storageHandler->set($cart); } @@ -353,7 +353,7 @@ public function set($cart = []) : void /** * Set controller for Cart */ - protected function storageInit() + protected function storageInit(): void { switch ($this->storage) { case 'session':