1515use Magento \Sales \Setup \SalesSetupFactory ;
1616use Magento \Framework \Setup \Patch \DataPatchInterface ;
1717use Magento \Framework \Setup \Patch \PatchVersionInterface ;
18+ use Magento \Framework \Setup \ModuleDataSetupInterface ;
1819
1920class FillQuoteAddressIdInSalesOrderAddress implements DataPatchInterface, PatchVersionInterface
2021{
2122 /**
22- * @var \Magento\Framework\Setup\ ModuleDataSetupInterface
23+ * @var ModuleDataSetupInterface
2324 */
2425 private $ moduleDataSetup ;
2526
@@ -55,10 +56,10 @@ class FillQuoteAddressIdInSalesOrderAddress implements DataPatchInterface, Patch
5556
5657 /**
5758 * PatchInitial constructor.
58- * @param \Magento\Framework\Setup\ ModuleDataSetupInterface $moduleDataSetup
59+ * @param ModuleDataSetupInterface $moduleDataSetup
5960 */
6061 public function __construct (
61- \ Magento \ Framework \ Setup \ ModuleDataSetupInterface $ moduleDataSetup ,
62+ ModuleDataSetupInterface $ moduleDataSetup ,
6263 SalesSetupFactory $ salesSetupFactory ,
6364 State $ state ,
6465 Config $ eavConfig ,
@@ -82,39 +83,41 @@ public function apply()
8283 {
8384 $ this ->state ->emulateAreaCode (
8485 \Magento \Backend \App \Area \FrontNameResolver::AREA_CODE ,
85- [$ this , 'fillQuoteAddressIdInSalesOrderAddress ' ]
86+ [$ this , 'fillQuoteAddressIdInSalesOrderAddress ' ],
87+ [$ this ->moduleDataSetup ]
8688 );
8789 $ this ->eavConfig ->clear ();
8890 }
8991
9092 /**
9193 * Fill quote_address_id in table sales_order_address if it is empty.
94+ *
95+ * @param ModuleDataSetupInterface $setup
9296 */
93- public function fillQuoteAddressIdInSalesOrderAddress ()
97+ public function fillQuoteAddressIdInSalesOrderAddress (ModuleDataSetupInterface $ setup )
9498 {
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 );
118121 }
119122
120123 /**
0 commit comments