-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '2.4-develop' of https://github.com/mage-os/mirror-magento2
- Loading branch information
Showing
10 changed files
with
1,387 additions
and
58 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
app/code/Magento/Bundle/Model/Product/OriginalPrice.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
/************************************************************************ | ||
* | ||
* Copyright 2024 Adobe | ||
* All Rights Reserved. | ||
* | ||
* NOTICE: All information contained herein is, and remains | ||
* the property of Adobe and its suppliers, if any. The intellectual | ||
* and technical concepts contained herein are proprietary to Adobe | ||
* and its suppliers and are protected by all applicable intellectual | ||
* property laws, including trade secret and copyright laws. | ||
* Dissemination of this information or reproduction of this material | ||
* is strictly forbidden unless prior written permission is obtained | ||
* from Adobe. | ||
* ************************************************************************ | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Bundle\Model\Product; | ||
|
||
use Magento\Catalog\Model\Product; | ||
use Magento\Framework\Serialize\Serializer\Json; | ||
|
||
/** | ||
* Get original price for bundle products | ||
*/ | ||
class OriginalPrice | ||
{ | ||
/** | ||
* @param Json $serializer | ||
*/ | ||
public function __construct(private readonly Json $serializer) | ||
{ | ||
} | ||
|
||
/** | ||
* Get Original Total price for Bundle items | ||
* | ||
* @param Product $product | ||
* @return float | ||
*/ | ||
public function getTotalBundleItemsOriginalPrice(Product $product): float | ||
{ | ||
$price = 0.0; | ||
|
||
if (!$product->hasCustomOptions()) { | ||
return $price; | ||
} | ||
|
||
$selectionIds = $this->getBundleSelectionIds($product); | ||
|
||
if (empty($selectionIds)) { | ||
return $price; | ||
} | ||
|
||
$selections = $product->getTypeInstance()->getSelectionsByIds($selectionIds, $product); | ||
foreach ($selections->getItems() as $selection) { | ||
if (!$selection->isSalable()) { | ||
continue; | ||
} | ||
|
||
$selectionQty = $product->getCustomOption('selection_qty_' . $selection->getSelectionId()); | ||
if ($selectionQty) { | ||
$price += $this->getSelectionOriginalTotalPrice( | ||
$product, | ||
$selection, | ||
(float) $selectionQty->getValue() | ||
); | ||
} | ||
} | ||
|
||
return $price; | ||
} | ||
|
||
/** | ||
* Calculate total original price of selection | ||
* | ||
* @param Product $bundleProduct | ||
* @param Product $selectionProduct | ||
* @param float $selectionQty | ||
* | ||
* @return float | ||
*/ | ||
private function getSelectionOriginalTotalPrice( | ||
Product $bundleProduct, | ||
Product $selectionProduct, | ||
float $selectionQty | ||
): float { | ||
$price = $this->getSelectionOriginalPrice($bundleProduct, $selectionProduct); | ||
|
||
return $price * $selectionQty; | ||
} | ||
|
||
/** | ||
* Calculate the original price of selection | ||
* | ||
* @param Product $bundleProduct | ||
* @param Product $selectionProduct | ||
* | ||
* @return float | ||
*/ | ||
public function getSelectionOriginalPrice(Product $bundleProduct, Product $selectionProduct): float | ||
{ | ||
if ($bundleProduct->getPriceType() == Price::PRICE_TYPE_DYNAMIC) { | ||
return (float) $selectionProduct->getPrice(); | ||
} | ||
if ($selectionProduct->getSelectionPriceType()) { | ||
// percent | ||
return $bundleProduct->getPrice() * ($selectionProduct->getSelectionPriceValue() / 100); | ||
} | ||
|
||
// fixed | ||
return (float) $selectionProduct->getSelectionPriceValue(); | ||
} | ||
|
||
/** | ||
* Retrieve array of bundle selection IDs | ||
* | ||
* @param Product $product | ||
* @return array | ||
*/ | ||
private function getBundleSelectionIds(Product $product): array | ||
{ | ||
$customOption = $product->getCustomOption('bundle_selection_ids'); | ||
if ($customOption) { | ||
$selectionIds = $this->serializer->unserialize($customOption->getValue()); | ||
if (is_array($selectionIds)) { | ||
return $selectionIds; | ||
} | ||
} | ||
return []; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
app/code/Magento/Bundle/Plugin/Quote/UpdateBundleQuoteItemBaseOriginalPrice.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
/************************************************************************ | ||
* | ||
* Copyright 2024 Adobe | ||
* All Rights Reserved. | ||
* | ||
* NOTICE: All information contained herein is, and remains | ||
* the property of Adobe and its suppliers, if any. The intellectual | ||
* and technical concepts contained herein are proprietary to Adobe | ||
* and its suppliers and are protected by all applicable intellectual | ||
* property laws, including trade secret and copyright laws. | ||
* Dissemination of this information or reproduction of this material | ||
* is strictly forbidden unless prior written permission is obtained | ||
* from Adobe. | ||
* ************************************************************************ | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Bundle\Plugin\Quote; | ||
|
||
use Magento\Bundle\Model\Product\OriginalPrice; | ||
use Magento\Bundle\Model\Product\Type; | ||
use Magento\Quote\Model\Quote; | ||
use Magento\Quote\Model\Quote\Address\Total\Subtotal; | ||
use Magento\Quote\Api\Data\ShippingAssignmentInterface; | ||
use Magento\Quote\Model\Quote\Address\Total; | ||
|
||
/** | ||
* Update bundle base original price | ||
*/ | ||
class UpdateBundleQuoteItemBaseOriginalPrice | ||
{ | ||
/** | ||
* @param OriginalPrice $price | ||
*/ | ||
public function __construct( | ||
private readonly OriginalPrice $price | ||
) { | ||
} | ||
|
||
/** | ||
* Update bundle base original price | ||
* | ||
* @param Subtotal $subject | ||
* @param Subtotal $result | ||
* @param Quote $quote | ||
* @param ShippingAssignmentInterface $shippingAssignment | ||
* @param Total $total | ||
* | ||
* @return Subtotal | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function afterCollect( | ||
Subtotal $subject, | ||
Subtotal $result, | ||
Quote $quote, | ||
ShippingAssignmentInterface $shippingAssignment, | ||
Total $total | ||
): Subtotal { | ||
foreach ($quote->getAllVisibleItems() as $quoteItem) { | ||
if ($quoteItem->getProductType() === Type::TYPE_CODE) { | ||
$price = $quoteItem->getProduct()->getPrice(); | ||
$price += $this->price->getTotalBundleItemsOriginalPrice($quoteItem->getProduct()); | ||
$quoteItem->setBaseOriginalPrice($price); | ||
} | ||
} | ||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.