Skip to content

Commit

Permalink
LYNX_387: Fix bundle original_row_total field (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliseacornejo committed May 9, 2024
1 parent d3057b6 commit 3ebda94
Show file tree
Hide file tree
Showing 4 changed files with 911 additions and 0 deletions.
133 changes: 133 additions & 0 deletions app/code/Magento/Bundle/Model/Product/OriginalPrice.php
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 [];
}
}
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;
}
}
5 changes: 5 additions & 0 deletions app/code/Magento/Bundle/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,9 @@
</argument>
</arguments>
</type>
<type name="Magento\Quote\Model\Quote\Address\Total\Subtotal">
<plugin name="update_bundle_quote_item_base_original_price"
type="Magento\Bundle\Plugin\Quote\UpdateBundleQuoteItemBaseOriginalPrice"
sortOrder="10"/>
</type>
</config>
Loading

0 comments on commit 3ebda94

Please sign in to comment.