Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. $installer = $this;
  2. try{
  3. $productTypes = array(
  4. Mage_Catalog_Model_Product_Type::TYPE_SIMPLE,
  5. Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE
  6. );
  7. $productTypes = join(',', $productTypes);
  8.  
  9. $installer->startSetup();
  10. $installer->addAttribute('catalog_product', 'discount', array(
  11. 'group' => 'Prices',
  12. 'type' => Varien_Db_Ddl_Table::TYPE_VARCHAR,
  13. 'backend' => '',
  14. 'frontend' => '',
  15. 'label' => 'Discount',
  16. 'input' => 'price',
  17. 'class' => '',
  18. 'source' => '',
  19. 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
  20. 'visible' => true,
  21. 'required' => false,
  22. 'user_defined' => true,
  23. 'default' => '',
  24. 'searchable' => false,
  25. 'filterable' => true,
  26. 'comparable' => false,
  27. //'visible_on_front' => true,
  28. 'unique' => false,
  29. 'apply_to' => $productTypes,
  30. 'is_configurable' => false
  31. ));
  32.  
  33. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  34. $products = Mage::getModel("catalog/product")->getCollection()->addAttributeToSelect('*');
  35.  
  36. foreach($products as $_product) {
  37. $product = Mage::getModel('catalog/product')->load($_product->getId());
  38.  
  39. if($product->getFinalPrice() < $product->getPrice()){
  40.  
  41. $diffAmt = $product->getPrice() - $product->getFinalPrice();
  42. $discountPer = ($diffAmt/ $product->getPrice()) * 100;
  43. $product->setDiscount($discountPer);
  44. $product->save();
  45. }
  46. }
  47.  
  48. $installer->endSetup();
  49. }
  50. catch(Exception $ex){
  51. echo $ex->getMessage();
  52. }
  53.  
  54. $products = Mage::getModel("catalog/product")->getCollection()
  55. ->addAttributeToSelect('*')
  56. ->addMinimalPrice()
  57. ->addFinalPrice()
  58. ->addTaxPercents();
  59. foreach ($products as $product) {
  60. if($product->getFinalPrice() < $product->getPrice()){
  61. $diffAmt = $product->getPrice() - $product->getFinalPrice();
  62. $discountPer = ($diffAmt/ $product->getPrice()) * 100;
  63. Mage::getSingleton('catalog/product_action')->updateAttributes(
  64. array($product->getId()), //products to update
  65. array('discount' => $discountPer), //attributes to update
  66. 0 //store to update. 0 means global values
  67. );
  68. }
  69. }
  70.  
  71. $attributesData = array("price" => $data['price']);
  72.  
  73. Mage::getSingleton('catalog/product_action')
  74. ->updateAttributes(array($data['product_id']), $attributesData, $storeId);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement