From 844e8f0d64aba01c3054e87adc08515fb3f35312 Mon Sep 17 00:00:00 2001 From: Raphael Adam Date: Wed, 8 Sep 2021 14:39:54 +0200 Subject: [PATCH 1/2] load prefix from settings --- src/Services/OrderService.php | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/Services/OrderService.php b/src/Services/OrderService.php index 317804015..a44c3fec5 100644 --- a/src/Services/OrderService.php +++ b/src/Services/OrderService.php @@ -25,6 +25,7 @@ use Plenty\Modules\Order\Models\OrderReference; use Plenty\Modules\Order\Property\Contracts\OrderPropertyRepositoryContract; use Plenty\Modules\Order\Property\Models\OrderProperty; +use Plenty\Modules\Order\Settings\Contracts\OrderSettingsRepositoryContract; use Plenty\Modules\Order\Status\Contracts\OrderStatusRepositoryContract; use Plenty\Modules\Payment\Contracts\PaymentRepositoryContract; use Plenty\Modules\Payment\Method\Contracts\PaymentMethodRepositoryContract; @@ -760,6 +761,49 @@ public function complete($order) ); } + /** + * Remove the bundle prefix from the order item name. + * Default prefix is "[BUNDLE] " + * + * @param string $name + * @return string + */ + public function removeBundlePrefix(string $name): string + { + return $this->removeOrderItemPrefix($name, \Plenty\Modules\Order\Models\OrderItemType::TYPE_ITEM_BUNDLE); + } + + /** + * Remove the bundle component prefix from the order item name. + * Default prefix is "[-] " + * + * @param string $name + * @return string + */ + public function removeBundleComponentPrefix(string $name): string + { + return $this->removeOrderItemPrefix($name, \Plenty\Modules\Order\Models\OrderItemType::TYPE_BUNDLE_COMPONENT); + } + + /** + * @param string $name + * @param int $orderItemTypeId + * @return string + */ + private function removeOrderItemPrefix(string $name, int $orderItemTypeId): string + { + /** @var OrderSettingsRepositoryContract $settingsRepository */ + $settingsRepository = pluginApp(OrderSettingsRepositoryContract::class); + $settings = $settingsRepository->get(); + $prefix = $settings->orderItemPrefixes[$orderItemTypeId] ?? null; + + if ($prefix !== null && (substr($name, 0, strlen($prefix)) == $prefix)) { + $name = substr($name, strlen($prefix)); + } + + return $name; + } + /** * @param \Throwable $throwable * @param null $message From 66285c985005e72e3c94149ad57d09abc044d8fe Mon Sep 17 00:00:00 2001 From: Steve Tentrop Date: Tue, 21 Sep 2021 13:42:20 +0200 Subject: [PATCH 2/2] add new function to get prefixes --- src/Services/OrderService.php | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/Services/OrderService.php b/src/Services/OrderService.php index a44c3fec5..4f10f660f 100644 --- a/src/Services/OrderService.php +++ b/src/Services/OrderService.php @@ -792,11 +792,7 @@ public function removeBundleComponentPrefix(string $name): string */ private function removeOrderItemPrefix(string $name, int $orderItemTypeId): string { - /** @var OrderSettingsRepositoryContract $settingsRepository */ - $settingsRepository = pluginApp(OrderSettingsRepositoryContract::class); - $settings = $settingsRepository->get(); - $prefix = $settings->orderItemPrefixes[$orderItemTypeId] ?? null; - + $prefix = $this->getOrderItemPrefix($orderItemTypeId); if ($prefix !== null && (substr($name, 0, strlen($prefix)) == $prefix)) { $name = substr($name, strlen($prefix)); } @@ -804,6 +800,35 @@ private function removeOrderItemPrefix(string $name, int $orderItemTypeId): stri return $name; } + /** + * Check if the prefix for components is included in the name + * + * @param string $name + * @return bool + */ + public function containsComponentPrefix(string $name): bool + { + $prefix = $this->getOrderItemPrefix(\Plenty\Modules\Order\Models\OrderItemType::TYPE_BUNDLE_COMPONENT); + if ($prefix !== null && (substr($name, 0, strlen($prefix)) == $prefix)) { + return true; + } + return false; + } + + /** + * Get prefix for item type id + * + * @param int $orderItemTypeId + * @return int|null + */ + public function getOrderItemPrefix(int $orderItemTypeId) + { + /** @var OrderSettingsRepositoryContract $settingsRepository */ + $settingsRepository = pluginApp(OrderSettingsRepositoryContract::class); + $settings = $settingsRepository->get(); + return $settings->orderItemPrefixes[$orderItemTypeId] ?? null; + } + /** * @param \Throwable $throwable * @param null $message