2

i am new to Magento.i am trying to achieve a simple way to apply custom margin at cost and then get its sale price.. i want to put only cost then some function to calculate 20% VAT (TAX) and custom attribute... and set margin 15%

example;

i have Three fields..when adding or editing product

 Cost = 100 
 Price = 0 
 margin_percentage: 15 (means 15%) 

after user add 100 and margin percentage 15, system first calculate 20% vat then on resulting amount add 15% margin so i this case

cost + vat + margin = 138 after saving

 Cost = 100 
 Price = 138 
 margin_percentage: 15

i have write a module and trying to achieve this using Event Observer - catalog_product_save_before and catalog_product_save_after here is a function ...if this code works i may apply formula’s ..

  class SmashingMagazine_LogProductUpdate_Model_Observer
  {

  public function logUpdate(Varien_Event_Observer $observer)
  {
    $product = $observer->getEvent()->getProduct();
    $cost = $product->getCost();

    $_product = Mage::getModel('catalog/product')->load($product->getId());
    $_product->setPrice($cost); // or an other price field
    $_product->save();

    Mage::log(
        "cost {$cost} Price {$price} updated",
        null, 
        'product-updates.log'
    );
  }
  }

when i create or edit product at catalog... above code not working and product price page like some infinite loop.. at end timeout and values not saved. please suggest me some hints and i am very confused how I accomplish this task. i have no experience of Magento research a lot for the last 5 days. i am following this tutorial to make module http://coding.smashingmagazine.com/2012/03/01/basics-creating-magento-module/

0

The loop is created because you do a save for the product. If you want to change attributes than you use the before save observer, since the object us saved after your xhabge you do not need to save it yourself.

  • Thanks Vladimir.. ok i remove save and ues before save observer.. its working but price field is empty. – lordman Jan 19 '14 at 13:09
  • You can use the $product, you do not need ro load an extra model. Changes made to the object from the observer are saved. – Vladimir Kerkhoff Jan 19 '14 at 13:42
  • oh man you really save me :) thanks a lot Vladimir Kerkhoff – lordman Jan 19 '14 at 14:13
  • i one small thing i have add custom arrtibute field "set_store_margin" and want to add 15% margin how i get this field value like i explain above (like i get getCost();) – lordman Jan 19 '14 at 14:20

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.