Magento failure.phtml, get the order total - magento

One of the projects I work on has the failure page and success page in magento as exactly the same and requires some tracking code on both. The tracking code requires the order ID and the amount being ordered. The issue I'm having is trying to get the order total on the failure page.
The methods built into the block class Mage_Checkout_Block_Onepage_Failure are getRealOrderId(), getErrorMessage() and getContinueShoppingUrl(), so no help there.
I found some code to do what I want on the success page but it doesn't work for the failure page.
<?php
$orderId = $this->getRealOrderId();
$order = Mage::getSingleton('sales/order');
$order->load($orderId);
$_totalData = $order->getData();
$_grand = $_totalData['grand_total'];
?>
I dumped $order and $_totalData. $_totalData just an empty array and $order an empty Mage_Sales_Model_Order instance.
Does anyone have any suggestions for where to get the grand total of what is being purchased?
Regards,
Kieran
(I'm also having issues testing the success page as I can't get to it, even putting in the correct test card details on the dev server - but I'll find a way around this)

If the order failed then there is no order to use, that seems sensible.
Perhaps, also, the cart hasn't been emptied in which case you can get the active totals with
$totals = Mage::helper('checkout')->getQuote()->getTotals();
$grand = $totals['grand_total'];

Related

Product model not returning description

Which reasons can it have, when a product model is not returning the description attribute?
I tested several approaches:
Mage::getModel('catalog/product')->loadByAttribute('sku', 'P001')->getData();
Mage::getModel('catalog/product')->loadByAttribute('sku', 'P001')->getData('description');
Mage::getModel('catalog/product')->loadByAttribute('sku', 'P001')->getDescription();
The getData() method returns even the short_description, but not the description.
I think it can't be a code fault, because in my local environment, it's working. Via git, I have the same codebase also on my stage server, where it's not working anymore.
Can somebody have edited some attribute settings to cause that problem? (I couldn't find any differences between short_description and description which could cause that problem in my opinion.
EDIT: On the stage page, descriptions are shown in articles.
To get the product details by loading sku, use the below code
<?php
$product_sku = "P001";
$product = Mage::getModel('catalog/product')->loadBySku($product_sku);
echo $product->getDescription();
?>

Magento : Taking 35 Second to load 4 product's

I have created a extension that filters product collection based on a attribute.
Below is the controllers, block and view template code.
Controller
$url = Mage::getUrl('no-route');
if(Mage::app()->getRequest()->getParam('ajax')){
echo $this->getLayout()->createBlock('catalogextensions/bestsellers_home_list')
->setTemplate('catalog/landings/bestseller.phtml')
->toHtml();
}
else{
$this->loadLayout();
$this->getLayout()->getBlock('head')->setTitle('Besesellers');
$this->renderLayout();
}
Block product collection function
$storeId = Mage::app()->getStore()->getId();
$products = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter(array( array( 'attribute'=>'top_seller', 'eq' => '1' )));
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($products);
$products->getSelect()->limit(4,$this->get_cur_page());
return $products ;
Product collection is iterated on view.phtml template.
Now, it is taking 35 seconds to get the output, below are the statistics of execution that i was able to get by making use of PHP's microtime() function
For
Block(product collection ) it taking around 0.01 second
Template Rendering its taking around 0.12 second
But for controller function it is taking around 35 second
I am not able to find what to check, because controller function just create a Block at runtime.
*Note:I am making use of a Paid full page cache Extension "Mirasvit FPC".
how can i find it what is taking time
Server configuration is
30GB Ram with 4 vCPU.
Application server : Nginx + php5-fpm.
Version: Magento CE 1.8.0.1
Thanks.
You use a bit deprecated way to call the filters. I don't know if that can really influate the time of loading but using addFieldToFilter() would limit the calls to database and can be usefull. You can use :
Magento: Filter products by Status
filtering product collection on "is_salable"
The block is maybe not cached correctly. I mean the products are load each time the page is called.
Make sure your flat product is set to on and all attributes needed for your filters are set as filter.
If a listing comes from EAv tables it might consume lots of resources thus slowing down.
Additionally apply optimization tricks.
First, are you sure you need to add all attributes to select?
You can replace the
addAttributeToSelect('*')
with
addAttributeToSelect(array('attribute_code', 'attribute_code'))
Next, what exactly is that
$this->get_cur_page()
doing? Rest of code shouldn't make disastrous performance issues.
PS
This should have been a comment, but I'm unable to comment yet.

Add total price in checkout success page

I need to know, if any solution how to add in magento success.phtml order's total price?
Because I want after placed order, customer get all info how to make payment including what price need to pay, because customer don't remember in last step what is total price.
You could use something like this in your success.phtml:
$sOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$oOrder = Mage::getModel('sales/order')->load($sOrderId);
echo $oOrder->getGrandTotal();
in success.phtml template you can use
$order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
$total = $order->getGrandTotal();
proper way is to extend the Mage_Checkout_Block_Onepage_Success and add your own method for loading the order again (As in this page quote is inactive already) as it is not nice to load such stuff in templates

Magento: increment_id empty after first order

I need the increment id from an order to create a folder on my server for moving some order specific files there, after a customer has successfully ordered something. This is what I've got so far:
$chkoutSess = Mage::getSingleton('checkout/session');
$lastOrderId = $chkoutSess->getLastOrderId();
$order = Mage::getModel('sales/order');
$order->load($lastOrderId);
$incid = $order->getData("increment_id");
I guess this can be done much easier but it works for me. The problem I have with this is that the $order is totally empty when I order something "the first time". After this it always works. So I think I can only get this data out of a session object when there's an actual session...this of course is senseless because there should always be a "lastOrderId" when a customer orders something. Is there any other way to simply get the last order and it's incremented out of the database? I tried several things but the objects where always empty..
The function I use is executed right after the user successfully hits the order submit button (event observer "sales_order_place_after"). Anyone can help me with this?
Try $chkoutSess->getRealOrderId(); and not $chkoutSess->getLastOrderId();

Magento - tracking who to give back to

We have a Magento multi-site that give a percentage back to a non-profit, and what we would like to do is to allow customers to select which non-profit or a group within that non-profit to receive the percentage.
Trying to keep it simple we thought allowing customers to enter a discount code named something like "GIVE BACK to {non-profit name}" (but no actual dollar amount subtracted from the purchase or maybe just a penny, I don't think you can have a 0 to a discount code), then internally we know to give the percentage back to that non-profit.
It just seems a little bit of an odd way of doing this, it would be better to have a drop down of the none-profits at the end, but we are not sure how to create that.
Does anyone have any suggestions, on an easy way of doing this?
Thanks in advance!
You could add an attribute to the quote/order model and then populate that with an appropriate value, populated at the cart level? It's the kind of think we've done for affiliate modules we've built in the past.
You'll need a custom controller to grab the value when the customer moves from the cart to the checkout, which means you'll also need to make the Cart -> Checkout step a form submission instead of a straight link.
Then, at the end of the month, you'll need to run a report on the collection with something along the lines of:
// I've added a * in the SELECT because I'm not sure of the attribute names off the top of my head :)
$collection = Mage::getModel('sales/order')->getCollection()
->addAttributeToSelect('*')
// Make sure the orders are in the correct date range
->addAttributeToFilter(...)
// Make sure the orders are in a valid state, e.g. processing, pending, complete, etc..
->addAttributeToFilter(...)
$donation_total = array();
foreach ($collection as $order) {
// You'll have to investigate the attribute values for these
$charity = $order->getData('charity_attribute_code');
$order_total = $order->getData('order_total_attribute_code');
if (!isset($donation_total[$charity])) {
$donation_total[$charity] = 0;
}
$donation_total[$charity] += $order_total;
}
print_r($donation_total);
You could make this more efficient with proper SUM()ing in the query.

Resources