Skip to content

Commit

Permalink
magento/magento2# improved getCustomerName method, added prefix, midd…
Browse files Browse the repository at this point in the history
…lename, suffix if configure
  • Loading branch information
Vaha committed Sep 27, 2019
1 parent ea71192 commit 3846ddd
Showing 1 changed file with 75 additions and 9 deletions.
84 changes: 75 additions & 9 deletions app/code/Magento/Sales/Model/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
*/
namespace Magento\Sales\Model;

use Magento\Config\Model\Config\Source\Nooptreq;
use Magento\Directory\Model\Currency;
use Magento\Framework\Api\AttributeValueFactory;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderItemInterface;
use Magento\Sales\Api\Data\OrderStatusHistoryInterface;
use Magento\Sales\Api\OrderItemRepositoryInterface;
use Magento\Sales\Model\Order\Payment;
use Magento\Sales\Model\Order\ProductOption;
use Magento\Sales\Model\ResourceModel\Order\Address\Collection;
Expand All @@ -24,8 +28,7 @@
use Magento\Sales\Model\ResourceModel\Order\Shipment\Collection as ShipmentCollection;
use Magento\Sales\Model\ResourceModel\Order\Shipment\Track\Collection as TrackCollection;
use Magento\Sales\Model\ResourceModel\Order\Status\History\Collection as HistoryCollection;
use Magento\Sales\Api\OrderItemRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Store\Model\ScopeInterface;

/**
* Order model
Expand Down Expand Up @@ -299,6 +302,11 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
*/
private $searchCriteriaBuilder;

/**
* @var ScopeConfigInterface;
*/
private $scopeConfig;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
Expand Down Expand Up @@ -331,6 +339,7 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
* @param ProductOption|null $productOption
* @param OrderItemRepositoryInterface $itemRepository
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param ScopeConfigInterface $scopeConfig
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand Down Expand Up @@ -364,7 +373,8 @@ public function __construct(
ResolverInterface $localeResolver = null,
ProductOption $productOption = null,
OrderItemRepositoryInterface $itemRepository = null,
SearchCriteriaBuilder $searchCriteriaBuilder = null
SearchCriteriaBuilder $searchCriteriaBuilder = null,
ScopeConfigInterface $scopeConfig = null
) {
$this->_storeManager = $storeManager;
$this->_orderConfig = $orderConfig;
Expand Down Expand Up @@ -392,6 +402,7 @@ public function __construct(
->get(OrderItemRepositoryInterface::class);
$this->searchCriteriaBuilder = $searchCriteriaBuilder ?: ObjectManager::getInstance()
->get(SearchCriteriaBuilder::class);
$this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()->get(ScopeConfigInterface::class);

parent::__construct(
$context,
Expand Down Expand Up @@ -1111,7 +1122,7 @@ public function addStatusHistoryComment($comment, $status = false)
{
return $this->addCommentToStatusHistory($comment, $status, false);
}

/**
* Add a comment to order status history.
*
Expand Down Expand Up @@ -1965,16 +1976,71 @@ public function getRelatedObjects()
*
* @return string
*/
public function getCustomerName()
public function getCustomerName(): string
{
if ($this->getCustomerFirstname()) {
$customerName = $this->getCustomerFirstname() . ' ' . $this->getCustomerLastname();
} else {
$customerName = (string)__('Guest');
if (null === $this->getCustomerFirstname()) {
return (string)__('Guest');
}

$customerName = '';
if ($this->isVisibleCustomerPrefix() && strlen($this->getCustomerPrefix())) {
$customerName .= $this->getCustomerPrefix() . ' ';
}
$customerName .= $this->getCustomerFirstname();
if ($this->isVisibleCustomerMiddlename() && strlen($this->getCustomerMiddlename())) {
$customerName .= ' ' . $this->getCustomerMiddlename();
}
$customerName .= ' ' . $this->getCustomerLastname();
if ($this->isVisibleCustomerSuffix() && strlen($this->getCustomerSuffix())) {
$customerName .= ' ' . $this->getCustomerSuffix();
}

return $customerName;
}

/**
* Is visible customer middlename
*
* @return bool
*/
private function isVisibleCustomerMiddlename(): bool
{
return $this->scopeConfig->isSetFlag(
'customer/address/middlename_show',
ScopeInterface::SCOPE_STORE
);
}

/**
* Is visible customer prefix
*
* @return bool
*/
private function isVisibleCustomerPrefix(): bool
{
$prefixShowValue = $this->scopeConfig->getValue(
'customer/address/prefix_show',
ScopeInterface::SCOPE_STORE
);

return $prefixShowValue !== Nooptreq::VALUE_NO;
}

/**
* Is visible customer suffix
*
* @return bool
*/
private function isVisibleCustomerSuffix(): bool
{
$prefixShowValue = $this->scopeConfig->getValue(
'customer/address/suffix_show',
ScopeInterface::SCOPE_STORE
);

return $prefixShowValue !== Nooptreq::VALUE_NO;
}

/**
* Add New object to related array
*
Expand Down

0 comments on commit 3846ddd

Please sign in to comment.