@@ -674,6 +674,49 @@ private function calculateFixedBundleSelectionPrice()
674674 private function calculateDynamicBundleSelectionPrice (array $ dimensions ): void
675675 {
676676 $ connection = $ this ->getConnection ();
677+
678+ $ price = 'idx.min_price * bs.selection_qty ' ;
679+ $ specialExpr = $ connection ->getCheckSql (
680+ 'i.special_price > 0 AND i.special_price < 100 ' ,
681+ 'ROUND( ' . $ price . ' * (i.special_price / 100), 4) ' ,
682+ $ price
683+ );
684+ $ tierExpr = $ connection ->getCheckSql (
685+ 'i.tier_percent IS NOT NULL ' ,
686+ 'ROUND((1 - i.tier_percent / 100) * ' . $ price . ', 4) ' ,
687+ 'NULL '
688+ );
689+ $ priceExpr = $ connection ->getLeastSql (
690+ [
691+ $ specialExpr ,
692+ $ connection ->getIfNullSql ($ tierExpr , $ price ),
693+ ]
694+ );
695+
696+ $ select = $ this ->getBaseBundleSelectionPriceSelect ();
697+ $ select ->join (
698+ ['idx ' => $ this ->getMainTable ($ dimensions )],
699+ 'bs.product_id = idx.entity_id AND i.customer_group_id = idx.customer_group_id ' .
700+ ' AND i.website_id = idx.website_id ' ,
701+ []
702+ )->where (
703+ 'i.price_type=? ' ,
704+ \Magento \Bundle \Model \Product \Price::PRICE_TYPE_DYNAMIC
705+ )->columns (
706+ [
707+ 'group_type ' => $ connection ->getCheckSql ("bo.type = 'select' OR bo.type = 'radio' " , '0 ' , '1 ' ),
708+ 'is_required ' => 'bo.required ' ,
709+ 'price ' => $ priceExpr ,
710+ 'tier_price ' => $ tierExpr ,
711+ ]
712+ );
713+ $ select ->join (
714+ ['si ' => $ this ->getTable ('cataloginventory_stock_status ' )],
715+ 'si.product_id = bs.product_id ' ,
716+ []
717+ );
718+ $ select ->where ('si.stock_status = ? ' , Stock::STOCK_IN_STOCK );
719+ $ select = str_replace ('AS `idx` ' , 'AS `idx` USE INDEX (PRIMARY) ' , $ select ->__toString ());
677720 $ insertColumns = [
678721 'entity_id ' ,
679722 'customer_group_id ' ,
@@ -692,53 +735,7 @@ private function calculateDynamicBundleSelectionPrice(array $dimensions): void
692735 foreach ($ insertColumns as $ column ) {
693736 $ updateValues [] = sprintf ("%s = VALUES(%s) " , $ column , $ column );
694737 }
695- $ selectColumns = [
696- '`i`.`entity_id` ' ,
697- '`i`.`customer_group_id` ' ,
698- '`i`.`website_id` ' ,
699- '`bo`.`option_id` ' ,
700- '`bs`.`selection_id` ' ,
701- "IF(bo.type = 'select' OR bo.type = 'radio', 0, 1) AS `group_type` " ,
702- "`bo`.`required` AS `is_required` " ,
703- 'LEAST(IF(i.special_price > 0 AND i.special_price < 100,
704- ROUND(idx.min_price * bs.selection_qty * (i.special_price / 100), 4), idx.min_price * bs.selection_qty),
705- IFNULL((IF(i.tier_percent IS NOT NULL,
706- ROUND((1 - i.tier_percent / 100) * idx.min_price * bs.selection_qty, 4), NULL)),
707- idx.min_price * bs.selection_qty)) AS `price` ' ,
708- 'IF(i.tier_percent IS NOT NULL, ROUND((1 - i.tier_percent / 100) * idx.min_price * bs.selection_qty, 4),
709- NULL) AS `tier_price` '
710- ];
711- $ metadata = $ this ->metadataPool ->getMetadata (ProductInterface::class);
712- $ linkField = $ metadata ->getLinkField ();
713- $ selectFrom = [
714- '` ' . $ this ->getBundlePriceTable () . '` AS `i` ' ,
715- 'INNER JOIN ` ' . $ this ->getTable ('catalog_product_entity ' ) .
716- '` AS `parent_product` ON parent_product.entity_id = i.entity_id AND
717- (parent_product.created_in <= 1 AND parent_product.updated_in > 1) ' ,
718- 'INNER JOIN ` ' . $ this ->getTable ('catalog_product_bundle_option ' ) . '` AS `bo`
719- ON bo.parent_id = parent_product. ' . $ linkField ,
720- 'INNER JOIN ` ' . $ this ->getTable ('catalog_product_bundle_selection ' ) . '` AS `bs`
721- ON bs.option_id = bo.option_id ' ,
722- 'INNER JOIN ` ' . $ this ->getMainTable ($ dimensions ) . '` AS `idx` USE INDEX (PRIMARY)
723- ON bs.product_id = idx.entity_id AND i.customer_group_id = idx.customer_group_id AND
724- i.website_id = idx.website_id ' ,
725- 'INNER JOIN ` ' . $ this ->getTable ('cataloginventory_stock_status ' ) . '` AS `si`
726- ON si.product_id = bs.product_id '
727- ];
728- $ selectWhere = [
729- $ connection ->quoteInto ('si.stock_status = ? ' , Stock::STOCK_IN_STOCK , \Zend_Db::INT_TYPE ),
730- $ connection ->quoteInto (
731- 'i.price_type = ? ' ,
732- \Magento \Bundle \Model \Product \Price::PRICE_TYPE_DYNAMIC ,
733- \Zend_Db::INT_TYPE
734- )
735- ];
736- $ select = sprintf (
737- "SELECT %s FROM %s WHERE %s " ,
738- implode (", " , $ selectColumns ),
739- implode ("\n" , $ selectFrom ),
740- implode (" AND " , $ selectWhere )
741- );
738+
742739 $ connection ->query (sprintf (
743740 "INSERT INTO ` " . $ this ->getBundleSelectionTable () . "` (%s) %s ON DUPLICATE KEY UPDATE %s " ,
744741 implode (", " , $ insertColumns ),
0 commit comments