|
| 1 | +<?php |
| 2 | +/************************************************************************ |
| 3 | + * |
| 4 | + * Copyright 2024 Adobe |
| 5 | + * All Rights Reserved. |
| 6 | + * |
| 7 | + * NOTICE: All information contained herein is, and remains |
| 8 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 9 | + * and technical concepts contained herein are proprietary to Adobe |
| 10 | + * and its suppliers and are protected by all applicable intellectual |
| 11 | + * property laws, including trade secret and copyright laws. |
| 12 | + * Dissemination of this information or reproduction of this material |
| 13 | + * is strictly forbidden unless prior written permission is obtained |
| 14 | + * from Adobe. |
| 15 | + * ************************************************************************ |
| 16 | + */ |
| 17 | +declare(strict_types=1); |
| 18 | + |
| 19 | +namespace Magento\Bundle\Model\Product; |
| 20 | + |
| 21 | +use Magento\Catalog\Model\Product; |
| 22 | +use Magento\Framework\Serialize\Serializer\Json; |
| 23 | + |
| 24 | +/** |
| 25 | + * Get original price for bundle products |
| 26 | + */ |
| 27 | +class OriginalPrice |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @param Json $serializer |
| 31 | + */ |
| 32 | + public function __construct(private readonly Json $serializer) |
| 33 | + { |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Get Original Total price for Bundle items |
| 38 | + * |
| 39 | + * @param Product $product |
| 40 | + * @return float |
| 41 | + */ |
| 42 | + public function getTotalBundleItemsOriginalPrice(Product $product): float |
| 43 | + { |
| 44 | + $price = 0.0; |
| 45 | + |
| 46 | + if (!$product->hasCustomOptions()) { |
| 47 | + return $price; |
| 48 | + } |
| 49 | + |
| 50 | + $selectionIds = $this->getBundleSelectionIds($product); |
| 51 | + |
| 52 | + if (empty($selectionIds)) { |
| 53 | + return $price; |
| 54 | + } |
| 55 | + |
| 56 | + $selections = $product->getTypeInstance()->getSelectionsByIds($selectionIds, $product); |
| 57 | + foreach ($selections->getItems() as $selection) { |
| 58 | + if (!$selection->isSalable()) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + $selectionQty = $product->getCustomOption('selection_qty_' . $selection->getSelectionId()); |
| 63 | + if ($selectionQty) { |
| 64 | + $price += $this->getSelectionOriginalTotalPrice( |
| 65 | + $product, |
| 66 | + $selection, |
| 67 | + (float) $selectionQty->getValue() |
| 68 | + ); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return $price; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Calculate total original price of selection |
| 77 | + * |
| 78 | + * @param Product $bundleProduct |
| 79 | + * @param Product $selectionProduct |
| 80 | + * @param float $selectionQty |
| 81 | + * |
| 82 | + * @return float |
| 83 | + */ |
| 84 | + private function getSelectionOriginalTotalPrice( |
| 85 | + Product $bundleProduct, |
| 86 | + Product $selectionProduct, |
| 87 | + float $selectionQty |
| 88 | + ): float { |
| 89 | + $price = $this->getSelectionOriginalPrice($bundleProduct, $selectionProduct); |
| 90 | + |
| 91 | + return $price * $selectionQty; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Calculate the original price of selection |
| 96 | + * |
| 97 | + * @param Product $bundleProduct |
| 98 | + * @param Product $selectionProduct |
| 99 | + * |
| 100 | + * @return float |
| 101 | + */ |
| 102 | + public function getSelectionOriginalPrice(Product $bundleProduct, Product $selectionProduct): float |
| 103 | + { |
| 104 | + if ($bundleProduct->getPriceType() == Price::PRICE_TYPE_DYNAMIC) { |
| 105 | + return (float) $selectionProduct->getPrice(); |
| 106 | + } |
| 107 | + if ($selectionProduct->getSelectionPriceType()) { |
| 108 | + // percent |
| 109 | + return $bundleProduct->getPrice() * ($selectionProduct->getSelectionPriceValue() / 100); |
| 110 | + } |
| 111 | + |
| 112 | + // fixed |
| 113 | + return (float) $selectionProduct->getSelectionPriceValue(); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Retrieve array of bundle selection IDs |
| 118 | + * |
| 119 | + * @param Product $product |
| 120 | + * @return array |
| 121 | + */ |
| 122 | + private function getBundleSelectionIds(Product $product): array |
| 123 | + { |
| 124 | + $customOption = $product->getCustomOption('bundle_selection_ids'); |
| 125 | + if ($customOption) { |
| 126 | + $selectionIds = $this->serializer->unserialize($customOption->getValue()); |
| 127 | + if (is_array($selectionIds)) { |
| 128 | + return $selectionIds; |
| 129 | + } |
| 130 | + } |
| 131 | + return []; |
| 132 | + } |
| 133 | +} |
0 commit comments