66namespace Magento \Sales \Model \ResourceModel \Order \Address ;
77
88use Magento \Sales \Api \Data \OrderAddressSearchResultInterface ;
9- use \Magento \Sales \Model \ResourceModel \Order \Collection \AbstractCollection ;
9+ use Magento \Sales \Model \ResourceModel \Order \Collection \AbstractCollection ;
10+ use Magento \Framework \Locale \ResolverInterface ;
11+ use Magento \Framework \Data \Collection \EntityFactoryInterface ;
12+ use Magento \Framework \Data \Collection \Db \FetchStrategyInterface ;
13+ use Magento \Framework \Event \ManagerInterface ;
14+ use Magento \Framework \Model \ResourceModel \Db \VersionControl \Snapshot ;
15+ use Magento \Framework \DB \Adapter \AdapterInterface ;
16+ use Magento \Framework \Model \ResourceModel \Db \AbstractDb ;
17+ use Magento \Framework \App \ObjectManager ;
18+ use Psr \Log \LoggerInterface ;
1019
1120/**
12- * Flat sales order payment collection
13- *
14- * @author Magento Core Team <core@magentocommerce.com>
21+ * Order addresses collection
1522 */
1623class Collection extends AbstractCollection implements OrderAddressSearchResultInterface
1724{
@@ -29,6 +36,44 @@ class Collection extends AbstractCollection implements OrderAddressSearchResultI
2936 */
3037 protected $ _eventObject = 'order_address_collection ' ;
3138
39+ /**
40+ * @var ResolverInterface
41+ */
42+ private $ localeResolver ;
43+
44+ /**
45+ * @param EntityFactoryInterface $entityFactory
46+ * @param LoggerInterface $logger
47+ * @param FetchStrategyInterface $fetchStrategy
48+ * @param ManagerInterface $eventManager
49+ * @param Snapshot $entitySnapshot
50+ * @param AdapterInterface|null $connection
51+ * @param AbstractDb|null $resource
52+ * @param ResolverInterface|null $localeResolver
53+ */
54+ public function __construct (
55+ EntityFactoryInterface $ entityFactory ,
56+ LoggerInterface $ logger ,
57+ FetchStrategyInterface $ fetchStrategy ,
58+ ManagerInterface $ eventManager ,
59+ Snapshot $ entitySnapshot ,
60+ AdapterInterface $ connection = null ,
61+ AbstractDb $ resource = null ,
62+ ResolverInterface $ localeResolver = null
63+ ) {
64+ $ this ->localeResolver = $ localeResolver ?: ObjectManager::getInstance ()
65+ ->get (ResolverInterface::class);
66+ parent ::__construct (
67+ $ entityFactory ,
68+ $ logger ,
69+ $ fetchStrategy ,
70+ $ eventManager ,
71+ $ entitySnapshot ,
72+ $ connection ,
73+ $ resource
74+ );
75+ }
76+
3277 /**
3378 * Model initialization
3479 *
@@ -42,6 +87,16 @@ protected function _construct()
4287 );
4388 }
4489
90+ /**
91+ * @inheritdoc
92+ */
93+ protected function _initSelect ()
94+ {
95+ parent ::_initSelect ();
96+ $ this ->joinRegions ();
97+ return $ this ;
98+ }
99+
45100 /**
46101 * Redeclare after load method for dispatch event
47102 *
@@ -55,4 +110,31 @@ protected function _afterLoad()
55110
56111 return $ this ;
57112 }
113+
114+ /**
115+ * Join region name table with current locale
116+ *
117+ * @return $this
118+ */
119+ private function joinRegions ()
120+ {
121+ $ locale = $ this ->localeResolver ->getLocale ();
122+ $ connection = $ this ->getConnection ();
123+
124+ $ defaultNameExpr = $ connection ->getIfNullSql (
125+ $ connection ->quoteIdentifier ('rct.default_name ' ),
126+ $ connection ->quoteIdentifier ('main_table.region ' )
127+ );
128+ $ expression = $ connection ->getIfNullSql ($ connection ->quoteIdentifier ('rnt.name ' ), $ defaultNameExpr );
129+
130+ $ regionId = $ connection ->quoteIdentifier ('main_table.region_id ' );
131+ $ condition = $ connection ->quoteInto ("rnt.locale=? " , $ locale );
132+ $ rctTable = $ this ->getTable ('directory_country_region ' );
133+ $ rntTable = $ this ->getTable ('directory_country_region_name ' );
134+
135+ $ this ->getSelect ()
136+ ->joinLeft (['rct ' => $ rctTable ], "rct.region_id= {$ regionId }" , [])
137+ ->joinLeft (['rnt ' => $ rntTable ], "rnt.region_id= {$ regionId } AND {$ condition }" , ['region ' => $ expression ]);
138+ return $ this ;
139+ }
58140}
0 commit comments