2

I created 2 new attribute and I want to use them to calculate price. I use this code but I don't know how to get the value of these attribute. Any ideas on the best way to do this? Thanks so much!

<?php
class GoIvvy_PriceFormula_Model_Observer
{
   public function calculateFinalPrice($observer)
   {
        $_product = $observer->getEvent()->getProduct();
        // $mount = ???
        // $per = ???
        $finalPrice = (100+$mount)*$per;
        $_product->setFinalPrice($finalPrice);
        return $this;
   }
   public function collectionFinalPrice($observer)
   {
       $collection = $observer->getEvent()->getCollection();
       foreach($collection as $_product){
        // $mount = ???
        // $per = ???
        $finalPrice = (100+$mount)*$per;
        $_product->setMinimalPrice($finalPrice)
                  ->setPrice($finalPrice)
                  ->setFinalPrice($finalPrice);
       }
     return $this; 
   }
}
  • when you want to calculate this price on product save or product cart – Amit Bera Feb 9 '15 at 10:45
  • I want to calculate this price on product save and display it on homepage – tuanptit Feb 9 '15 at 10:50
1

you can do this by magento event observer.

Try an fire an observer on catalog_product_save_before/catalog_product__save_commit_after event.

$_product = $observer->getEvent()->getProduct();
 $mount = $_product->getData('yourattribute');
 $per = $_product->getData('perAttribute')
$finalPrice = (100+$mount)*$per;
$_product->setFinalPrice($finalPrice);
  • Thank you so much. It works when I click on product page, but the price on homepage didn't change. – tuanptit Feb 9 '15 at 11:29

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.