dqef7931 2014-01-18 10:58
浏览 24
已采纳

如何在成本上应用自定义属性值

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 attributes margin which suppose I set 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 get this case

cost + vat + margin = 138 after saving

Cost = 100 
Price = 138 
margin_percentage: 15

I have write a module and tried 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, but I try to update db tables nothing happened. I don't know what is going wrong. I am trying to update this table catalog_product_flat_1

class SmashingMagazine_LogProductUpdate_Model_Observer
{
/**
 * Magento passes a Varien_Event_Observer object as
 * the first parameter of dispatched events.
 */
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} ",
        null, 
        'product-updates.log'
    );
}

}

I follow this tutorial for making this module http://coding.smashingmagazine.com/2012/03/01/basics-creating-magento-module/ ... now what is wrong with the above code? Also give me execution time out error. My server current execution time is 300000

Hope someone guide me to a right direction

Best Regards

  • 写回答

2条回答 默认 最新

  • duanbu1421 2014-01-18 11:16
    关注

    Why don't you write the price in the Magento catalog/price mobel object?

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

    instead of:

    $connection = Mage::getSingleton('core/resource')->getConnection('core_write');
    $connection->beginTransaction();
    $fields = array();
    $fields['price'] = $cost;
    $where = $connection->quoteInto('entity_id =?', $id);
    $connection->update('catalog_product_flat_1', $fields, $where);
    $connection->commit();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集