Files
esun3d/app/code/Esun/Reward/Plugin/Checkout/SummaryLayoutProcessorPlugin.php
T
2026-09-15 10:35:56 +08:00

82 lines
2.8 KiB
PHP

<?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;
}
}