3

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.

  • The adjusted pricing is saved on the quote item / order item level. Have you considered looking there? Alternatively it would be good to include some extra information why you need to look it up via the product. – Kristof at Fooman Jul 22 '13 at 14:38
  • Thx Fooman, yes I have. I need this price to supply to an affiliate network. – Jongosi Jul 22 '13 at 15:02
  • @Fooman, sry, I see what you mean now! No, I don't necessarily need it via the product, I went that route because the quote level wasn't returning what was expected (see my edit). Thx. – Jongosi Jul 22 '13 at 15:04
3

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).

  • Thx Marius, you sir are correct; I need the final sale price of each product. Please see my edit. – Jongosi Jul 22 '13 at 15:05
  • I've updated the the answer. You can calculate the price subtracting the discount amount $item->getDiscountAmount(); – Marius Jul 22 '13 at 16:15
  • Thx Marius! I've marked that as the answer - except it returns the float value even if discount is a %. So 99.9% is returned as 99.90. – Jongosi Jul 22 '13 at 16:21
  • Does your product have the price of 100? :) – Marius Jul 22 '13 at 16:24
  • Thank you Marius. I always forget this tip: var_dump($item->getData()); It works, thx! – Jongosi Jul 23 '13 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.