Ejemplo n.º 1
0
 public function createAttribute($object)
 {
     $attributeId = Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product', $object['attribute_code']);
     if ($attributeId) {
         Mage::getModel('eav/entity_attribute')->load($attributeId)->delete();
     }
     $model = Mage::getModel('catalog/resource_eav_attribute');
     $data = array('attribute_code' => $object['attribute_code'], 'attribute_model' => $object['attribute_model'], 'backend_model' => $object['backend_model'], 'backend_type' => $object['backend_type'], 'backend_table' => $object['backend_table'], 'frontend_model' => $object['frontend_model'], 'frontend_input' => $object['frontend_input'], 'frontend_label' => $object['frontend_label'], 'frontend_class' => $object['frontend_class'], 'source_model' => $object['source_model'], 'is_required' => $object['is_required'], 'is_user_defined' => $object['is_user_defined'], 'default_value' => $object['default_value'], 'is_unique' => $object['is_unique'], 'note' => $object['note'], 'frontend_input_renderer' => $object['frontend_input_renderer'], 'is_global' => $object['is_global'], 'is_visible' => $object['is_visible'], 'is_searchable' => $object['is_searchable'], 'is_filterable' => $object['is_filterable'], 'is_comparable' => $object['is_comparable'], 'is_visible_on_front' => $object['is_visible_on_front'], 'is_html_allowed_on_front' => $object['is_html_allowed_on_front'], 'is_used_for_price_rules' => $object['is_used_for_price_rules'], 'is_filterable_in_search' => $object['is_filterable_in_search'], 'used_in_product_listing' => $object['used_in_product_listing'], 'used_for_sort_by' => $object['used_for_sort_by'], 'is_configurable' => $object['is_configurable'], 'apply_to' => $object['apply_to'], 'is_visible_in_advanced_search' => $object['is_visible_in_advanced_search'], 'position' => $object['position'], 'is_wysiwyg_enabled' => $object['is_wysiwyg_enabled'], 'is_used_for_promo_rules' => $object['is_used_for_promo_rules']);
     $model->addData($data);
     $model->setEntityTypeId($this->typeId);
     try {
         $model->save();
         echo $model->getAttributeCode() . " ..........................ok. ID: " . $model->getId() . "\n";
         $model->setSourceModel($object['source_model']);
         // TODO: check why need it, and how fix it for right set source_model
         $model->save();
     } catch (Exception $ex) {
         echo "Error: " . $ex->getMessage() . "\n";
     }
     //add attribute options
     if (empty($object['options'])) {
         echo "Model " . $model->getAttributeCode() . " saved ..........................ok. \n";
         return;
     }
     $option['attribute_id'] = $model->getId();
     foreach ($object['options'] as $opt) {
         $option['value']['option'][0] = $opt;
         //mage::d($option); //die;
         $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
         $setup->addAttributeOption($option);
     }
     echo "Model " . $model->getAttributeCode() . " saved. options added ..........................ok. \n";
 }
 public function catalogProductPrepareSave($event)
 {
     $productData = $event->getRequest()->getPost('product');
     if ($productData) {
         foreach (Mage::helper('davidjordan_lazyloadingoption')->getAttributeCodes() as $attributeCode) {
             if (empty($productData[$attributeCode])) {
                 $post = $event->getRequest()->getPost();
                 if (!empty($post['product']["{$attributeCode}_visible_input"])) {
                     $label = $post['product']["{$attributeCode}_visible_input"];
                     $attributeModel = Mage::getModel('catalog/resource_eav_attribute')->loadByCode(4, $attributeCode);
                     $foundVal = array_reduce($attributeModel->getSource()->getAllOptions(false), function ($acc, $val) use($label) {
                         return $acc ?: ($val['label'] == $label ? $val['value'] : $acc);
                     }, "");
                     if ($foundVal) {
                         $productData[$attributeCode] = $foundVal;
                     } else {
                         $setup = new Mage_Eav_Model_Entity_Setup('core_write');
                         $setup->addAttributeOption(array('attribute_id' => $attributeModel->getId(), 'values' => array($label)));
                         $connection = Mage::getSingleton('core/resource')->getConnection('core_write');
                         $optionValueId = $connection->lastInsertId();
                         $optionId = $connection->fetchOne("SELECT option_id FROM " . $setup->getTable('eav/attribute_option_value') . " WHERE value_id = ?", $optionValueId);
                         $productData[$attributeCode] = $optionId;
                     }
                     $event->getProduct()->setData($attributeCode, $productData[$attributeCode]);
                 } elseif ($origData = $event->getProduct()->getOrigData($attributeCode)) {
                     $event->getProduct()->setData($attributeCode, $origData);
                 }
             }
         }
     }
 }
