Skip to content

Commit 3d1b3c9

Browse files
authored
Removed redundant giftcard observer on core_block_abstract_to_html_after (#575)
1 parent 21dfd72 commit 3d1b3c9

File tree

8 files changed

+399
-229
lines changed

8 files changed

+399
-229
lines changed

.phpstorm.meta.php/models.meta.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,7 +1530,6 @@
15301530
'giftcard/resource_indexer_price' => \Maho_Giftcard_Model_Resource_Indexer_Price::class,
15311531
'giftcard/total_creditmemo' => \Maho_Giftcard_Model_Total_Creditmemo::class,
15321532
'giftcard/total_invoice' => \Maho_Giftcard_Model_Total_Invoice::class,
1533-
'giftcard/total_order' => \Maho_Giftcard_Model_Total_Order::class,
15341533
'giftcard/total_quote' => \Maho_Giftcard_Model_Total_Quote::class,
15351534
'giftcard_resource/giftcard' => \Maho_Giftcard_Model_Resource_Giftcard::class,
15361535
'giftcard_resource/giftcard_collection' => \Maho_Giftcard_Model_Resource_Giftcard_Collection::class,
@@ -4257,7 +4256,6 @@
42574256
'giftcard/resource_indexer_price' => \Maho_Giftcard_Model_Resource_Indexer_Price::class,
42584257
'giftcard/total_creditmemo' => \Maho_Giftcard_Model_Total_Creditmemo::class,
42594258
'giftcard/total_invoice' => \Maho_Giftcard_Model_Total_Invoice::class,
4260-
'giftcard/total_order' => \Maho_Giftcard_Model_Total_Order::class,
42614259
'giftcard/total_quote' => \Maho_Giftcard_Model_Total_Quote::class,
42624260
'giftcard_resource/giftcard' => \Maho_Giftcard_Model_Resource_Giftcard::class,
42634261
'giftcard_resource/giftcard_collection' => \Maho_Giftcard_Model_Resource_Giftcard_Collection::class,

app/code/core/Maho/Giftcard/Block/Adminhtml/Sales/Order/Creditmemo/Totals/Giftcard.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,22 @@ public function initTotals()
6868

6969
$baseGiftcardAmount = $source->getBaseGiftcardAmount() ?: ($order ? $order->getBaseGiftcardAmount() : 0);
7070

71-
// Add total after tax - show as positive since it's money going back to gift card
72-
// Use a custom block_html to avoid it being summed into grand_total display
73-
$parent->addTotal(new Maho\DataObject([
71+
// Show as positive since it's money going back to gift card
72+
// Add before grand_total
73+
$parent->addTotalBefore(new Maho\DataObject([
7474
'code' => 'giftcard',
7575
'value' => abs((float) $giftcardAmount),
7676
'base_value' => abs((float) $baseGiftcardAmount),
7777
'label' => $label,
7878
'is_formated' => false,
79-
]), 'tax');
79+
]), ['grand_total', 'base_grandtotal']);
80+
81+
// Ensure tax appears before giftcard
82+
$taxTotal = $parent->getTotal('tax');
83+
if ($taxTotal) {
84+
$parent->removeTotal('tax');
85+
$parent->addTotalBefore($taxTotal, 'giftcard');
86+
}
8087

8188
return $this;
8289
}

app/code/core/Maho/Giftcard/Block/Adminhtml/Sales/Order/Invoice/Totals/Giftcard.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,20 @@ public function initTotals()
7171
$baseGiftcardAmount = $order->getBaseGiftcardAmount();
7272
}
7373

74-
// Add total after tax
75-
$parent->addTotal(new Maho\DataObject([
74+
// Add before grand_total
75+
$parent->addTotalBefore(new Maho\DataObject([
7676
'code' => 'giftcard',
7777
'value' => -abs((float) $giftcardAmount),
7878
'base_value' => -abs((float) $baseGiftcardAmount),
7979
'label' => $label,
80-
]), 'tax');
80+
]), ['grand_total', 'base_grandtotal']);
81+
82+
// Ensure tax appears before giftcard
83+
$taxTotal = $parent->getTotal('tax');
84+
if ($taxTotal) {
85+
$parent->removeTotal('tax');
86+
$parent->addTotalBefore($taxTotal, 'giftcard');
87+
}
8188

