From 57f4be471240092fc014ae2b2cf7924560ad17a0 Mon Sep 17 00:00:00 2001 From: Serhii Koval Date: Sat, 16 Aug 2025 21:34:12 +0200 Subject: [PATCH] Optimize performance for method \Bread\BreadCheckout\Block\Checkout\Minicart::isActive --- Block/Checkout/Minicart.php | 79 +++++++++++++------ .../Method/BreadPaymentMethodFactory.php | 25 +++++- etc/frontend/di.xml | 9 ++- 3 files changed, 87 insertions(+), 26 deletions(-) diff --git a/Block/Checkout/Minicart.php b/Block/Checkout/Minicart.php index 7863d8c..e5563bd 100644 --- a/Block/Checkout/Minicart.php +++ b/Block/Checkout/Minicart.php @@ -2,10 +2,21 @@ namespace Bread\BreadCheckout\Block\Checkout; +use Bread\BreadCheckout\Helper\Catalog; +use Bread\BreadCheckout\Helper\Customer; +use Bread\BreadCheckout\Helper\Quote; use Bread\BreadCheckout\Model\Payment\Method\BreadPaymentMethodFactory; use Magento\Catalog\Block\ShortcutInterface; +use Magento\Catalog\Helper\Product; use Magento\Checkout\Model\Session; +use Magento\ConfigurableProduct\Model\ConfigurableAttributeData; +use Magento\ConfigurableProduct\Model\Product\Type\ConfigurableFactory; +use Magento\Customer\Helper\Session\CurrentCustomer; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Json\EncoderInterface; use Magento\Framework\Locale\ResolverInterface; +use Magento\Framework\Pricing\PriceCurrencyInterface; +use Magento\Framework\Stdlib\ArrayUtils; use Magento\Framework\View\Element\Template\Context; use Magento\Payment\Model\MethodInterface; use Bread\BreadCheckout\Helper\Data; @@ -39,12 +50,25 @@ class Minicart extends Overview implements ShortcutInterface /** * Minicart constructor. * - * @param Context $context - * @param ResolverInterface $localeResolver - * @param Session $checkoutSession - * @param BreadPaymentMethodFactory $breadPaymentMethodFactory - * @param Data $helperData - * @param array $data + * @param \Magento\Catalog\Block\Product\Context $context + * @param \Magento\Framework\Json\Helper\Data $jsonHelper + * @param Catalog $catalogHelper + * @param Customer $customerHelper + * @param Data $dataHelper + * @param ConfigurableFactory $configurableProductFactory + * @param \Magento\ConfigurableProduct\Block\Product\View\Type\ConfigurableFactory $configurableBlockFactory + * @param Quote $quoteHelper + * @param ArrayUtils $arrayUtils + * @param EncoderInterface $jsonEncoder + * @param \Magento\ConfigurableProduct\Helper\Data $configurableHelper + * @param Product $catalogProductHelper + * @param CurrentCustomer $currentCustomer + * @param PriceCurrencyInterface $priceCurrency + * @param ConfigurableAttributeData $configurableAttributeData + * @param Session $checkoutSession + * @param BreadPaymentMethodFactory $paymentFactory + * @param Data $helperData + * @param array $data */ public function __construct( \Magento\Catalog\Block\Product\Context $context, @@ -63,7 +87,7 @@ public function __construct( \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency, \Magento\ConfigurableProduct\Model\ConfigurableAttributeData $configurableAttributeData, Session $checkoutSession, - BreadPaymentMethodFactory $breadPaymentMethodFactory, + private BreadPaymentMethodFactory $paymentFactory, Data $helperData, array $data = [] ) { @@ -87,7 +111,6 @@ public function __construct( ); $this->checkoutSession = $checkoutSession; - $this->payment = $breadPaymentMethodFactory->create(); $this->helperData = $helperData; $this->quoteHelper = $quoteHelper; } @@ -120,25 +143,37 @@ public function getContainerId() } /** + * Check if the block should be displayed + * * @return bool - * @throws \Magento\Framework\Exception\LocalizedException + * @throws \Exception */ public function isActive() { + $show = ($this->isApiVersionIsBread2()) + ? $this->helperData->showMinicartLink() + : $this->helperData->allowMinicartCheckout(); + if (!$show) { + return false; + } + + if (!$this->payment) { + $this->payment = $this->paymentFactory->create(); + } + + return $this->payment->isAvailable($this->checkoutSession->getQuote()) && + !$this->isCartView() && + $this->quoteHelper->aboveThreshold($this->quoteHelper->getGrandTotal()/100); + } - $aboveThreshold = $this->quoteHelper->aboveThreshold($this->quoteHelper->getGrandTotal()/100); - $apiVersion = $this->helperData->getApiVersion(); - if($apiVersion === 'bread_2') { - return $this->payment->isAvailable($this->checkoutSession->getQuote()) && - $this->helperData->showMinicartLink() && - !$this->isCartView() && - $aboveThreshold; - } else { - return $this->payment->isAvailable($this->checkoutSession->getQuote()) && - $this->helperData->allowMinicartCheckout() && - !$this->isCartView() && - $aboveThreshold; - } + /** + * Check if API version is bread_2 + * + * @return bool + */ + private function isApiVersionIsBread2(): bool + { + return $this->helperData->getApiVersion() === 'bread_2'; } /** diff --git a/Model/Payment/Method/BreadPaymentMethodFactory.php b/Model/Payment/Method/BreadPaymentMethodFactory.php index 79421e1..c7b7389 100644 --- a/Model/Payment/Method/BreadPaymentMethodFactory.php +++ b/Model/Payment/Method/BreadPaymentMethodFactory.php @@ -1,12 +1,20 @@ -helper = $helper; $this->quoteHelper = $quoteHelper; } - public function create($quote = null, array $data = array()) + + /** + * Create Payment Method. + * + * @param null $quote + * @param array $data + * + * @return mixed + * @throws \Exception + */ + public function create($quote = null, array $data = []) { $currentCurrencyCode = $this->helper->getCurrentCurrencyCode(); // Get currency from quote if it was passed in @@ -31,6 +49,7 @@ public function create($quote = null, array $data = array()) } else { $instanceName = 'Bread\BreadCheckout\Model\Payment\Method\Bread'; } + return $this->objectManager->create($instanceName, $data); } } diff --git a/etc/frontend/di.xml b/etc/frontend/di.xml index 4b5dfd1..f4dee46 100644 --- a/etc/frontend/di.xml +++ b/etc/frontend/di.xml @@ -15,6 +15,7 @@ + Magento\Checkout\Model\Session\Proxy Bread_BreadCheckout::breadcheckout/minicart.phtml breadcheckout.mini-cart @@ -23,4 +24,10 @@ Bread\BreadCheckout\Model\Payment\Method\BreadPaymentMethodFactory - \ No newline at end of file + + + + Magento\Checkout\Model\Session\Proxy + + +