Question

This question has sort-of been asked over here: https://stackoverflow.com/questions/8254682/magento-getting-discounted-price

I need to get the price of a product AFTER Shopping Cart Price Rules have been applied. I need the price on the checkout confirmation (success) page.

For example, product A costs 9.90. I apply a discount code discount of 50%. The price is now 4.95. I pay and checkout. On the order confirmation page, I need to retrieve the price.

I've tried:

Mage::getModel('catalogrule/rule')->calcProductPriceRule($prod, $prod->getPrice());

And...

Mage::getModel('catalogrule/rule')->calcProductPriceRule($prod
    ->setStoreId($this->getCurrentStoreId())
    ->setCustomerGroupId($this->getCurrentCustomerGroupId()),
    $prod->getPrice()
);

But it always returns null. Can I achieve this without rewriting the whole logic of the rules?

I'm developing a local module for this and using Magento 1.7.0.2 CE. Thank you.

~ edit ~

The meat and potatoes of my code looks as follows:

public function getLatestOrder()
{
    $order = Mage::getModel('sales/order')->getCollection()
        ->addAttributeToSelect('*')
        ->setOrder('entity_id', Varien_Data_Collection::SORT_ORDER_DESC)
        ->getFirstItem();
    return $order;
}

// iterate as follows:

$items = $this->getLatestOrder()->getAllVisibleItems();
foreach ($items as $item) {
    echo $item->getPrice();
}

The value for $item->getPrice(); is the full price for each item, even though a 99.9% discount voucher was applied.

Was it helpful?

Solution

If I understood correctly, you need the price at which the product has been sold.
You can get that price directly from the order item.
On the success page you have access to the last order it with:

$orderId = Mage::getSingleton('checkout/session')->getLastOrderId()

Then loop through the order items until you find the product you need:
Let's say the product you are looking for is $productId

$order = Mage::getModel('sales/order')->load($orderId);
$price = null;
foreach ($order->getAllItems() as $item){
   if ($item->getProductId() == $productId){
        $price = $item->getPrice() - $item->getDiscountAmount();
        break;
   }
}
if (!is_null($price)){
    //do something with $price
}

There is no point on applying a rule to the main product. You can get wrong results. For example some rules may be applied only on a certain combination of products are in cart, or when a specific shipping address is set. Also one rule cannot be valid anymore (customer or total capping can be reached).

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top