Skip to content

Commit

Permalink
Merge pull request #1023 from plentymarkets/order/feature/order_item_…
Browse files Browse the repository at this point in the history
…prefixes

Configurable bundle prefixes: use setting
  • Loading branch information
stentrop authored Oct 4, 2021
2 parents 88d5122 + a373bb7 commit 48afb43
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/Services/OrderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -760,6 +761,74 @@ 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
{
$prefix = $this->getOrderItemPrefix($orderItemTypeId);
if ($prefix !== null && (substr($name, 0, strlen($prefix)) == $prefix)) {
$name = substr($name, strlen($prefix));
}

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
Expand Down

0 comments on commit 48afb43

Please sign in to comment.