15
15
use Magento \Sales \Setup \SalesSetupFactory ;
16
16
use Magento \Framework \Setup \Patch \DataPatchInterface ;
17
17
use Magento \Framework \Setup \Patch \PatchVersionInterface ;
18
+ use Magento \Framework \Setup \ModuleDataSetupInterface ;
18
19
19
20
class FillQuoteAddressIdInSalesOrderAddress implements DataPatchInterface, PatchVersionInterface
20
21
{
21
22
/**
22
- * @var \Magento\Framework\Setup\ ModuleDataSetupInterface
23
+ * @var ModuleDataSetupInterface
23
24
*/
24
25
private $ moduleDataSetup ;
25
26
@@ -55,10 +56,10 @@ class FillQuoteAddressIdInSalesOrderAddress implements DataPatchInterface, Patch
55
56
56
57
/**
57
58
* PatchInitial constructor.
58
- * @param \Magento\Framework\Setup\ ModuleDataSetupInterface $moduleDataSetup
59
+ * @param ModuleDataSetupInterface $moduleDataSetup
59
60
*/
60
61
public function __construct (
61
- \ Magento \ Framework \ Setup \ ModuleDataSetupInterface $ moduleDataSetup ,
62
+ ModuleDataSetupInterface $ moduleDataSetup ,
62
63
SalesSetupFactory $ salesSetupFactory ,
63
64
State $ state ,
64
65
Config $ eavConfig ,
@@ -82,39 +83,41 @@ public function apply()
82
83
{
83
84
$ this ->state ->emulateAreaCode (
84
85
\Magento \Backend \App \Area \FrontNameResolver::AREA_CODE ,
85
- [$ this , 'fillQuoteAddressIdInSalesOrderAddress ' ]
86
+ [$ this , 'fillQuoteAddressIdInSalesOrderAddress ' ],
87
+ [$ this ->moduleDataSetup ]
86
88
);
87
89
$ this ->eavConfig ->clear ();
88
90
}
89
91
90
92
/**
91
93
* Fill quote_address_id in table sales_order_address if it is empty.
94
+ *
95
+ * @param ModuleDataSetupInterface $setup
92
96
*/
93
- public function fillQuoteAddressIdInSalesOrderAddress ()
97
+ public function fillQuoteAddressIdInSalesOrderAddress (ModuleDataSetupInterface $ setup )
94
98
{
95
- $ addressCollection = $ this ->addressCollectionFactory ->create ();
96
- $ addressCollection ->addFieldToFilter ('quote_address_id ' , ['null ' => true ]);
97
-
98
- /** @var \Magento\Sales\Model\Order\Address $orderAddress */
99
- foreach ($ addressCollection as $ orderAddress ) {
100
- $ orderId = $ orderAddress ->getParentId ();
101
- $ addressType = $ orderAddress ->getAddressType ();
102
-
103
- /** @var \Magento\Sales\Model\Order $order */
104
- $ order = $ this ->orderFactory ->create ()->load ($ orderId );
105
- $ quoteId = $ order ->getQuoteId ();
106
- $ quote = $ this ->quoteFactory ->create ()->load ($ quoteId );
107
-
108
- if ($ addressType == \Magento \Sales \Model \Order \Address::TYPE_SHIPPING ) {
109
- $ quoteAddressId = $ quote ->getShippingAddress ()->getId ();
110
- $ orderAddress ->setData ('quote_address_id ' , $ quoteAddressId );
111
- } elseif ($ addressType == \Magento \Sales \Model \Order \Address::TYPE_BILLING ) {
112
- $ quoteAddressId = $ quote ->getBillingAddress ()->getId ();
113
- $ orderAddress ->setData ('quote_address_id ' , $ quoteAddressId );
114
- }
115
-
116
- $ orderAddress ->save ();
117
- }
99
+ $ addressTable = $ setup ->getTable ('sales_order_address ' );
100
+ $ updateOrderAddress = $ setup ->getConnection ()
101
+ ->select ()
102
+ ->joinInner (
103
+ ['sales_order ' => $ setup ->getTable ('sales_order ' )],
104
+ $ addressTable . '.parent_id = sales_order.entity_id ' ,
105
+ ['quote_address_id ' => 'quote_address.address_id ' ]
106
+ )
107
+ ->joinInner (
108
+ ['quote_address ' => $ setup ->getTable ('quote_address ' )],
109
+ 'sales_order.quote_id = quote_address.quote_id
110
+ AND ' . $ addressTable . '.address_type = quote_address.address_type ' ,
111
+ []
112
+ )
113
+ ->where (
114
+ $ addressTable . '.quote_address_id IS NULL '
115
+ );
116
+ $ updateOrderAddress = $ setup ->getConnection ()->updateFromSelect (
117
+ $ updateOrderAddress ,
118
+ $ addressTable
119
+ );
120
+ $ setup ->getConnection ()->query ($ updateOrderAddress );
118
121
}
119
122
120
123
/**
0 commit comments