init
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Reward\Plugin\Checkout;
|
||||
|
||||
use Amasty\CheckoutCore\Block\Onepage\LayoutProcessor;
|
||||
|
||||
/**
|
||||
* Places checkout promotions inside Order Summary, directly before totals.
|
||||
*/
|
||||
class SummaryLayoutProcessorPlugin
|
||||
{
|
||||
public function afterProcess(LayoutProcessor $subject, array $jsLayout): array
|
||||
{
|
||||
$checkout = &$jsLayout['components']['checkout']['children'];
|
||||
$sidebar = &$checkout['sidebar']['children'];
|
||||
$summary = &$sidebar['summary']['children'];
|
||||
|
||||
if (!is_array($summary)) {
|
||||
return $jsLayout;
|
||||
}
|
||||
|
||||
$summary['cart_items']['sortOrder'] = 10;
|
||||
|
||||
// Amasty moves Magento's coupon component to summary_additional, which
|
||||
// renders after totals. Move that same component above totals instead.
|
||||
$additional = &$sidebar['summary_additional']['children'];
|
||||
if (isset($additional['discount']) && is_array($additional['discount'])) {
|
||||
$discount = $additional['discount'];
|
||||
unset($additional['discount']);
|
||||
$discount['sortOrder'] = 20;
|
||||
$discount['config']['template'] = 'Magento_SalesRule/payment/discount';
|
||||
$summary['discount-code'] = $discount;
|
||||
}
|
||||
|
||||
// Depending on Amasty configuration, third-party payment additions can
|
||||
// be moved or renamed before this processor finishes. Extract the
|
||||
// component wherever it ended up; create it when Amasty removed it.
|
||||
$reward = $this->extractComponent(
|
||||
$checkout,
|
||||
'Esun_Reward/js/view/payment/reward'
|
||||
) ?? [
|
||||
'component' => 'Esun_Reward/js/view/payment/reward'
|
||||
];
|
||||
$reward['sortOrder'] = 21;
|
||||
$summary['esun-reward'] = $reward;
|
||||
|
||||
// Amasty supplies an expandable details template. Use the theme's
|
||||
// always-open Magento template so product options display immediately.
|
||||
if (isset($summary['cart_items']['children']['details'])) {
|
||||
$summary['cart_items']['children']['details']['config']['template'] =
|
||||
'Magento_Checkout/summary/item/details';
|
||||
}
|
||||
|
||||
$summary['totals']['sortOrder'] = 30;
|
||||
|
||||
return $jsLayout;
|
||||
}
|
||||
|
||||
private function extractComponent(array &$node, string $component): ?array
|
||||
{
|
||||
foreach (array_keys($node) as $key) {
|
||||
if (!isset($node[$key]) || !is_array($node[$key])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (($node[$key]['component'] ?? null) === $component) {
|
||||
$result = $node[$key];
|
||||
unset($node[$key]);
|
||||
return $result;
|
||||
}
|
||||
|
||||
$result = $this->extractComponent($node[$key], $component);
|
||||
if ($result !== null) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Reward\Plugin\CustomerData;
|
||||
|
||||
use Esun\Reward\Model\BalanceManager;
|
||||
use Esun\Reward\Model\Config;
|
||||
use Magento\Checkout\CustomerData\Cart;
|
||||
use Magento\Checkout\Helper\Data as CheckoutHelper;
|
||||
use Magento\Checkout\Model\Cart as CheckoutCart;
|
||||
use Magento\Customer\Helper\Session\CurrentCustomer;
|
||||
|
||||
class CartPlugin
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CheckoutCart $checkoutCart,
|
||||
private readonly CurrentCustomer $currentCustomer,
|
||||
private readonly BalanceManager $balances,
|
||||
private readonly Config $config,
|
||||
private readonly CheckoutHelper $checkoutHelper
|
||||
) {}
|
||||
public function afterGetSectionData(Cart $subject, array $result): array
|
||||
{
|
||||
// Use the same cart model and current-customer helper as Magento's cart
|
||||
// and customer sections. Injecting fresh session models here can create
|
||||
// a second, empty quote/session during customer/section/load.
|
||||
$quote = $this->checkoutCart->getQuote();
|
||||
$customerId = (int)$this->currentCustomer->getCustomerId()
|
||||
?: (int)$quote->getCustomerId();
|
||||
$balance = $customerId > 0 ? (int)$this->balances->getAccount($customerId, (int)$quote->getStore()->getWebsiteId())['balance'] : 0;
|
||||
$result['reward'] = ['logged_in' => $customerId > 0, 'balance' => $balance, 'applied_points' => (int)$quote->getData('reward_points_used'), 'maximum' => $this->config->maxCheckoutPoints((int)$quote->getStoreId()), 'rate' => $this->config->redemptionRate((int)$quote->getStoreId())];
|
||||
if (isset($result['drawer_totals'])) { $result['drawer_totals']['reward_discount'] = (float)$quote->getData('reward_discount_amount') > 0 ? '-' . $this->checkoutHelper->formatPrice((float)$quote->getData('reward_discount_amount')) : ''; }
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user