Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $installer = $this;
- try{
- $productTypes = array(
- Mage_Catalog_Model_Product_Type::TYPE_SIMPLE,
- Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE
- );
- $productTypes = join(',', $productTypes);
- $installer->startSetup();
- $installer->addAttribute('catalog_product', 'discount', array(
- 'group' => 'Prices',
- 'type' => Varien_Db_Ddl_Table::TYPE_VARCHAR,
- 'backend' => '',
- 'frontend' => '',
- 'label' => 'Discount',
- 'input' => 'price',
- 'class' => '',
- 'source' => '',
- 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
- 'visible' => true,
- 'required' => false,
- 'user_defined' => true,
- 'default' => '',
- 'searchable' => false,
- 'filterable' => true,
- 'comparable' => false,
- //'visible_on_front' => true,
- 'unique' => false,
- 'apply_to' => $productTypes,
- 'is_configurable' => false
- ));
- Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
- $products = Mage::getModel("catalog/product")->getCollection()->addAttributeToSelect('*');
- foreach($products as $_product) {
- $product = Mage::getModel('catalog/product')->load($_product->getId());
- if($product->getFinalPrice() < $product->getPrice()){
- $diffAmt = $product->getPrice() - $product->getFinalPrice();
- $discountPer = ($diffAmt/ $product->getPrice()) * 100;
- $product->setDiscount($discountPer);
- $product->save();
- }
- }
- $installer->endSetup();
- }
- catch(Exception $ex){
- echo $ex->getMessage();
- }
- $products = Mage::getModel("catalog/product")->getCollection()
- ->addAttributeToSelect('*')
- ->addMinimalPrice()
- ->addFinalPrice()
- ->addTaxPercents();
- foreach ($products as $product) {
- if($product->getFinalPrice() < $product->getPrice()){
- $diffAmt = $product->getPrice() - $product->getFinalPrice();
- $discountPer = ($diffAmt/ $product->getPrice()) * 100;
- Mage::getSingleton('catalog/product_action')->updateAttributes(
- array($product->getId()), //products to update
- array('discount' => $discountPer), //attributes to update
- 0 //store to update. 0 means global values
- );
- }
- }
- $attributesData = array("price" => $data['price']);
- Mage::getSingleton('catalog/product_action')
- ->updateAttributes(array($data['product_id']), $attributesData, $storeId);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement