rsly
rsly

Reputation: 25

Magento 1.9 - How to get final product price on add to cart?

Final Update

Apparently I just needed the pressure of posting the questions on here. On the off chance anyone else comes along with this same problem, this seems to work and covers the case of catalog price rules being used. Specifically for the official Facebook Ads Magento plugin for Magento 1.9, you have to add this code to the app/code/community/Facebook/AdsExtension/Block/AddToCart.php file

public function getValue() {
    $latestItem = Mage::getSingleton('checkout/session')
   ->getQuote()
   ->getItemsCollection()
   ->getLastItem()
   ->getProduct();
    return $latestItem->getFinalPrice();
  }

Original Question

I know it's EOL, but I'm stuck running Magento 1.9 for a few more months. I'm trying to fix Facebook's official Pixel module because it's missing the product value on the add to cart event. The method to return a value is missing completely. I'm struggling to figure out how to get the price of the individual product when it gets added to the cart though since it seems to fire after leaving the product page.

public function getValue() {
    $collection = Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection();
    $collection->getSelect()->order('created_at DESC');
    $latestItem = $collection->getLastItem();
    $product = $latestItem->getProduct();
    return $product->getPrice();
  }

This is what I came up with. I'm pulling the last item added to the cart and taking the price from that, but it returns the full product price ignoring any catalog price rules that are being applied. I also feel like I'm overcomplicating the process to get the value too with all of these calls and pulling in the entire set of quote items to then sort it and extract one single product from it.

Anyone have suggestions on the best way to get the price of the item that was just added to the cart? There seem to be plenty of questions on how to update the price for an item during the add to cart process, but I haven't had luck finding finding someone asking this same questions with an answer that works. TIA

Update: I found this, which seems to be a cleaner way to return the product, but the price is still full retail and doesn't reflect any catalog price rules that may discount the price. Credit: Magento get last product added to cart

  public function getValue() {
    $latestItem = Mage::getSingleton('checkout/session')
   ->getQuote()
   ->getItemsCollection()
   ->getLastItem()
   ->getProduct();
    return $latestItem->getPrice();
  }

Upvotes: 0

Views: 912

Answers (1)

rsly
rsly

Reputation: 25

Answered and added this into the original question, but this seems to work

public function getValue() {
    $latestItem = Mage::getSingleton('checkout/session')
   ->getQuote()
   ->getItemsCollection()
   ->getLastItem()
   ->getProduct();
    return $latestItem->getFinalPrice();
  }

Upvotes: 0

Related Questions