Ejemplo n.º 3
0
 /**
  * Create attribute options
  *
  * @param string $attributeId
  * @param array $attributeOptions
  * @return int
  */
 public function addoptions($attributeId, $attributeOptions)
 {
     $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
     foreach ($attributeOptions as $label) {
         $option = array();
         $option['attribute_id'] = $attributeId;
         $option['value'] = array(array(0 => $label));
         $setup->addAttributeOption($option);
     }
     return true;
 }
Ejemplo n.º 4
0
 public function deletarAtributo($id_opcao)
 {
     $attribute_code = Mage::getStoreConfig('cores/config/codigo_cor');
     $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $attribute_code);
     $options = $attribute->getSource()->getAllOptions();
     $option_id = $id_opcao;
     $options['delete'][$option_id] = true;
     $options['value'][$option_id] = true;
     $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
     $setup->addAttributeOption($options);
 }
Ejemplo n.º 5
0
 *
 * @category    Pan
 * @package     Pan_Updater
 * @copyright   Copyright (c) 2014 August Ash, Inc. (http://www.augustash.com)
 * @author      Josh Johnson (August Ash)
 */
/** @var $installer Mage_Core_Model_Resource_Setup */
$installer = $this;
$installer->startSetup();
/**
 * -----------------------------------
 * Add default attribute options for Gift Card Type (wts_gc_type)
 * -----------------------------------
 */
// Get the eav attribute model
$attrModel = Mage::getModel('catalog/resource_eav_attribute');
// Load the particular attribute by attribute_code
$attrCode = 'wts_gc_type';
$attr = $attrModel->loadByCode('catalog_product', $attrCode);
$attrId = $attr->getAttributeId();
$storeId = 0;
// default admin store
$optionsToAdd = array('email', 'print', 'offline');
// syntax to add attribute options to existing attribute
$option = array('attribute_id' => $attrId, 'value' => array());
foreach ($optionsToAdd as $optionVal) {
    $option['value'][$optionVal] = array($optionVal);
}
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);
$installer->endSetup();
Ejemplo n.º 6
0
<?php

