Show value of all totals in controller action
-
15-02-2021 - |
Question
I need to log the values of all totals from a controller action. If the controller action is called, then i need a list rendered which shows the totals:
If you are not familiar with totals then look at the screenshot of the cart below:
All items in the list are totals.
How can I render a specific total after my controller action is called?
For example I need my controller action getGesamtsummeAction()
to get the value of GESAMTSUMME
(grand_total) ,RAL ZWISCHENSUMME
(ral_zwischensumme) and all the others and log or show them.
I need help to write the code for the controller action, everything else is already done.
public function getGesamtsummeAction()
{
$customerId = Mage::getSingleton('customer/session')->getId();
$customer = Mage::getModel('customer/customer')->load($customerId);
...???
}
Install script:
<?php
/**
* @var Mage_Sales_Model_Mysql4_Setup $installer
*/
Mage::Log("wr_epo_sales_setup: ral_zwischensumme überall hinzugefügt", 7, "setup.log");
$installer = $this;
$installer->startSetup();
$installer->addAttribute('quote_item', 'ral_zwischensumme',
array(
'type' => 'decimal',
'label' => 'RAL Zwischensumme'
)
);
$installer->addAttribute('order_item', 'ral_zwischensumme',
array(
'type' => 'decimal',
'label' => 'RAL Zwischensumme'
)
);
$installer->addAttribute('invoice', 'ral_zwischensumme',
array(
'type' => 'decimal',
'label' => 'RAL Zwischensumme'
)
);
$installer->addAttribute('creditmemo', 'ral_zwischensumme',
array(
'type' => 'decimal',
'label' => 'RAL Zwischensumme'
)
);
$installer->endSetup();
Model:
<?php
class WR_EPO_Model_Quote_Address_Total_Ral_Zwischensumme extends Mage_Sales_Model_Quote_Address_Total_Abstract
{
/**
* Berechnen des gesamten Versandaufpreises
*
* @param Mage_Sales_Model_Quote_Address $address
* @return Webkochshop_Versandaufpreis_Model_Quote_Address_Total_Shipping_Surcharge
*/
public function collect(Mage_Sales_Model_Quote_Address $address)
{
parent::collect($address);
$total = Mage::helper("wr_epo")->getRalZwischensumme($address);
$quote = Mage::getModel('checkout/session')->getQuote();
/*
* In dem Address Model ablegen zur späteren Referenz in fetch()
*/
$address->setRalZwischensumme($total);
if ($total > 0) {
$quote->setRalZwischensumme($total);
}
return $this;
}
/**
* Den Versandaufpreis in die Basiswährung des Shops umrechnen
*
* @param float $amount
* @return float $amount konvertiert in die Basis Währung
*/
public function _getBaseAmount($amount)
{
$currentCurrency = Mage::app()->getStore()->getCurrentCurrency();
$baseCurrency = Mage::app()->getStore()->getBaseCurrency();
if ($baseCurrency->getCode() == $currentCurrency->getCode())
{
$baseAmount = $amount;
}
else
{
$baseAmount = Mage::helper('directory')->currencyConvert($amount, $currentCurrency, $baseCurrency);
}
return $baseAmount;
}
/**
* Zuweisen des Versandaufpreises an das Address Objekt zur Anzeige
*
* @param Mage_Sales_Model_Quote_Address $address
* @return Webkochshop_Versandaufpreis_Model_Quote_Address_Total_Shipping_Surcharge
*/
public function fetch(Mage_Sales_Model_Quote_Address $address)
{
if ($address->getRalZwischensumme() > 0)
{
$address->addTotal(array(
'code' => $this->getCode(),
'title' => 'RAL Zwischensumme',
'value' => $address->getRalZwischensumme()
));
}
return $this;
}
}
Update:
Im not 100% sure yet, but I think I needed to also add the attribute to the quote table, now my custom total "ral_zwischensumme" is ready and can show. It can show after I added it, but I also did other stuff, so Im not sure if it works because of that stuff or because of the install script. I have to check.
<?php
/**
* @var Mage_Sales_Model_Mysql4_Setup $installer
*/
Mage::Log("wr_epo_sales_setup: ral_zwischensumme zu quote hinzugefügt", 7, "setup.log", true);
$installer = $this;
$installer->startSetup();
$installer->addAttribute('quote', 'ral_zwischensumme',
array(
'type' => 'decimal',
'label' => 'RAL Zwischensumme'
)
);
$installer->endSetup();
Solution
This should work:
$quote = Mage::getModel('checkout/session')->getQuote();
$address = $quote->getShippingAddress();
$quoteData = $quote->getData(); //getData() is not necessary, you could also directly access the getter methods, hint: use var_dump($quoteData) to see all the data.
$addressData = $address->getData();
$tax = $addressData['tax_amount']; //equal to $address->getData('tax_amount') and equal to $address->getTaxAmount();
$shipping = $address->getData('shipping_amount');
$subtotal = $quote->getSubtotal();
$ralZwischensumme = $quote->getRalZwischensumme();
$grandTotal = $address->getGrandTotal();