8289
return $this;
8390
}

app/code/core/Maho/Giftcard/Block/Adminhtml/Sales/Order/Totals/Giftcard.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* Maho
57
*
@@ -33,7 +35,7 @@ public function initTotals()
3335

3436
$giftcardAmount = $order->getGiftcardAmount();
3537

36-
if ($giftcardAmount != 0) {
38+
if ((float) $giftcardAmount !== 0.0) {
3739
// Get gift card codes for display
3840
$codes = [];
3941
$giftcardCodes = $order->getGiftcardCodes();
@@ -49,13 +51,20 @@ public function initTotals()
4951
$label .= ' (' . implode(', ', $codes) . ')';
5052
}
5153

52-
// Add after tax
53-
$parent->addTotal(new Maho\DataObject([
54+
// Add before grand_total
55+
$parent->addTotalBefore(new Maho\DataObject([
5456
'code' => 'giftcard',
5557
'value' => -abs((float) $giftcardAmount),
5658
'base_value' => -abs((float) $order->getBaseGiftcardAmount()),
5759
'label' => $label,
58-
]), 'tax');
60+
]), ['grand_total', 'base_grandtotal']);
61+
62+
// Ensure tax appears before giftcard
63+
$taxTotal = $parent->getTotal('tax');
64+
if ($taxTotal) {
65+
$parent->removeTotal('tax');
66+
$parent->addTotalBefore($taxTotal, 'giftcard');
67+
}
5968
}
6069

6170
return $this;

app/code/core/Maho/Giftcard/Model/Observer.php

Lines changed: 9 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ protected function _createGiftcard(
9898

9999
// Convert amount to base currency using order's conversion rate
100100
// Gift card amount is in order currency, need to convert to base currency
101-
$baseAmount = $amount * $order->getBaseToOrderRate();
101+
// base_to_order_rate converts base→order, so to go order→base we divide
102+
$baseToOrderRate = (float) $order->getBaseToOrderRate();
103+
$baseAmount = $baseToOrderRate > 0 ? $amount / $baseToOrderRate : $amount;
102104

103105
$giftcard = Mage::getModel('giftcard/giftcard');
104106
$giftcard->setData([
@@ -304,15 +306,12 @@ public function applyGiftcardToOrder(Maho\Event\Observer $observer)
304306

305307
if ($baseGiftcardAmount > 0) {
306308
// Set gift card amounts on order
309+
// Grand total is already reduced during quote total collection
307310
$order->setBaseGiftcardAmount($baseGiftcardAmount);
308311
$order->setGiftcardAmount($giftcardAmount);
309312
if ($giftcardCodes) {
310313
$order->setGiftcardCodes($giftcardCodes);
311314
}
312-
313-
// Reduce grand total by gift card amount
314-
$order->setGrandTotal(max(0, $order->getGrandTotal() - $giftcardAmount));
315-
$order->setBaseGrandTotal(max(0, $order->getBaseGrandTotal() - $baseGiftcardAmount));
316315
}
317316
}
318317

@@ -375,17 +374,15 @@ public function deductGiftcardBalance(Maho\Event\Observer $observer)
375374
}
376375
}
377376

378-
// Set gift card amounts on order
377+
// Set gift card amounts on order (codes/amounts may already be set
378+
// by fieldset conversion + applyGiftcardToOrder, but ensure they're present)
379379
$order->setGiftcardCodes($giftcardCodes);
380380
$order->setBaseGiftcardAmount($baseGiftcardAmount);
381381
$order->setGiftcardAmount($giftcardAmount);
382382