error_reporting(E_ALL);
require_once 'app/Mage.php';
umask(0);
echo "<pre>";
/* not Mage::run(); */
Mage::app();
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$row = 1;
$fields = array();
if (($handle = fopen("sizeofoneunit.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $fields[] = trim($data[1]);
    }
    fclose($handle);
}
print_r(array_unique($fields));
$color = array_values(array_unique($fields));
//print_r($color);exit;
$iProductEntityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();
$aOption = array();
$aOption['attribute_id'] = $installer->getAttributeId($iProductEntityTypeId, 'sizeofoneunit');
for ($iCount = 0; $iCount < sizeof($color); $iCount++) {
    $aOption['value']['option' . $iCount][0] = $color[$iCount];
}
$installer->addAttributeOption($aOption);
$installer->endSetup();
Ejemplo n.º 7
0
 public function createOptions($newOptions)
 {
     $cache = Mage::app()->getCache();
     $installer = new Mage_Eav_Model_Entity_Setup('core_setup');
     $attributeApi = Mage::getModel('catalog/product_attribute_api');
     $helperCatalog = Mage::helper('catalog');
     $installer->startSetup();
     $attributesList = array();
     foreach ($newOptions as $attribute_id => $options) {
         $newOption = array();
         $newOption['attribute_id'] = $attribute_id;
         $attributesList[] = $attribute_id;
         $newOption['value'] = array();
         for ($i = 0; $i < sizeof($options); $i++) {
             $label = $helperCatalog->stripTags($options[$i]);
             $cachedValue = $cache->load($attribute_id . '_' . strtolower($label));
             if ($cachedValue != 1) {
                 $newOption['value']['option' . $i][0] = $label;
                 $cache->save("1", $attribute_id . '_' . strtolower($label), array('LINNLIVE_EXTENSION_ATTRIBUTES'), 300);
             }
         }
         if (sizeof($newOption['value'])) {
             $installer->addAttributeOption($newOption);
         }
     }
     $installer->endSetup();
     Mage::app()->cleanCache(array(Mage_Core_Model_Translate::CACHE_TAG));
     $currentOptions = array();
     $attributeApi = Mage::getModel('catalog/product_attribute_api');
     foreach ($attributesList as $attribute_id) {
         $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $attribute_id);
         foreach ($attribute->getSource()->getAllOptions(true, true) as $option) {
             $label = strtolower($option['label']);
             $optionId = $option['value'];
             if (!isset($currentOptions[$attribute_id][$label])) {
                 $currentOptions[$attribute_id][$label] = $optionId;
             }
             $currentOptionId = $currentOptions[$attribute_id][$label];
             if ($optionId > $currentOptionId) {
                 $attributeApi->removeOption($attribute_id, $optionId);
             }
             if ($currentOptionId > $optionId) {
                 $attributeApi->removeOption($attribute_id, $currentOptionId);
                 $currentOptions[$attribute_id][$label] = $optionId;
             }
         }
     }
     return $currentOptions;
 }
 /**
  * Assigns options to an attribute
  *
  * The attribute should be a select/multiselect
  *
  * @param $attribute
  * @param $options
  */
 private function _maintainAttributeOptions($attribute, $options)
 {
     $currentOptions = $attribute->getSource()->getAllOptions();
     $currentOptionFormat = array();
     foreach ($currentOptions as $option) {
         if ($option['label'] != '') {
             $currentOptionFormat[] = $option['label'];
         }
     }
     $optionsToAdd = array_diff($options, $currentOptionFormat);
     $optionsToRemove = array_diff($currentOptionFormat, $options);
     // Create new attributes
     foreach ($optionsToAdd as $option) {
         // Check if the option already exists
         if (!$attribute->getSource()->getOptionId($option)) {
             $attribute->setData('option', array('value' => array('option' => array($option))));
             $attribute->save();
             $this->log($this->__("Created attribute option %s for %s", $option, $attribute->getAttributeCode()));
         }
     }
     // Remove old attributes
     foreach ($optionsToRemove as $option) {
         $optionId = $attribute->getSource()->getOptionId($option);
         $toDelete['delete'][$optionId] = true;
         $toDelete['value'][$optionId] = true;
         $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
         $setup->addAttributeOption($toDelete);
         $this->log($this->__("Deleted attribute option %s for %s", $option, $attribute->getAttributeCode()));
     }
 }
 /**
  * Enregistre les modifs faite sur le manufacturer
  *
  */
 public function SaveAction()
 {
     //Charge le manufacturer
     $Manufacturer = Mage::getModel('Purchase/Manufacturer')->load($this->getRequest()->getParam('man_id'));
     //Enregistre les modifs
     $Manufacturer->setman_name($this->getRequest()->getParam('man_name'));
     $Manufacturer->setman_contact($this->getRequest()->getParam('man_contact'));
     $Manufacturer->setman_address1($this->getRequest()->getParam('man_address1'));
     $Manufacturer->setman_address2($this->getRequest()->getParam('man_address2'));
     $Manufacturer->setman_zipcode($this->getRequest()->getParam('man_zipcode'));
     $Manufacturer->setman_city($this->getRequest()->getParam('man_city'));
     $Manufacturer->setman_country($this->getRequest()->getParam('man_country'));
     $Manufacturer->setman_tel($this->getRequest()->getParam('man_tel'));
     $Manufacturer->setman_fax($this->getRequest()->getParam('man_fax'));
     $Manufacturer->setman_email($this->getRequest()->getParam('man_email'));
     $Manufacturer->setman_website($this->getRequest()->getParam('man_website'));
     $Manufacturer->setman_comments($this->getRequest()->getParam('man_comments'));
     $Manufacturer->setman_attribute_option_id($this->getRequest()->getParam('man_attribute_option_id'));
     //Si on doit créer le manufacturer magento
     if ($this->getRequest()->getParam('create_magento_manufacturer') == '1') {
         //cree l'option
         $option['attribute_id'] = mage::getModel('Purchase/Constant')->GetProductManufacturerAttributeId();
         $option['value'][0][0] = $Manufacturer->getman_name();
         $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
         $setup->addAttributeOption($option);
         //recupere son id
         $OptionId = null;
         $sql = "\n\t\t\t\tSELECT \n\t\t\t\t\t" . mage::getModel('Purchase/Constant')->getTablePrefix() . "eav_attribute_option_value.option_id id, " . mage::getModel('Purchase/Constant')->getTablePrefix() . "eav_attribute_option_value.value name\n\t\t\t\tfrom \n\t\t\t\t\t" . mage::getModel('Purchase/Constant')->getTablePrefix() . "eav_attribute_option,\n\t\t\t\t\t" . mage::getModel('Purchase/Constant')->getTablePrefix() . "eav_attribute_option_value\n\t\t\t\twhere \n\t\t\t\t\t" . mage::getModel('Purchase/Constant')->getTablePrefix() . "eav_attribute_option.attribute_id = " . mage::getModel('Purchase/Constant')->GetProductManufacturerAttributeId() . "\n\t\t\t\t\tand " . mage::getModel('Purchase/Constant')->getTablePrefix() . "eav_attribute_option.option_id = " . mage::getModel('Purchase/Constant')->getTablePrefix() . "eav_attribute_option_value.option_id \n\t\t\t\t\torder by " . mage::getModel('Purchase/Constant')->getTablePrefix() . "eav_attribute_option_value.value\n\t\t\t\t";
         //execute la requete
         $data = mage::getResourceModel('sales/order_item_collection')->getConnection()->fetchAll($sql);
         for ($i = 0; $i < count($data); $i++) {
             if ($data[$i]['name'] == $Manufacturer->getman_name()) {
                 $OptionId = $data[$i]['id'];
             }
         }
         //l'associe
         $Manufacturer->setman_attribute_option_id($OptionId);
     }
     $Manufacturer->save();
     //confirme
     Mage::getSingleton('adminhtml/session')->addSuccess($this->__('Manufacturer Saved'));
     //Redirige vers la fiche créée
     $this->_redirect('Purchase/Manufacturers/Edit/man_id/' . $Manufacturer->getId());
 }
 public function deletetagAction()
 {
     $user_id = Mage::getSingleton('customer/session')->getId();
     $userid = $user_id ? $user_id : 0;
     $tagname = Mage::app()->getRequest()->getParam('tag');
     $product_id = Mage::app()->getRequest()->getParam('productid');
     $model = Mage::getModel('catalog/product')->load($product_id);
     $attr = $model->getResource()->getAttribute("tag_custom");
     if ($attr->usesSource()) {
         $tagvalue = $attr->getSource()->getOptionId($tagname);
     }
     $attribute_code = 'tag_custom';
     $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $attribute_code);
     $options = $attribute->getSource()->getAllOptions();
     $options['delete'][$tagvalue] = true;
     $options['value'][$tagvalue] = true;
     $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
     $setup->addAttributeOption($options);
     return true;
 }
