Skip to content

Commit 3846ddd

Browse files
author
Vaha
committed
magento/magento2# improved getCustomerName method, added prefix, middlename, suffix if configure
1 parent ea71192 commit 3846ddd

File tree

1 file changed

+75
-9
lines changed

1 file changed

+75
-9
lines changed

app/code/Magento/Sales/Model/Order.php

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@
55
*/
66
namespace Magento\Sales\Model;
77

8+
use Magento\Config\Model\Config\Source\Nooptreq;
89
use Magento\Directory\Model\Currency;
910
use Magento\Framework\Api\AttributeValueFactory;
11+
use Magento\Framework\Api\SearchCriteriaBuilder;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
1013
use Magento\Framework\App\ObjectManager;
1114
use Magento\Framework\Exception\LocalizedException;
1215
use Magento\Framework\Locale\ResolverInterface;
1316
use Magento\Framework\Pricing\PriceCurrencyInterface;
1417
use Magento\Sales\Api\Data\OrderInterface;
1518
use Magento\Sales\Api\Data\OrderItemInterface;
1619
use Magento\Sales\Api\Data\OrderStatusHistoryInterface;
20+
use Magento\Sales\Api\OrderItemRepositoryInterface;
1721
use Magento\Sales\Model\Order\Payment;
1822
use Magento\Sales\Model\Order\ProductOption;
1923
use Magento\Sales\Model\ResourceModel\Order\Address\Collection;
@@ -24,8 +28,7 @@
2428
use Magento\Sales\Model\ResourceModel\Order\Shipment\Collection as ShipmentCollection;
2529
use Magento\Sales\Model\ResourceModel\Order\Shipment\Track\Collection as TrackCollection;
2630
use Magento\Sales\Model\ResourceModel\Order\Status\History\Collection as HistoryCollection;
27-
use Magento\Sales\Api\OrderItemRepositoryInterface;
28-
use Magento\Framework\Api\SearchCriteriaBuilder;
31+
use Magento\Store\Model\ScopeInterface;
2932

3033
/**
3134
* Order model
@@ -299,6 +302,11 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
299302
*/
300303
private $searchCriteriaBuilder;
301304

305+
/**
306+
* @var ScopeConfigInterface;
307+
*/
308+
private $scopeConfig;
309+
302310
/**
303311
* @param \Magento\Framework\Model\Context $context
304312
* @param \Magento\Framework\Registry $registry
@@ -331,6 +339,7 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
331339
* @param ProductOption|null $productOption
332340
* @param OrderItemRepositoryInterface $itemRepository
333341
* @param SearchCriteriaBuilder $searchCriteriaBuilder
342+
* @param ScopeConfigInterface $scopeConfig
334343
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
335344
*/
336345
public function __construct(
@@ -364,7 +373,8 @@ public function __construct(
364373
ResolverInterface $localeResolver = null,
365374
ProductOption $productOption = null,
366375
OrderItemRepositoryInterface $itemRepository = null,
367-
SearchCriteriaBuilder $searchCriteriaBuilder = null
376+
SearchCriteriaBuilder $searchCriteriaBuilder = null,
377+
ScopeConfigInterface $scopeConfig = null
368378
) {
369379
$this->_storeManager = $storeManager;
370380
$this->_orderConfig = $orderConfig;
@@ -392,6 +402,7 @@ public function __construct(
392402
->get(OrderItemRepositoryInterface::class);
393403
$this->searchCriteriaBuilder = $searchCriteriaBuilder ?: ObjectManager::getInstance()
394404
->get(SearchCriteriaBuilder::class);
405+
$this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()->get(ScopeConfigInterface::class);
395406

396407
parent::__construct(
397408
$context,
@@ -1111,7 +1122,7 @@ public function addStatusHistoryComment($comment, $status = false)
11111122
{
11121123
return $this->addCommentToStatusHistory($comment, $status, false);
11131124
}
1114-
1125+
11151126
/**
11161127
* Add a comment to order status history.
11171128
*
@@ -1965,16 +1976,71 @@ public function getRelatedObjects()
19651976
*
19661977
* @return string
19671978
*/
1968-
public function getCustomerName()
1979+
public function getCustomerName(): string
19691980
{
1970-
if ($this->getCustomerFirstname()) {
1971-
$customerName = $this->getCustomerFirstname() . ' ' . $this->getCustomerLastname();
1972-
} else {
1973-
$customerName = (string)__('Guest');
1981+
if (null === $this->getCustomerFirstname()) {
1982+
return (string)__('Guest');
1983+
}
1984+
1985+
$customerName = '';
1986+
if ($this->isVisibleCustomerPrefix() && strlen($this->getCustomerPrefix())) {
1987+
$customerName .= $this->getCustomerPrefix() . ' ';
1988+
}
1989+
$customerName .= $this->getCustomerFirstname();
1990+
if ($this->isVisibleCustomerMiddlename() && strlen($this->getCustomerMiddlename())) {
1991+
$customerName .= ' ' . $this->getCustomerMiddlename();
1992+
}
1993+
$customerName .= ' ' . $this->getCustomerLastname();
1994+
if ($this->isVisibleCustomerSuffix() && strlen($this->getCustomerSuffix())) {
1995+
$customerName .= ' ' . $this->getCustomerSuffix();
19741996
}
1997+
19751998
return $customerName;
19761999
}
19772000

2001+
/**
2002+
* Is visible customer middlename
2003+
*
2004+
* @return bool
2005+
*/
2006+
private function isVisibleCustomerMiddlename(): bool
2007+
{
2008+
return $this->scopeConfig->isSetFlag(
2009+
'customer/address/middlename_show',
2010+
ScopeInterface::SCOPE_STORE
2011+
);
2012+
}
2013+
2014+
/**
2015+
* Is visible customer prefix
2016+
*
2017+
* @return bool
2018+
*/
2019+
private function isVisibleCustomerPrefix(): bool
2020+
{
2021+
$prefixShowValue = $this->scopeConfig->getValue(
2022+
'customer/address/prefix_show',
2023+
ScopeInterface::SCOPE_STORE
2024+
);
2025+
2026+
return $prefixShowValue !== Nooptreq::VALUE_NO;
2027+
}
2028+
2029+
/**
2030+
* Is visible customer suffix
2031+
*
2032+
* @return bool
2033+
*/
2034+
private function isVisibleCustomerSuffix(): bool
2035+
{
2036+
$prefixShowValue = $this->scopeConfig->getValue(
2037+
'customer/address/suffix_show',
2038+
ScopeInterface::SCOPE_STORE
2039+
);
2040+
2041+
return $prefixShowValue !== Nooptreq::VALUE_NO;
2042+
}
2043+
19782044
/**
19792045
* Add New object to related array
19802046
*

0 commit comments

Comments
 (0)