How to get grand total without shipping costs from a Magento order model? - magento

I have a Magento order model that I create like this:
$order = Mage::getModel('sales/order')->load($orderId);
Now I want to get the order grand total including the taxes etc., but without the shipping costs. I can retrieve the grand total with $order->getGrandTotal(), but how can I exclude the shipping costs?
Thank you in advance!

$amount = $order->getGrandTotal() - $order->getShippingAmount();
Try not to over-think it. ;-)

Here is another quick method to get grand total incuding taxes and without shipping fee.
if you are dealing with the cart:
$quote = Mage::getModel('checkout/session')->getQuote();
$cartGrossTotal = 0;
foreach ($quote->getAllItems() as $item) {
$cartGrossTotal += $item->getPriceInclTax()*$item->getQty();
}
if you are dealing with an order:
$orderGrossTotal = 0;
foreach ($order->getAllItems() as $item) {
$orderGrossTotal += $item->getPriceInclTax()*$item->getQty();
}

I think
base_subtotal_incl_tax can be, so
$order->getBaseSubtotalInclTax()

There is one way more easy:
$orderValue = $order->getSubtotal();
Return the subtotal from the order, without shipping method;
Try it.

Related

Magento Shopping Cart Rule - Is this combination possible

Hoping some one can help me with a Magento Rule - is this rule even possible
I have numerous products of numerous sizes all part of the same category.
Each product regardless of size costs £3.98 - If a buyer buys 3 products of the same category regardless of the product or size they get it for £9.99. If they buy 4 products, they get 3 of them for 9.99 but pay full price for the 4th...Every group of 3 is £9.99
I have a rule created that seems to work perfect if a Customer buys 3 / 6 / 9 items of the same product and same size...However if they mix and match it doesn't work (though they are the same category)
The rule is:
IF ALL of these conditions are TRUE:
If total quantity equals or greater than 3 for a subselection of items in cart matching ALL of these conditions:
Category is 4
I have also set the Discount Qty Step to be 3
* UPDATE *
Thanks for your reply - I have tried to implement what you suggest and have got so far as to where I get the category id of the added products. I'm unsure how to set the price the previous products so it will be an automatically discounted price
$quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems();
$itemPrice = "3.33";
foreach ($cartItems as $items) {
$product = $items->getProduct();
$prodCats = $product->getCategoryIds();
if (in_array('4', $prodCats)) {
$itemQty = $items->getQty();
}
$totalItems += $itemQty;
}
So what I want to do is apply a discount for multiple of 3's for any product that has a category_id of 4...The price will be 3.33 instead of the normal 3.99
You need event - observer approach for this, that will give you the flexibility you need. You can build an observer that catches the add-to-cart event sales_quote_add_item and put your logic there.
Following code within your observer function will point you in the right direction:
// Get products in cart:
$quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems();
foreach ($cartItems as $item) {
$itemSku = $item->getSku();
$itemQty = $item->getQty();
}
// Added product:
$item = $observer->getEvent()->getQuoteItem();
$itemQty = $item->getQty();
$itemSku = $item->getSku();
// Change price of added product:
$item->setOriginalCustomPrice($newPrice);
Good luck!

Magento: count products using stock quantity

I'm new in magento.
I'm wondering how I can count all products using stock quantity. For example, I have
category 1
product one - stock 10
product two - stock 5
category 2
product three - stock 10
The result of the sum of all products should be 25
Actually, I'm using
<?php
$prods = Mage::getModel('catalog/product')->getCollection();
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($prods);
$count = number_format(count($prods));
echo $count;
?>
but this counts the products without stock quantity.
Thanks for your help.
Untested but this should get you what you need…
$stockItemCollection = Mage::getModel('cataloginventory/stock_item')
->getCollection();
$stockTotal = array_sum($stockItemCollection->getColumnValues('qty'));
This should work, too. The reports collection joins together all the quote_items. But I'm not sure wether any order status is considered
$collection = Mage::getResourceModel('reports/product_sold_collection');
$collection->addOrderedQty();
// EDIT reading the question is all
$sum = 0;
foreach($collection as $product) {
$sum += $product->getOrderedQty();
}
echo $sum;

In Prestashop, how to get category of last product added?

I don't have that much experience with Prestashop, php, and Smarty.
How do I get the category of lastProductAdded?
I am trying to make the "continue shopping" button redirect to the category of the last product added.
« {l s='Continue shopping'}
The following code doesn't seem to work, giving category id of 0 for some reason. (I have no idea whether it makes sense either)
Any help would be much appreciated. Thank you!
(The variable lastProductAdded and function getCategoryLink are already defined in-built)
For Prestashop 1.4.x you need to modificate Cart::getLastProduct() with this code:
public function getLastProduct()
{
$sql = '
SELECT cp.`id_product`, cp.`id_product_attribute`, p.`id_category_default`
FROM `'._DB_PREFIX_.'cart_product` cp
JOIN `'._DB_PREFIX_.'product` p ON (cp.`id_product` = p.`id_product`)
WHERE `id_cart` = '.(int)($this->id).'
ORDER BY cp.`date_add` DESC';
$result = Db::getInstance()->getRow($sql);
if ($result AND isset($result['id_product']) AND $result['id_product'])
return $result;
return false;
}
Regards
you need to use $lastProductAdded.id_category_default instead of $lastProductAdded.category->id
Regards

Magento tax rounding issue

I got strange rounding issue for VAT in Magento. My product set up is
* product price incl 20% VAT is 183.59
I added 30 items into basket and it would cost 30 * 183.59 = 5507.70. I can see this value in basket/checkout so that's fine. If I have just 1 item in basket it's ok.
Also the final VAT would be 5507.70 * 20 / 120 = 917.95, but I'm getting 918.00
Do you have any idea how to fix this or where would I take a look? Thanks in advance.
In the end I found the solution. I changed System > VAT > Tax Calculation Method Based On from Unit price to Row Total and it works, more details here
The issue which I found is in core/store model. I had to rewrite roundPrice method and change rounding precision there.
public function roundPrice($price)
{
return round($price, 4);
}
Info
Round price in Magento based on previous rounding operation delta.
app/code/core/Mage/Tax/Model/Sales/Total/Quote/Tax.php:1392
app/code/core/Mage/Tax/Model/Sales/Total/Quote/Subtotal.php:719
protected function _deltaRound($price, $rate, $direction, $type = 'regular')
{
if ($price) {
$rate = (string)$rate;
$type = $type . $direction;
// initialize the delta to a small number to avoid non-deterministic behavior with rounding of 0.5
$delta = isset($this->_roundingDeltas[$type][$rate]) ? $this->_roundingDeltas[$type][$rate] : 0.000001;
$price += $delta;
$this->_roundingDeltas[$type][$rate] = $price - $this->_calculator->round($price);
$price = $this->_calculator->round($price);
}
return $price;
}
Sometimes this can cause an error due to the high delta calculation error ($this->_calculator->round($price)). For example, for this reason, some prices can vary in the range of ±1 cent.
Solution
To avoid this, you need to improve the accuracy of the delta calculation.
Change
$this->_roundingDeltas[$type][$rate] = $price - $this->_calculator->round($price);
to
$this->_roundingDeltas[$type][$rate] = $price - round($price, 4);
Changes need to be made in both files:
app/code/core/Mage/Tax/Model/Sales/Total/Quote/Tax.php:1392
app/code/core/Mage/Tax/Model/Sales/Total/Quote/Subtotal.php:719
Don't modify or hack core files! Do a rewrite!
The solution was tested on different versions of Magento 1.9.x, but maybe this will work in earlier versions.
P.S.
Change roundPrice function, as shown below, can solve the rounding error problem, but it can cause others (for example, some platforms require rounding up to 2 decimal places).
app/code/core/Mage/Core/Model/Store.php:995
public function roundPrice($price)
{
return round($price, 4);
}

Magento Filter By Relative Value

I would like to filter a collection by relative value per that row. For example,
SELECT * FROM table WHERE column_1 > column_2
The only thing I know how to do in Magento would be
$q = Mage::getModel('table')->getCollection()
->addAttributeToFilter('column_1', array('gt' => $some_number));
or something of that sort. I can only give it a value to compare against, not a column name. I also looked at the Zend_Db_Select method at the where clause but didn't find anything that would help. Do I actually have to go all the way down to a direct SQL query (something which is, of course, avoided at all costs)? (I'm running Magento 1.3.2.4)
Thank you.
Try something like
$q = Mage::getModel('table')->getCollection()
->addAttributeToSelect('column_1')
->addAttributeToSelect('column_2')
->addAttributeToFilter('column_1', array('gt' => Zend_Db_Expr('`column_2`')));
OK, this is probably not the ideal solution, but it's one way of getting what you need.
This is how I got a collection of products that were on sale, where a products final price is lower than its price.
I first made a new empty data collection. Then defined the collection I was going to loop through filtering what I could first. After that I looped through that collection and any products that matched my requirements got added to the empty collection.
$this->_itemCollection = new Varien_Data_Collection();
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
$category = Mage::getModel('catalog/category')->load($this->getCategoryId());
$collection = $this->_addProductAttributesAndPrices($collection)
->addStoreFilter()
->addCategoryFilter($category)
->addAttributeToSort('position', 'asc');
$i=0;
foreach($collection as $product){
if($product->getFinalPrice() > $product->getPrice() && !$this->_itemCollection->getItemById($product->getId())){
$this->_itemCollection->addItem($product);
$i++;
}
if($i==$this->getProductsCount()){
break;
}
}
$this->setProductCollection($this->_itemCollection);

Resources