Ejemplo n.º 11
0
<?php

require_once "lib/Magento.php";
// Deleting attribute options by GUID
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$attribute = Mage::getSingleton("eav/config")->getAttribute("catalog_product", "brand");
//debug($attribute->getSource()->getAllOptions());die;
//Values have to be deleted one at a time
// Eav Setup.php
$updates = array();
foreach ($attribute->getSource()->getAllOptions() as $option) {
    if ($option['value'] != "") {
        $optionsDelete['delete'][$option['value']] = true;
        $optionsDelete['value'][$option['value']] = true;
    }
}
debug($optionsDelete);
$setup->addAttributeOption($optionsDelete);
Ejemplo n.º 12
0
 /**
  * Deletes the given option of the attribute
  *
  * @param   string  $code       Attribute code
  * @param   string|array    $reference      The reference to delete or multiple
  */
 public function removeOptions($code, $references)
 {
     $optionsDel = array();
     $options = Mage::helper('liip/attribute')->getOptions($code);
     foreach ($options as $key => $option) {
         if (in_array($option->getReference(), (array) $references)) {
             $optionsDel['delete'][$option->option_id] = true;
             $optionsDel['value'][$option->option_id] = true;
         }
     }
     $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
     $setup->addAttributeOption($optionsDel);
 }
Ejemplo n.º 13
0
 public function createAttributeOption($attributeId, $option_label)
 {
     $attribute = Mage::getModel('catalog/product')->setStoreId(0)->getResource()->getAttribute($attributeId);
     if (!$attribute->getId()) {
         $this->_fault('attribute_does_not_exist');
     }
     foreach ($attribute->getSource()->getAllOptions(true) as $option) {
         if (strcmp($option_label, $option['label']) == 0) {
             $this->_fault('attribute_option_already_exists');
         }
     }
     $option = array();
     $option['attribute_id'] = $attributeId;
     $option['value']['new_option'][0] = $option_label;
     $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
     $setup->addAttributeOption($option);
     // Since the addAtributeOption does not return a value, we will have to parse out the new option by name and return it's id.
     $attribute->save();
     $collection = Mage::getResourceModel('eav/entity_attribute_option_collection')->addFieldToFilter('attribute_id', $attributeId)->join('attribute_option_value', 'main_table.option_id=attribute_option_value.option_id')->addFieldToFilter('value', $option_label);
     $new_option = $collection->getFirstItem();
     return $new_option->getId();
 }
Ejemplo n.º 14
0
 public function loadAttributes()
 {
     $optionText = file_get_contents('../var/import/attributes.txt');
     $attrArray = unserialize($optionText);
     //mage::d($attrArray);
     //die;
     //$model = Mage::getModel('catalog/resource_eav_attribute')->load(143)->delete(); die;
     foreach ($attrArray as $attr) {
         try {
             $attr->getSource()->getAllOptions(false);
         } catch (Exception $e) {
             echo "yes";
             die;
             continue;
         }
         $model = Mage::getModel('catalog/resource_eav_attribute');
         //mage::ms($model); die;
         // if attribute_code already exists, renew it only.
         if ($attributeId = Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product', $attr->getAttributeCode())) {
             $model = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
             #$model = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId)->delete();
             //delete old attribute options. TODO: apply API for it.
             $query = "DELETE ao FROM eav_attribute_option ao JOIN eav_attribute ea ON ao.attribute_id = ea.attribute_id AND ea.attribute_id ={$attributeId}";
             $this->query($query);
         }
         $data = array('attribute_code' => $attr->getAttributeCode(), 'attribute_model' => $attr->getData('attribute_model'), 'backend_model' => $attr->getData('backend_model'), 'backend_type' => $attr->getData('backend_type'), 'backend_table' => $attr->getData('backend_table'), 'frontend_model' => $attr->getData('frontend_model'), 'frontend_input' => $attr->getData('frontend_input'), 'frontend_label' => $attr->getData('frontend_label'), 'frontend_class' => $attr->getData('frontend_class'), 'source_model' => $attr->getData('source_model'), 'is_required' => $attr->getData('is_required'), 'is_user_defined' => $attr->getData('is_user_defined'), 'default_value' => $attr->getData('default_value'), 'is_unique' => $attr->getData('is_unique'), 'note' => $attr->getData('note'), 'frontend_input_renderer' => $attr->getData('frontend_input_renderer'), 'is_global' => $attr->getData('is_global'), 'is_visible' => $attr->getData('is_visible'), 'is_searchable' => $attr->getData('is_searchable'), 'is_filterable' => $attr->getData('is_filterable'), 'is_comparable' => $attr->getData('is_comparable'), 'is_visible_on_front' => $attr->getData('is_visible_on_front'), 'is_html_allowed_on_front' => $attr->getData('is_html_allowed_on_front'), 'is_used_for_price_rules' => $attr->getData('is_used_for_price_rules'), 'is_filterable_in_search' => $attr->getData('is_filterable_in_search'), 'used_in_product_listing' => $attr->getData('used_in_product_listing'), 'used_for_sort_by' => $attr->getData('used_for_sort_by'), 'is_configurable' => $attr->getData('is_configurable'), 'apply_to' => $attr->getData('apply_to'), 'is_visible_in_advanced_search' => $attr->getData('is_visible_in_advanced_search'), 'position' => $attr->getData('position'), 'is_wysiwyg_enabled' => $attr->getData('is_wysiwyg_enabled'), 'is_used_for_promo_rules' => $attr->getData('is_used_for_promo_rules'));
         if (!$attributeId) {
             $model->addData($data);
             $model->setEntityTypeId(Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId());
         } else {
             $model->setDataChanges($data);
         }
         // set groups and sets
         $connection = Mage::getModel('core/resource')->getConnection('core_read');
         $select = $connection->select()->from(array('oasm' => 'oberig_attribute_set_mapping'), array('attribute_group_name', 'sort_order'))->joinInner(array('eas' => 'eav_attribute_set'), 'oasm.attribute_set_name = eas.attribute_set_name  AND entity_type_id = 4', 'attribute_set_id')->joinInner(array('eag' => 'eav_attribute_group'), 'eag.attribute_set_id = eas.attribute_set_id AND eag.attribute_group_name = oasm.attribute_group_name', 'attribute_group_id')->where('attribute_code=?', $attr->getAttributeCode());
         echo $select->__toString() . "\n";
         $result = $connection->query($select)->fetchAll();
         echo "-------------------------------------------------\n";
         mage::D($result);
         echo "-------------------------------------------------\n";
         foreach ($result as $res) {
             if ($attributeId) {
                 $model = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
             }
             $model->setAttributeSetId($res['attribute_set_id']);
             $model->setAttributeGroupId($res['attribute_group_id']);
             $model->setSortOrder($res['sort_order']);
             if ($attributeId) {
                 try {
                     $model->save();
                 } catch (Exception $ex) {
                     echo "Error: " . $ex->getMessage() . "\n";
                 }
             }
         }
         //end set groups and sets
         //if( $attr->getAttributeCode() == 'lenstint' )
         //{
         try {
             if (!$attributeId) {
                 $model->save();
                 echo $attr->getAttributeCode() . " ..........................ok. ID: " . $model->getId() . "\n";
             }
             $option['attribute_id'] = $model->getId();
             $all_opts = $attr->getSource()->getAllOptions(false);
         } catch (Exception $ex) {
             echo "Error: " . $ex->getMessage() . "\n";
         }
         //add attribute options
         foreach ($all_opts as $opt) {
             $option['value']['option'][0] = $opt['label'];
             mage::d($option);
             //die;
             $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
             $setup->addAttributeOption($option);
         }
         echo "Model " . $model->getAttributeCode() . " saved. options added ..........................ok. \n";
         //$model = Mage::getModel('catalog/resource_eav_attribute')->load($model->getId())->delete(); //die;
         //}
     }
     // set tax class options TODO: API
     $this->query("insert ignore into tax_class select * from {$this->oldDB}.tax_class");
     //die;
 }
Ejemplo n.º 15
0
 /**
  * When saving the product, assign a subscription plan attribute option.
  * This is used with configurable products.
  */
 protected function _beforeSave()
 {
     $allowProductTypes = array();
     foreach (Mage::helper('catalog/product_configuration')->getConfigurableAllowedTypes() as $type) {
         $allowProductTypes[] = $type->getName();
     }
     if (in_array($this->getTypeId(), $allowProductTypes)) {
         $helperAttribute = Mage::getModel('catalog/resource_eav_attribute')->loadByCode('catalog_product', 'subscription_plan');
         if (!$this->isSubscription()) {
             $optionValue = 'None';
         } else {
             $optionValue = $this->getPlanDescription();
         }
         if (!($optionId = $this->getAttributeOptionId($helperAttribute, $optionValue))) {
             $option['attribute_id'] = $helperAttribute->getAttributeId();
             $option['value']['option_name'][0] = $optionValue;
             $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
             $setup->addAttributeOption($option);
             $optionId = $this->getAttributeOptionId($helperAttribute, $optionValue);
         }
         $this->setSubscriptionPlan($optionId);
     }
     parent::_beforeSave();
 }
Ejemplo n.º 16
0
 public function saveAction()
 {
     if ($data = $this->getRequest()->getPost()) {
         if (isset($_FILES['filename']['name']) && $_FILES['filename']['name'] != '') {
             try {
                 /* Starting upload */
                 $uploader = new Varien_File_Uploader('filename');
                 // Any extention would work
                 $uploader->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png'));
                 $uploader->setAllowRenameFiles(false);
                 // Set the file upload mode
                 // false -> get the file directly in the specified folder
                 // true -> get the file in the product like folders
                 //	(file.jpg will go in something like /media/f/i/file.jpg)
                 $uploader->setFilesDispersion(false);
                 // We set media as the upload dir
                 $path = Mage::getBaseDir('media') . DS;
                 $uploader->save($path, $_FILES['filename']['name']);
             } catch (Exception $e) {
             }
             //this way the name is saved in DB
             $data['filename'] = $_FILES['filename']['name'];
         }
         $model = Mage::getModel('vendor/vendor');
         $model->setData($data)->setId($this->getRequest()->getParam('id'));
         try {
             if ($model->getCreatedTime == NULL || $model->getUpdateTime() == NULL) {
                 $model->setCreatedTime(now())->setUpdateTime(now());
             } else {
                 $model->setUpdateTime(now());
             }
             if ($this->getRequest()->getParam('id') == NULL) {
                 $arg_attribute = 'vendor';
                 $arg_value = $data['seller_name'] . ' (' . $data['seller_code'] . ')';
                 $attr_model = Mage::getModel('catalog/resource_eav_attribute');
                 $attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
                 $attr_id = $attr->getAttributeId();
                 $option['attribute_id'] = $attr_id;
                 $option['value'][$arg_value][0] = $arg_value;
                 $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
                 $setup->addAttributeOption($option);
                 $model->save();
                 $resource = Mage::getSingleton('core/resource');
                 $readConnection = $resource->getConnection('core_read');
                 $writeConnection = $resource->getConnection('core_write');
                 $vendortableName = $resource->getTableName('vendor/vendor');
                 $optiontableName = $resource->getTableName('eav/attribute_option');
                 $query = 'SELECT MAX(option_id) as maxoptionid FROM ' . $optiontableName;
                 $maxoptionid = $readConnection->fetchAll($query);
                 $query = 'SELECT MAX(vendor_id) as vendor_id FROM ' . $vendortableName;
                 $VendorId = $readConnection->fetchAll($query);
                 $query = 'UPDATE ' . $vendortableName . ' SET option_id = ' . $maxoptionid[0]['maxoptionid'] . ' WHERE vendor_id = ' . (int) $VendorId[0]['vendor_id'];
                 $writeConnection->query($query);
             } else {
                 $model->save();
             }
             Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('vendor')->__('Item was successfully saved'));
             Mage::getSingleton('adminhtml/session')->setFormData(false);
             if ($this->getRequest()->getParam('back')) {
                 $this->_redirect('*/*/edit', array('id' => $model->getId()));
                 return;
             }
             $this->_redirect('*/*/');
             return;
         } catch (Exception $e) {
             Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
             Mage::getSingleton('adminhtml/session')->setFormData($data);
             $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
             return;
         }
     }
     Mage::getSingleton('adminhtml/session')->addError(Mage::helper('vendor')->__('Unable to find item to save'));
     $this->_redirect('*/*/');
 }