383-
// Reduce grand total by gift card amount (if not already reduced)
384-
$currentGrandTotal = (float) $order->getGrandTotal();
385-
if ($giftcardAmount > 0 && $currentGrandTotal > 0) {
386-
$order->setGrandTotal(max(0, $currentGrandTotal - $giftcardAmount));
387-
$order->setBaseGrandTotal(max(0, (float) $order->getBaseGrandTotal() - $baseGiftcardAmount));
388-
}
383+
// Grand total is NOT modified here — it was already reduced during
384+
// quote total collection via Total_Quote::collect() → _addAmount(-$amount)
385+
// and carried over to the order during quote-to-order conversion.
389386

390387
// Add gift card info to payment additional information for display in grid
391388
$payment = $order->getPayment();
@@ -494,63 +491,6 @@ public function deductGiftcardBalance(Maho\Event\Observer $observer)
494491
}
495492
}
496493

497-
/**
498-
* Add gift card total to admin order view
499-
*
500-
* @return void
501-
*/
502-
public function addGiftcardTotalToAdminOrder(Maho\Event\Observer $observer)
503-
{
504-
$block = $observer->getEvent()->getBlock();
505-
506-
// Check if this is an order totals block
507-
if ($block->getNameInLayout() != 'order_totals') {
508-
return;
509-
}
510-
511-
$order = $block->getOrder();
512-
if (!$order || !$order->getId()) {
513-
return;
514-
}
515-
516-
$giftcardAmount = $order->getGiftcardAmount();
517-
518-
if ($giftcardAmount != 0) {
519-
// Get gift card codes for display
520-
$codes = [];
521-
$giftcardCodes = $order->getGiftcardCodes();
522-
if ($giftcardCodes) {
523-
$codesArray = json_decode($giftcardCodes, true);
524-
if (is_array($codesArray)) {
525-
// Show partial codes for security
526-
foreach (array_keys($codesArray) as $code) {
527-
if (strlen($code) > 10) {
528-
$codes[] = substr($code, 0, 5) . '...' . substr($code, -4);
529-
} else {
530-
$codes[] = $code;
531-
}
532-
}
533-
}
534-
}
535-
536-
$label = Mage::helper('giftcard')->__('Gift Cards');
537-
if ($codes !== []) {
538-
$label .= ' (' . implode(', ', $codes) . ')';
539-
}
540-
541-
$total = new Maho\DataObject([
542-
'code' => 'giftcard',
543-
'value' => -abs((float) $giftcardAmount),
544-
'base_value' => -abs((float) $order->getBaseGiftcardAmount()),
545-
'label' => $label,
546-
'strong' => false,
547-
]);
548-
549-
// Add after tax, before grand_total
550-
$block->addTotal($total, 'tax');
551-
}
552-
}
553-
554494
/**
555495
* Refund gift card balance when credit memo is created
556496
*

app/code/core/Maho/Giftcard/Model/Total/Order.php

Lines changed: 0 additions & 69 deletions
This file was deleted.

app/code/core/Maho/Giftcard/etc/config.xml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,6 @@
1515
</giftcard>
1616
</totals>
1717
</quote>
18-
<order>
19-
<totals>
20-
<giftcard>
21-
<class>giftcard/total_order</class>
22-
<after>subtotal,shipping,discount,tax</after>
23-
<before>grand_total</before>
24-
</giftcard>
25-
</totals>
26-
</order>
2718
</sales>
2819

2920
<models>
@@ -160,14 +151,6 @@
160151
</giftcard_create_on_payment>
161152
</observers>
162153
</sales_order_invoice_pay>
163-
<core_block_abstract_to_html_after>
164-
<observers>
165-
<giftcard_add_total_to_admin>
166-
<class>giftcard/observer</class>
167-
<method>addGiftcardTotalToAdminOrder</method>
168-
</giftcard_add_total_to_admin>
169-
</observers>
170-
</core_block_abstract_to_html_after>
171154
<sales_convert_quote_address_to_order>
172155
<observers>
173156
<giftcard_apply_to_order>

0 commit comments

Comments
 (0)