Created
May 25, 2020 07:19
-
-
Save cdiacon/2f036d2034b5f1ab010cd20a063e10b6 to your computer and use it in GitHub Desktop.
Magento2 access protected property for plugin
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
<?php | |
/** | |
* Imagination Media | |
* | |
* @category ImaginationMedia | |
* @package ImaginationMedia_ | |
* @author Calin Diacon <calin@imaginationmedia.com> | |
* @copyright Copyright (c) 2020 Imagination Media (https://www.imaginationmedia.com/) | |
* @license https://opensource.org/licenses/OSL-3.0.php Open Software License 3.0 | |
*/ | |
namespace ImaginationMedia\Catalog\Plugin; | |
use Magento\ConfigurableProduct\Pricing\Price\ConfigurablePriceResolver; | |
use Magento\ConfigurableProduct\Pricing\Price\LowestPriceOptionsProviderInterface; | |
use Magento\Framework\Pricing\SaleableInterface; | |
use ReflectionClass; | |
class PluginPrice | |
{ | |
/** | |
* @var LowestPriceOptionsProviderInterface | |
*/ | |
private $lowestPriceOptionsProvider; | |
public function __construct( | |
LowestPriceOptionsProviderInterface $lowestPriceOptionsProvider | |
) | |
{ | |
$this->lowestPriceOptionsProvider = $lowestPriceOptionsProvider; | |
} | |
/** | |
* @param ConfigurablePriceResolver $subject | |
* @param callable $proceed | |
* @param SaleableInterface $product | |
*/ | |
public function aroundResolvePrice( | |
ConfigurablePriceResolver $subject, | |
callable $proceed, | |
SaleableInterface $product | |
) { | |
$reflection = new ReflectionClass($subject); | |
$property = $reflection->getProperty('priceResolver'); | |
$property->setAccessible(true); | |
$priceResolver = $property->getValue($subject); | |
$price = null; | |
foreach ($this->lowestPriceOptionsProvider->getProducts($product) as $subProduct) { | |
$productPrice = $priceResolver->resolvePrice($subProduct); | |
$price = isset($price) ? min($price, $productPrice) : $productPrice; | |
} | |
return $proceed($product); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment