├── app ├── etc │ └── modules │ │ └── Magebuzz_Customoption.xml ├── code │ └── local │ │ └── Magebuzz │ │ └── Customoption │ │ ├── Model │ │ ├── Customoption.php │ │ ├── Mysql4 │ │ │ ├── Customoption │ │ │ │ └── Collection.php │ │ │ └── Customoption.php │ │ └── Status.php │ │ ├── Helper │ │ └── Data.php │ │ ├── Block │ │ ├── Customoption.php │ │ ├── Adminhtml │ │ │ ├── Customoption.php │ │ │ └── Customoption │ │ │ │ ├── Edit │ │ │ │ ├── Form.php │ │ │ │ ├── Tabs.php │ │ │ │ └── Tab │ │ │ │ │ └── Form.php │ │ │ │ ├── Edit.php │ │ │ │ └── Grid.php │ │ └── Catalog │ │ │ └── Product │ │ │ └── View │ │ │ └── Options │ │ │ └── Type │ │ │ └── Select.php │ │ ├── sql │ │ └── customoption_setup │ │ │ └── mysql4-install-0.1.0.php │ │ ├── etc │ │ ├── adminhtml.xml │ │ └── config.xml │ │ └── controllers │ │ └── Adminhtml │ │ └── CustomoptionController.php └── design │ └── adminhtml │ └── default │ └── default │ └── layout │ └── customoption.xml ├── modman └── README.md /app/etc/modules/Magebuzz_Customoption.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | true 6 | local 7 | 8 | 9 | -------------------------------------------------------------------------------- /modman: -------------------------------------------------------------------------------- 1 | app/code/local/Magebuzz/Customoption app/code/local/Magebuzz/Customoption 2 | app/design/adminhtml/default/default/layout/customoption.xml app/design/adminhtml/default/default/layout/customoption.xml 3 | app/etc/modules/Magebuzz_Customoption.xml app/etc/modules/Magebuzz_Customoption.xml -------------------------------------------------------------------------------- /app/code/local/Magebuzz/Customoption/Model/Customoption.php: -------------------------------------------------------------------------------- 1 | _init('customoption/customoption'); 9 | } 10 | } -------------------------------------------------------------------------------- /app/code/local/Magebuzz/Customoption/Model/Mysql4/Customoption/Collection.php: -------------------------------------------------------------------------------- 1 | _init('customoption/customoption'); 9 | } 10 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Magebuzz_Customoption 2 | ===================== 3 | © 2014 Magebuzz Store. All Rights Reserved. 4 | http://www.magebuzz.com 5 | 6 | 7 | 8 | This module helps you set Default Value For Product Custom Option In Magento. 9 | 10 | To see previous discussions, please visit: 11 | http://www.magebuzz.com/blog/2012/07/09/select-default-value-for-product-custom-option-in-magento 12 | 13 | 14 | -------------------------------------------------------------------------------- /app/code/local/Magebuzz/Customoption/Model/Status.php: -------------------------------------------------------------------------------- 1 | Mage::helper('customoption')->__('Enabled'), 12 | self::STATUS_DISABLED => Mage::helper('customoption')->__('Disabled') 13 | ); 14 | } 15 | } -------------------------------------------------------------------------------- /app/code/local/Magebuzz/Customoption/Helper/Data.php: -------------------------------------------------------------------------------- 1 | getCollection(); 8 | $collection->addFieldToFilter('option_id', $option_id); 9 | $option = $collection->getData(); 10 | if (empty($option)) { 11 | return FALSE; 12 | } 13 | return $option; 14 | } 15 | } -------------------------------------------------------------------------------- /app/code/local/Magebuzz/Customoption/Block/Customoption.php: -------------------------------------------------------------------------------- 1 | hasData('customoption')) { 13 | $this->setData('customoption', Mage::registry('customoption')); 14 | } 15 | return $this->getData('customoption'); 16 | 17 | } 18 | } -------------------------------------------------------------------------------- /app/code/local/Magebuzz/Customoption/sql/customoption_setup/mysql4-install-0.1.0.php: -------------------------------------------------------------------------------- 1 | startSetup(); 6 | 7 | $installer->run(" 8 | 9 | -- DROP TABLE IF EXISTS {$this->getTable('default_option_value')}; 10 | CREATE TABLE {$this->getTable('default_option_value')} ( 11 | `option_id` INT(11) UNSIGNED NOT NULL, 12 | `option_type_id` INT(11) UNSIGNED , 13 | PRIMARY KEY (`option_id`) 14 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 15 | 16 | "); 17 | 18 | $installer->endSetup(); -------------------------------------------------------------------------------- /app/code/local/Magebuzz/Customoption/Block/Adminhtml/Customoption.php: -------------------------------------------------------------------------------- 1 | _controller = 'adminhtml_customoption'; 8 | $this->_blockGroup = 'customoption'; 9 | $this->_headerText = Mage::helper('customoption')->__('Manage Optioned Products'); 10 | $this->_addButtonLabel = Mage::helper('customoption')->__('Add Item'); 11 | parent::__construct(); 12 | $this->_removeButton('add'); 13 | } 14 | } -------------------------------------------------------------------------------- /app/design/adminhtml/default/default/layout/customoption.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /app/code/local/Magebuzz/Customoption/Block/Adminhtml/Customoption/Edit/Form.php: -------------------------------------------------------------------------------- 1 | 'edit_form', 9 | 'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))), 10 | 'method' => 'post', 11 | 'enctype' => 'multipart/form-data' 12 | ) 13 | ); 14 | 15 | $form->setUseContainer(TRUE); 16 | $this->setForm($form); 17 | return parent::_prepareForm(); 18 | } 19 | } -------------------------------------------------------------------------------- /app/code/local/Magebuzz/Customoption/Model/Mysql4/Customoption.php: -------------------------------------------------------------------------------- 1 | _init('customoption/customoption', 'customoption_id'); 8 | } 9 | 10 | public function getTableName() 11 | { 12 | return $this->getTable('customoption/customoption'); 13 | } 14 | 15 | public function getValue($option_id) 16 | { 17 | $read = $this->_getReadAdapter(); 18 | $select = $read->select(); 19 | $select->from($this->getTable('customoption/customoption'))->where('option_id = ?', $option_id); 20 | $value = $this->_getReadAdapter()->fetchAll($select); 21 | return $value; 22 | } 23 | } -------------------------------------------------------------------------------- /app/code/local/Magebuzz/Customoption/Block/Adminhtml/Customoption/Edit/Tabs.php: -------------------------------------------------------------------------------- 1 | setId('customoption_tabs'); 10 | $this->setDestElementId('edit_form'); 11 | $this->setTitle(Mage::helper('customoption')->__('Product Information')); 12 | } 13 | 14 | protected function _beforeToHtml() 15 | { 16 | $this->addTab('form_section', array( 17 | 'label' => Mage::helper('customoption')->__('Custom Options'), 18 | 'title' => Mage::helper('customoption')->__('Custom Options'), 19 | 'content' => $this->getLayout()->createBlock('customoption/adminhtml_customoption_edit_tab_form')->toHtml(), 20 | )); 21 | 22 | return parent::_beforeToHtml(); 23 | } 24 | } -------------------------------------------------------------------------------- /app/code/local/Magebuzz/Customoption/etc/adminhtml.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Product Options 6 | 89 7 | 8 | 9 | Manage Optioned Products 10 | 0 11 | customoption/adminhtml_customoption 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | Allow Everything 20 | 21 | 22 | 23 | 24 | Customoption Module 25 | 10 26 | 27 | 28 | 29 | 30 | Customoption 31 | 89 32 | 33 | 34 | Manage Product Options 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /app/code/local/Magebuzz/Customoption/Block/Adminhtml/Customoption/Edit.php: -------------------------------------------------------------------------------- 1 | _objectId = 'id'; 10 | $this->_blockGroup = 'customoption'; 11 | $this->_controller = 'adminhtml_customoption'; 12 | 13 | $this->_updateButton('save', 'label', Mage::helper('customoption')->__('Save')); 14 | $this->_updateButton('delete', 'label', Mage::helper('customoption')->__('Delete')); 15 | 16 | $this->_addButton('saveandcontinue', array( 17 | 'label' => Mage::helper('adminhtml')->__('Save And Continue Edit'), 18 | 'onclick' => 'saveAndContinueEdit()', 19 | 'class' => 'save', 20 | ), -100); 21 | 22 | $this->_formScripts[] = " 23 | function toggleEditor() { 24 | if (tinyMCE.getInstanceById('customoption_content') == null) { 25 | tinyMCE.execCommand('mceAddControl', false, 'customoption_content'); 26 | } else { 27 | tinyMCE.execCommand('mceRemoveControl', false, 'customoption_content'); 28 | } 29 | } 30 | 31 | function saveAndContinueEdit(){ 32 | editForm.submit($('edit_form').action+'back/edit/'); 33 | } 34 | "; 35 | } 36 | 37 | public function getHeaderText() 38 | { 39 | if (Mage::registry('customoption_data') && Mage::registry('customoption_data')->getId()) { 40 | return Mage::helper('customoption')->__("%s", $this->htmlEscape(Mage::registry('customoption_data')->getName())); 41 | } else { 42 | return Mage::helper('customoption')->__('Add Item'); 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /app/code/local/Magebuzz/Customoption/etc/config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 0.1.1 6 | 7 | 8 | 9 | 10 | 11 | standard 12 | 13 | Magebuzz_Customoption 14 | customoption 15 | 16 | 17 | 18 | 19 | 20 | 21 | customoption.xml 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | admin 30 | 31 | Magebuzz_Customoption 32 | customoption 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | customoption.xml 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | Magebuzz_Customoption_Model 50 | customoption_mysql4 51 | 52 | 53 | Magebuzz_Customoption_Model_Mysql4 54 | 55 | 56 | default_option_value
57 |
58 |
59 |
60 |
61 | 62 | 63 | 64 | Magebuzz_Customoption 65 | 66 | 67 | core_setup 68 | 69 | 70 | 71 | 72 | core_write 73 | 74 | 75 | 76 | 77 | core_read 78 | 79 | 80 | 81 | 82 | 83 | Magebuzz_Customoption_Block 84 | 85 | 86 | 87 | Magebuzz_Customoption_Block_Catalog_Product_View_Options_Type_Select 88 | 89 | 90 | 91 | 92 | 93 | Magebuzz_Customoption_Helper 94 | 95 | 96 |
97 |
-------------------------------------------------------------------------------- /app/code/local/Magebuzz/Customoption/Block/Catalog/Product/View/Options/Type/Select.php: -------------------------------------------------------------------------------- 1 | getOption(); 10 | $option_data = $_option->getData(); 11 | $default_value = Mage::getModel('customoption/customoption')->getResource()->getValue($option_data['option_id']); 12 | $configValue = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $_option->getId()); 13 | $store = $this->getProduct()->getStore(); 14 | if ($_option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_DROP_DOWN 15 | || $_option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_MULTIPLE 16 | ) { 17 | $require = ($_option->getIsRequire()) ? ' required-entry' : ''; 18 | $extraParams = ''; 19 | $select = $this->getLayout()->createBlock('core/html_select') 20 | ->setData(array( 21 | 'id' => 'select_' . $_option->getId(), 22 | 'class' => $require . ' product-custom-option' 23 | )); 24 | 25 | if ($_option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_DROP_DOWN) { 26 | $select->setName('options[' . $_option->getid() . ']') 27 | ->addOption('', $this->__('-- Please Select --')); 28 | } else { 29 | $select->setName('options[' . $_option->getid() . '][]'); 30 | $select->setClass('multiselect' . $require . ' product-custom-option'); 31 | } 32 | foreach ($_option->getValues() as $_value) { 33 | $priceStr = $this->_formatPrice(array( 34 | 'is_percent' => ($_value->getPriceType() == 'percent') ? TRUE : FALSE, 35 | 'pricing_value' => $_value->getPrice(TRUE) 36 | ), FALSE); 37 | 38 | if (isset($default_value[0]['option_type_id']) && ($_value->getOptionTypeId() == $default_value[0]['option_type_id'])) { 39 | $select->addOption( 40 | $_value->getOptionTypeId(), 41 | $_value->getTitle() . ' ' . $priceStr . '', 42 | array('price' => $this->helper('core')->currencyByStore($_value->getPrice(TRUE), $store, FALSE), 43 | 'selected' => 'selected' 44 | ) 45 | ); 46 | 47 | } else { 48 | $select->addOption( 49 | $_value->getOptionTypeId(), 50 | $_value->getTitle() . ' ' . $priceStr . '', 51 | array('price' => $this->helper('core')->currencyByStore($_value->getPrice(TRUE), $store, FALSE)) 52 | ); 53 | } 54 | } 55 | if ($_option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_MULTIPLE) { 56 | $extraParams = ' multiple="multiple"'; 57 | } 58 | if (!$this->getSkipJsReloadPrice()) { 59 | $extraParams .= ' onchange="opConfig.reloadPrice()"'; 60 | } 61 | $select->setExtraParams($extraParams); 62 | 63 | if ($configValue) { 64 | $select->setValue($configValue); 65 | } 66 | 67 | return $select->getHtml(); 68 | } 69 | 70 | if ($_option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_RADIO 71 | || $_option->getType() == Mage_Catalog_Model_Product_Option::OPTION_TYPE_CHECKBOX 72 | ) { 73 | $selectHtml = ''; 121 | 122 | return $selectHtml; 123 | } 124 | } 125 | 126 | } 127 | -------------------------------------------------------------------------------- /app/code/local/Magebuzz/Customoption/Block/Adminhtml/Customoption/Edit/Tab/Form.php: -------------------------------------------------------------------------------- 1 | setForm($form); 9 | $fieldset = $form->addFieldset('customoption_form', array('legend' => Mage::helper('customoption')->__('Product default option value is showed in frontent'))); 10 | $options = $this->getOptionValues(); 11 | foreach ($options as $k => $option) { 12 | $option_id = $option->getData('option_id'); 13 | $fieldset->addField('value[' . $option_id . ']', 'select', array( 14 | 'label' => $option->getTitle(), 15 | 'name' => 'value[' . $option_id . ']', 16 | 'values' => $this->getValuesArray($option) 17 | )); 18 | } 19 | 20 | if (Mage::getSingleton('adminhtml/session')->getCustomoptionData()) { 21 | $form->setValues(Mage::getSingleton('adminhtml/session')->getCustomoptionData()); 22 | Mage::getSingleton('adminhtml/session')->setCustomoptionData(null); 23 | } elseif (Mage::registry('customoption_data')) { 24 | $form->setValues(Mage::registry('customoption_data')->getData()); 25 | } 26 | return parent::_prepareForm(); 27 | } 28 | 29 | public function getProduct() 30 | { 31 | $id = $this->getRequest()->getParam('id'); 32 | $_product = Mage::getModel('catalog/product')->load($id); 33 | return $_product; 34 | } 35 | 36 | public function getOptionValues() 37 | { 38 | $optionsArr = array_reverse($this->getProduct()->getOptions(), TRUE); 39 | // $optionsArr = $this->getProduct()->getOptions(); 40 | 41 | if (!$this->_values) { 42 | $showPrice = $this->getCanReadPrice(); 43 | $values = array(); 44 | $scope = (int)Mage::app()->getStore()->getConfig(Mage_Core_Model_Store::XML_PATH_PRICE_SCOPE); 45 | foreach ($optionsArr as $option) { 46 | /* @var $option Mage_Catalog_Model_Product_Option */ 47 | 48 | $this->setItemCount($option->getOptionId()); 49 | 50 | $value = array(); 51 | 52 | $value['id'] = $option->getOptionId(); 53 | $value['item_count'] = $this->getItemCount(); 54 | $value['option_id'] = $option->getOptionId(); 55 | $value['title'] = $this->htmlEscape($option->getTitle()); 56 | $value['type'] = $option->getType(); 57 | $value['is_require'] = $option->getIsRequire(); 58 | $value['sort_order'] = $option->getSortOrder(); 59 | $value['can_edit_price'] = $this->getCanEditPrice(); 60 | 61 | if ($this->getProduct()->getStoreId() != '0') { 62 | $value['checkboxScopeTitle'] = $this->getCheckboxScopeHtml($option->getOptionId(), 'title', 63 | is_null($option->getStoreTitle())); 64 | $value['scopeTitleDisabled'] = is_null($option->getStoreTitle()) ? 'disabled' : null; 65 | } 66 | 67 | if ($option->getGroupByType() == Mage_Catalog_Model_Product_Option::OPTION_GROUP_SELECT) { 68 | 69 | // $valuesArr = array_reverse($option->getValues(), true); 70 | 71 | $i = 0; 72 | $itemCount = 0; 73 | foreach ($option->getValues() as $_value) { 74 | /* @var $_value Mage_Catalog_Model_Product_Option_Value */ 75 | $value['optionValues'][$i] = array( 76 | 'item_count' => max($itemCount, $_value->getOptionTypeId()), 77 | 'option_id' => $_value->getOptionId(), 78 | 'option_type_id' => $_value->getOptionTypeId(), 79 | 'title' => $this->htmlEscape($_value->getTitle()), 80 | 'price' => ($showPrice) 81 | ? $this->getPriceValue($_value->getPrice(), $_value->getPriceType()) : '', 82 | 'price_type' => ($showPrice) ? $_value->getPriceType() : 0, 83 | 'sku' => $this->htmlEscape($_value->getSku()), 84 | 'sort_order' => $_value->getSortOrder(), 85 | ); 86 | 87 | if ($this->getProduct()->getStoreId() != '0') { 88 | $value['optionValues'][$i]['checkboxScopeTitle'] = $this->getCheckboxScopeHtml( 89 | $_value->getOptionId(), 'title', is_null($_value->getStoreTitle()), 90 | $_value->getOptionTypeId()); 91 | $value['optionValues'][$i]['scopeTitleDisabled'] = is_null($_value->getStoreTitle()) 92 | ? 'disabled' : null; 93 | if ($scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE) { 94 | $value['optionValues'][$i]['checkboxScopePrice'] = $this->getCheckboxScopeHtml( 95 | $_value->getOptionId(), 'price', is_null($_value->getstorePrice()), 96 | $_value->getOptionTypeId()); 97 | $value['optionValues'][$i]['scopePriceDisabled'] = is_null($_value->getStorePrice()) 98 | ? 'disabled' : null; 99 | } 100 | } 101 | $i++; 102 | } 103 | } else { 104 | $value['price'] = ($showPrice) 105 | ? $this->getPriceValue($option->getPrice(), $option->getPriceType()) : ''; 106 | $value['price_type'] = $option->getPriceType(); 107 | $value['sku'] = $this->htmlEscape($option->getSku()); 108 | $value['max_characters'] = $option->getMaxCharacters(); 109 | $value['file_extension'] = $option->getFileExtension(); 110 | $value['image_size_x'] = $option->getImageSizeX(); 111 | $value['image_size_y'] = $option->getImageSizeY(); 112 | if ($this->getProduct()->getStoreId() != '0' && 113 | $scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE 114 | ) { 115 | $value['checkboxScopePrice'] = $this->getCheckboxScopeHtml($option->getOptionId(), 116 | 'price', is_null($option->getStorePrice())); 117 | $value['scopePriceDisabled'] = is_null($option->getStorePrice()) ? 'disabled' : null; 118 | } 119 | } 120 | $values[] = new Varien_Object($value); 121 | } 122 | $this->_values = $values; 123 | } 124 | return $this->_values; 125 | } 126 | 127 | public function getValuesArray($option) 128 | { 129 | $valuesArr = array(); 130 | $valuesArr[""] = '-- Please Select --'; 131 | $values = $option->getData('optionValues'); 132 | foreach ($values as $value) { 133 | $valuesArr[$value['option_type_id']] = $value['title']; 134 | } 135 | return $valuesArr; 136 | } 137 | } -------------------------------------------------------------------------------- /app/code/local/Magebuzz/Customoption/Block/Adminhtml/Customoption/Grid.php: -------------------------------------------------------------------------------- 1 | setId('customoptionGrid'); 9 | $this->setUseAjax(TRUE); 10 | $this->setDefaultSort('entity_id'); 11 | // $this->setSaveParametersInSession(true); 12 | } 13 | 14 | protected function _prepareCollection() 15 | { 16 | 17 | $store = $this->_getStore(); 18 | $collection = Mage::getModel('catalog/product')->getCollection() 19 | ->addFieldToFilter('has_options', array('neq' => 0)) 20 | ->addFieldToFilter('type_id', 'simple') 21 | ->addAttributeToSelect('sku') 22 | ->addAttributeToSelect('name') 23 | ->addAttributeToSelect('attribute_set_id') 24 | ->addAttributeToSelect('type_id'); 25 | 26 | if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) { 27 | $collection->joinField('qty', 28 | 'cataloginventory/stock_item', 29 | 'qty', 30 | 'product_id=entity_id', 31 | '{{table}}.stock_id=1', 32 | 'left'); 33 | } 34 | if ($store->getId()) { 35 | //$collection->setStoreId($store->getId()); 36 | $adminStore = Mage_Core_Model_App::ADMIN_STORE_ID; 37 | $collection->addStoreFilter($store); 38 | $collection->joinAttribute('name', 'catalog_product/name', 'entity_id', null, 'inner', $adminStore); 39 | $collection->joinAttribute('custom_name', 'catalog_product/name', 'entity_id', null, 'inner', $store->getId()); 40 | $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner', $store->getId()); 41 | $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner', $store->getId()); 42 | $collection->joinAttribute('price', 'catalog_product/price', 'entity_id', null, 'left', $store->getId()); 43 | } else { 44 | $collection->addAttributeToSelect('price'); 45 | $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner'); 46 | $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner'); 47 | } 48 | $this->setCollection($collection); 49 | return parent::_prepareCollection(); 50 | } 51 | 52 | protected function _prepareColumns() 53 | { 54 | $this->addColumn('entity_id', 55 | array( 56 | 'header' => Mage::helper('catalog')->__('ID'), 57 | 'width' => '50px', 58 | 'type' => 'number', 59 | 'index' => 'entity_id', 60 | )); 61 | $this->addColumn('name', 62 | array( 63 | 'header' => Mage::helper('catalog')->__('Name'), 64 | 'index' => 'name', 65 | )); 66 | 67 | $store = $this->_getStore(); 68 | if ($store->getId()) { 69 | $this->addColumn('custom_name', 70 | array( 71 | 'header' => Mage::helper('catalog')->__('Name in %s', $store->getName()), 72 | 'index' => 'custom_name', 73 | )); 74 | } 75 | 76 | $this->addColumn('type', 77 | array( 78 | 'header' => Mage::helper('catalog')->__('Type'), 79 | 'width' => '60px', 80 | 'index' => 'type_id', 81 | 'type' => 'options', 82 | 'options' => Mage::getSingleton('catalog/product_type')->getOptionArray(), 83 | )); 84 | 85 | $sets = Mage::getResourceModel('eav/entity_attribute_set_collection') 86 | ->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId()) 87 | ->load() 88 | ->toOptionHash(); 89 | 90 | $this->addColumn('set_name', 91 | array( 92 | 'header' => Mage::helper('catalog')->__('Attrib. Set Name'), 93 | 'width' => '100px', 94 | 'index' => 'attribute_set_id', 95 | 'type' => 'options', 96 | 'options' => $sets, 97 | )); 98 | 99 | $this->addColumn('sku', 100 | array( 101 | 'header' => Mage::helper('catalog')->__('SKU'), 102 | 'width' => '80px', 103 | 'index' => 'sku', 104 | )); 105 | 106 | $store = $this->_getStore(); 107 | $this->addColumn('price', 108 | array( 109 | 'header' => Mage::helper('catalog')->__('Price'), 110 | 'type' => 'price', 111 | 'currency_code' => $store->getBaseCurrency()->getCode(), 112 | 'index' => 'price', 113 | )); 114 | 115 | if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) { 116 | $this->addColumn('qty', 117 | array( 118 | 'header' => Mage::helper('catalog')->__('Qty'), 119 | 'width' => '100px', 120 | 'type' => 'number', 121 | 'index' => 'qty', 122 | )); 123 | } 124 | 125 | $this->addColumn('visibility', 126 | array( 127 | 'header' => Mage::helper('catalog')->__('Visibility'), 128 | 'width' => '70px', 129 | 'index' => 'visibility', 130 | 'type' => 'options', 131 | 'options' => Mage::getModel('catalog/product_visibility')->getOptionArray(), 132 | )); 133 | 134 | $this->addColumn('status', 135 | array( 136 | 'header' => Mage::helper('catalog')->__('Status'), 137 | 'width' => '70px', 138 | 'index' => 'status', 139 | 'type' => 'options', 140 | 'options' => Mage::getSingleton('catalog/product_status')->getOptionArray(), 141 | )); 142 | 143 | if (!Mage::app()->isSingleStoreMode()) { 144 | $this->addColumn('websites', 145 | array( 146 | 'header' => Mage::helper('catalog')->__('Websites'), 147 | 'width' => '100px', 148 | 'sortable' => FALSE, 149 | 'index' => 'websites', 150 | 'type' => 'options', 151 | 'options' => Mage::getModel('core/website')->getCollection()->toOptionHash(), 152 | )); 153 | } 154 | 155 | if (Mage::helper('catalog')->isModuleEnabled('Mage_Rss')) { 156 | $this->addRssList('rss/catalog/notifystock', Mage::helper('catalog')->__('Notify Low Stock RSS')); 157 | } 158 | return parent::_prepareColumns(); 159 | } 160 | 161 | public function getRowUrl($row) 162 | { 163 | return $this->getUrl('*/*/edit', array('id' => $row->getId())); 164 | } 165 | 166 | public function getGridUrl() 167 | { 168 | return $this->getData('grid_url') 169 | ? $this->getData('grid_url') 170 | : $this->getUrl('*/*/productlistGrid', array('_current' => TRUE, 'id' => $this->getRequest()->getParam('id'))); 171 | } 172 | 173 | 174 | protected function _getStore() 175 | { 176 | $storeId = (int)$this->getRequest()->getParam('store', 0); 177 | return Mage::app()->getStore($storeId); 178 | } 179 | 180 | 181 | } -------------------------------------------------------------------------------- /app/code/local/Magebuzz/Customoption/controllers/Adminhtml/CustomoptionController.php: -------------------------------------------------------------------------------- 1 | loadLayout() 8 | ->_setActiveMenu('customoption/items') 9 | ->_addBreadcrumb(Mage::helper('adminhtml')->__('Items Manager'), Mage::helper('adminhtml')->__('Item Manager')); 10 | 11 | return $this; 12 | } 13 | 14 | public function indexAction() 15 | { 16 | $this->_initAction() 17 | ->renderLayout(); 18 | } 19 | 20 | public function editAction() 21 | { 22 | $id = $this->getRequest()->getParam('id'); 23 | $model = Mage::getModel('catalog/product')->load($id); 24 | $collection = Mage::getModel('catalog/product_option')->getCollection(); 25 | $collection->addFieldToFilter('product_id', $id); 26 | $collection->getSelect() 27 | ->distinct() 28 | ->join(array('tbl2' => Mage::getSingleton('core/resource')->getTableName('catalog_product_option_title')), 'tbl2.option_id=main_table.option_id'); 29 | $options = $collection->getData(); 30 | foreach ($options as $option) { 31 | $option_id = $option['option_id']; 32 | $value = Mage::getModel('customoption/customoption')->getResource()->getValue($option['option_id']); 33 | $model->setData('value[' . $option_id . ']', $value[0]['option_type_id']); 34 | } 35 | if ($model->getId() || $id == 0) { 36 | $data = Mage::getSingleton('adminhtml/session')->getFormData(TRUE); 37 | if (!empty($data)) { 38 | $model->setData($data); 39 | } 40 | 41 | Mage::register('customoption_data', $model); 42 | 43 | $this->loadLayout(); 44 | $this->_setActiveMenu('customoption/items'); 45 | 46 | $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item Manager'), Mage::helper('adminhtml')->__('Item Manager')); 47 | $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item News'), Mage::helper('adminhtml')->__('Item News')); 48 | 49 | $this->getLayout()->getBlock('head')->setCanLoadExtJs(TRUE); 50 | 51 | $this->_addContent($this->getLayout()->createBlock('customoption/adminhtml_customoption_edit')) 52 | ->_addLeft($this->getLayout()->createBlock('customoption/adminhtml_customoption_edit_tabs')); 53 | 54 | $this->renderLayout(); 55 | } else { 56 | Mage::getSingleton('adminhtml/session')->addError(Mage::helper('customoption')->__('Item does not exist')); 57 | $this->_redirect('*/*/'); 58 | } 59 | } 60 | 61 | public function newAction() 62 | { 63 | $this->_forward('edit'); 64 | } 65 | 66 | protected function _getReadConnection() 67 | { 68 | return Mage::getModel('core/resource')->getConnection('core_read'); 69 | } 70 | 71 | public function saveAction() 72 | { 73 | if ($data = $this->getRequest()->getPost()) { 74 | $product_id = $this->getRequest()->getParam('id'); 75 | $model = Mage::getModel('customoption/customoption'); 76 | /* $model->setData($data) 77 | ->setId($this->getRequest()->getParam('id')); */ 78 | /* $opion_data=$model->getCollection()->getData(); 79 | if(!empty($opion_data)) { 80 | foreach ($opion_data as $key) { 81 | $model->load($key['customoption_id'])->delete(); 82 | } 83 | } */ 84 | $tablename = Mage::getModel('customoption/customoption')->getResource()->getTableName(); 85 | $connection = Mage::getSingleton('core/resource')->getConnection('core_write'); 86 | $fields = array(); 87 | foreach ($data['value'] as $option_id => $option_type_id) { 88 | // $option=Mage::getModel('customoption/customoption')->getResource()->getOption($product_id,$option_id); 89 | $option = Mage::helper('customoption')->getOption($option_id); 90 | if (!$option) { 91 | $model->setData('option_id', $option_id); 92 | $model->setData('option_type_id', $option_type_id); 93 | $model->save(); 94 | $model->unsetData(); 95 | } else { 96 | if ($option[0]['option_type_id'] != $option_type_id) { 97 | $fields['option_type_id'] = $option_type_id; 98 | $where = $connection->quoteInto('option_id=? ', $option_id); 99 | $connection->update($tablename, $fields, $where); 100 | $connection->commit(); 101 | } 102 | 103 | } 104 | } 105 | try { 106 | Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('customoption')->__('Option was successfully saved')); 107 | Mage::getSingleton('adminhtml/session')->setFormData(FALSE); 108 | 109 | if ($this->getRequest()->getParam('back')) { 110 | $this->_redirect('*/*/edit', array('id' => $product_id)); 111 | return; 112 | } 113 | $this->_redirect('*/*/'); 114 | return; 115 | } catch (Exception $e) { 116 | Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); 117 | Mage::getSingleton('adminhtml/session')->setFormData($data); 118 | $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id'))); 119 | return; 120 | } 121 | } 122 | Mage::getSingleton('adminhtml/session')->addError(Mage::helper('customoption')->__('Unable to find item to save')); 123 | $this->_redirect('*/*/'); 124 | } 125 | 126 | public function deleteAction() 127 | { 128 | if ($this->getRequest()->getParam('id') > 0) { 129 | try { 130 | $model = Mage::getModel('customoption/customoption'); 131 | 132 | $model->setId($this->getRequest()->getParam('id')) 133 | ->delete(); 134 | 135 | Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully deleted')); 136 | $this->_redirect('*/*/'); 137 | } catch (Exception $e) { 138 | Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); 139 | $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id'))); 140 | } 141 | } 142 | $this->_redirect('*/*/'); 143 | } 144 | 145 | public function massDeleteAction() 146 | { 147 | $customoptionIds = $this->getRequest()->getParam('customoption'); 148 | if (!is_array($customoptionIds)) { 149 | Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select item(s)')); 150 | } else { 151 | try { 152 | foreach ($customoptionIds as $customoptionId) { 153 | $customoption = Mage::getModel('customoption/customoption')->load($customoptionId); 154 | $customoption->delete(); 155 | } 156 | Mage::getSingleton('adminhtml/session')->addSuccess( 157 | Mage::helper('adminhtml')->__( 158 | 'Total of %d record(s) were successfully deleted', count($customoptionIds) 159 | ) 160 | ); 161 | } catch (Exception $e) { 162 | Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); 163 | } 164 | } 165 | $this->_redirect('*/*/index'); 166 | } 167 | 168 | public function massStatusAction() 169 | { 170 | $customoptionIds = $this->getRequest()->getParam('customoption'); 171 | if (!is_array($customoptionIds)) { 172 | Mage::getSingleton('adminhtml/session')->addError($this->__('Please select item(s)')); 173 | } else { 174 | try { 175 | foreach ($customoptionIds as $customoptionId) { 176 | $customoption = Mage::getSingleton('customoption/customoption') 177 | ->load($customoptionId) 178 | ->setStatus($this->getRequest()->getParam('status')) 179 | ->setIsMassupdate(TRUE) 180 | ->save(); 181 | } 182 | $this->_getSession()->addSuccess( 183 | $this->__('Total of %d record(s) were successfully updated', count($customoptionIds)) 184 | ); 185 | } catch (Exception $e) { 186 | $this->_getSession()->addError($e->getMessage()); 187 | } 188 | } 189 | $this->_redirect('*/*/index'); 190 | } 191 | 192 | public function exportCsvAction() 193 | { 194 | $fileName = 'customoption.csv'; 195 | $content = $this->getLayout()->createBlock('customoption/adminhtml_customoption_grid') 196 | ->getCsv(); 197 | 198 | $this->_sendUploadResponse($fileName, $content); 199 | } 200 | 201 | public function exportXmlAction() 202 | { 203 | $fileName = 'customoption.xml'; 204 | $content = $this->getLayout()->createBlock('customoption/adminhtml_customoption_grid') 205 | ->getXml(); 206 | 207 | $this->_sendUploadResponse($fileName, $content); 208 | } 209 | 210 | protected function _sendUploadResponse($fileName, $content, $contentType = 'application/octet-stream') 211 | { 212 | $response = $this->getResponse(); 213 | $response->setHeader('HTTP/1.1 200 OK', ''); 214 | $response->setHeader('Pragma', 'public', TRUE); 215 | $response->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', TRUE); 216 | $response->setHeader('Content-Disposition', 'attachment; filename=' . $fileName); 217 | $response->setHeader('Last-Modified', date('r')); 218 | $response->setHeader('Accept-Ranges', 'bytes'); 219 | $response->setHeader('Content-Length', strlen($content)); 220 | $response->setHeader('Content-type', $contentType); 221 | $response->setBody($content); 222 | $response->sendResponse(); 223 | die; 224 | } 225 | 226 | public function productlistGridAction() 227 | { 228 | $this->loadLayout(); 229 | $this->renderLayout(); 230 | } 231 | 232 | public function gridAction() 233 | { 234 | $this->loadLayout(); 235 | $this->renderLayout(); 236 | } 237 | 238 | } 239 | --------------------------------------------------------------------------------