Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.05 KB | None | 0 0
  1. <?xml version="1.0"?>
  2. <config>
  3. <modules>
  4. <Yourpackage_Yourmodule>
  5. <version>0.1.0</version>
  6. </Yourpackage_Yourmodule>
  7. </modules>
  8. <global>
  9. <blocks>
  10. <bundle>
  11. <rewrite>
  12. <adminhtml_catalog_product_edit_tab_bundle_option>Yourpackage_Yourmodule_Block_Adminhtml_Catalog_Product_Edit_Tab_Bundle_Option</adminhtml_catalog_product_edit_tab_bundle_option>
  13. </rewrite>
  14. </bundle>
  15. </blocks>
  16. <models>
  17. <yourmodule>
  18. <class>Yourpackage_Yourmodule_Model</class>
  19. <resourceModel>yourmodule_mysql4</resourceModel>
  20. </yourmodule>
  21. <yourmodule_mysql4>
  22. <class>Yourpackage_Yourmodule_Model_Mysql4</class>
  23. <entities>
  24. <fixedbundle>
  25. <table>catalog_product_bundle_option_fixedbundle</table>
  26. </fixedbundle>
  27. </entities>
  28. </yourmodule_mysql4>
  29. </models>
  30. <resources>
  31. <yourmodule_setup>
  32. <setup>
  33. <module>Yourpackage_Yourmodule</module>
  34. <class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
  35. </setup>
  36. <connection>
  37. <use>core_setup</use>
  38. </connection>
  39. </yourmodule_setup>
  40. <yourmodule_write>
  41. <connection>
  42. <use>core_write</use>
  43. </connection>
  44. </yourmodule_write>
  45. <yourmodule_read>
  46. <connection>
  47. <use>core_read</use>
  48. </connection>
  49. </yourmodule_read>
  50. </resources>
  51. </global>
  52. <adminhtml>
  53. <events>
  54. <catalog_product_save_commit_after>
  55. <observers>
  56. <custom_field_observer>
  57. <type>singleton</type>
  58. <class>Yourpackage_Yourmodule_Model_Observer</class>
  59. <method>SaveDropdownAfterProductSave</method>
  60. </custom_field_observer>
  61. </observers>
  62. </catalog_product_save_commit_after>
  63. </events>
  64. </adminhtml>
  65. </config>
  66.  
  67. <?php
  68. $installer=$this;
  69. $installer->startSetup();
  70.  
  71. $installer->run("
  72. -- DROP TABLE IF EXISTS {$this->getTable('yourmodule/fixedbundle')};
  73. CREATE TABLE {$this->getTable('yourmodule/fixedbundle')} (
  74. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  75. `store_id` int(11) NOT NULL default '0',
  76. `option_id` int(11) NOT NULL,
  77. `is_fixed_bundle` int(11) NOT NULL default '0',
  78. PRIMARY KEY (`id`)
  79. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  80.  
  81. ");
  82. $installer->endSetup();
  83. ?>
  84.  
  85. <?php
  86. class Yourpackage_Yourmodule_Model_Fixedbundle extends Mage_Core_Model_Abstract {
  87.  
  88. public function _construct()
  89. {
  90. $this->_init('yourmodule/fixedbundle');
  91. }
  92.  
  93. }
  94.  
  95. <?php
  96. class Yourpackage_Yourmodule_Model_Mysql4_Fixedbundle extends Mage_Core_Model_Mysql4_Abstract
  97. {
  98. public function _construct()
  99. {
  100. $this->_init('yourmodule/fixedbundle', 'id');
  101. }
  102. }
  103.  
  104. <?php
  105. class Yourpackage_Yourmodule_Model_Mysql4_Fixedbundle_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract {
  106. protected function _construct(){
  107. $this->_init('yourmodule/fixedbundle');
  108. }
  109. }
  110.  
  111. <?php
  112. class Yourpackage_Yourmodule_Block_Adminhtml_Catalog_Product_Edit_Tab_Bundle_Option extends Mage_Bundle_Block_Adminhtml_Catalog_Product_Edit_Tab_Bundle_Option
  113. {
  114. public function getOptions()
  115. {
  116. if (!$this->_options) {
  117. $this->getProduct()->getTypeInstance(true)->setStoreFilter($this->getProduct()->getStoreId(),
  118. $this->getProduct());
  119.  
  120. $optionCollection = $this->getProduct()->getTypeInstance(true)->getOptionsCollection($this->getProduct());
  121.  
  122. $selectionCollection = $this->getProduct()->getTypeInstance(true)->getSelectionsCollection(
  123. $this->getProduct()->getTypeInstance(true)->getOptionsIds($this->getProduct()),
  124. $this->getProduct()
  125. );
  126.  
  127. $this->_options = $optionCollection->appendSelections($selectionCollection);
  128. $storeId = $this->getProduct()->getData('store_id');
  129. foreach ($this->_options as $option) {
  130. //gets each option's id
  131. $option_id = $option->getData('option_id');
  132. $optionFixed = Mage::getModel('yourmodule/fixedbundle')->load($option_id, "option_id");
  133. if ($optionFixed->getId() != "") {
  134. $id = (int)$optionFixed->getId();
  135. $isFixedBundle = $optionFixed->getIsFixedBundle();
  136. //adds our new datas to option
  137. $option->addData(array('fixedbundle_id'=> $id, 'is_fixed_bundle' => $isFixedBundle, 'is_new'=> 'no'));
  138. } else {
  139. $option->addData(array('fixedbundle_id'=> '', 'is_fixed_bundle' => '', 'is_new'=> 'yes'));
  140. }
  141. }
  142.  
  143. if ($this->getCanReadPrice() === false) {
  144. foreach ($this->_options as $option) {
  145. if ($option->getSelections()) {
  146. foreach ($option->getSelections() as $selection) {
  147. $selection->setCanReadPrice($this->getCanReadPrice());
  148. $selection->setCanEditPrice($this->getCanEditPrice());
  149. }
  150. }
  151. }
  152. }
  153. }
  154. return $this->_options;
  155. }
  156.  
  157. public function getIsFixedSelectHtml()
  158. {
  159. $options = array(
  160. array('value' => 0, 'label'=>Mage::helper('adminhtml')->__('No')),
  161. array('value' => 1, 'label'=>Mage::helper('adminhtml')->__('Yes')),
  162. );
  163. $select = $this->getLayout()->createBlock('adminhtml/html_select')
  164. ->setData(array(
  165. 'id' => $this->getFieldId().'_{{index}}_is_fixed_bundle',
  166. 'class' => 'select is-fixed-bundle-assigned-select'
  167. ))
  168. ->setName($this->getFieldName().'[{{index}}][is_fixed_bundle]')
  169. ->setOptions($options);
  170. return $select->getHtml();
  171. }
  172.  
  173. }
  174.  
  175. <script type="text/javascript">
  176. optionTemplate = '<div id="<?php echo $this->getFieldId() ?>_{{index}}" class="option-box"> ' +
  177. '<input id="fixedbundle_id" type="hidden" name="<?php echo $this->getFieldName() ?>[{{index}}][fixedbundle_id]" value="{{fixedbundle_id}}" />'+
  178. '<input id="is_new" type="hidden" name="<?php echo $this->getFieldName() ?>[{{index}}][is_new]" value="{{is_new}}" />'+
  179. '<div class="option-title"> ' +
  180. '<label for="<?php echo $this->getFieldName() ?>[{{index}}][title]"><?php echo $this->jsQuoteEscape(Mage::helper('bundle')->__('Default Title')) ?> <span class="required">*</span></label>' +
  181. <?php if ($this->isDefaultStore()): ?>
  182. '<input class="input-text required-entry" type="text" name="<?php echo $this->getFieldName() ?>[{{index}}][title]" id="id_<?php echo $this->getFieldName() ?>_{{index}}_title" value="{{title}}">' +
  183. <?php else: ?>
  184. '<input class="input-text required-entry" type="text" name="<?php echo $this->getFieldName() ?>[{{index}}][default_title]" id="id_<?php echo $this->getFieldName() ?>_{{index}}_default_title" value="{{default_title}}">' +
  185. <?php endif; ?>
  186. '<?php echo $this->jsQuoteEscape($this->getOptionDeleteButtonHtml()) ?>' +
  187. '</div>' +
  188. '<table class="option-header" cellpadding="0" cellspacing="0">' +
  189. '<thead>' +
  190. '<tr>' +
  191. <?php if (!$this->isDefaultStore()): ?>
  192. '<th class="opt-title"><?php echo $this->jsQuoteEscape(Mage::helper('bundle')->__('Store View Title')) ?> <span class="required">*</span></th>' +
  193. <?php endif; ?>
  194. '<th class="opt-type"><?php echo $this->jsQuoteEscape(Mage::helper('bundle')->__('Input Type')) ?></th>' +
  195. '<th class="opt-req"><?php echo $this->jsQuoteEscape(Mage::helper('bundle')->__('Is Required')) ?></th>' +
  196. '<th class="opt-order"><?php echo $this->jsQuoteEscape(Mage::helper('bundle')->__('Position')) ?></th>' +
  197. '<th class="opt-order is-fixed-bundle-assigned"><?php echo Mage::helper('bundle')->__('Is Fixed Bundle') ?></th>' +
  198. '<th>&nbsp;</th>' +
  199. '</tr>' +
  200. '</thead>' +
  201. '<tbody>' +
  202. '<tr>' +
  203. '<input type="hidden" id="<?php echo $this->getFieldId() ?>_id_{{index}}" name="<?php echo $this->getFieldName() ?>[{{index}}][option_id]" value="{{option_id}}">' +
  204. '<input type="hidden" name="<?php echo $this->getFieldName() ?>[{{index}}][delete]" value="" class="delete">' +
  205. <?php if (!$this->isDefaultStore()): ?>
  206. '<td><input class="input-text required-entry" type="text" name="<?php echo $this->getFieldName() ?>[{{index}}][title]" id="id_<?php echo $this->getFieldName() ?>_{{index}}_title_store" value="{{title}}"></td>' +
  207. <?php endif; ?>
  208. '<td><?php echo $this->getTypeSelectHtml() ?></td>' +
  209. '<td><?php echo $this->getRequireSelectHtml() ?></td>' +
  210. '<td><input class="input-text validate-zero-or-greater" type="text" name="<?php echo $this->getFieldName() ?>[{{index}}][position]" value="{{position}}"></td>' +
  211. '<td class="is-fixed-bundle-assigned"><?php echo $this->getIsFixedSelectHtml() ?></td>' +
  212. '<td>&nbsp;<?php echo $this->jsQuoteEscape($this->getAddSelectionButtonHtml()) ?></td>' +
  213. '</tr>' +
  214. '</tbody>' +
  215. '</table>' +
  216. '<div id="<?php echo $this->getFieldId() ?>_search_{{index}}">' +
  217. '</div>' +
  218. '</div>';
  219. </script>
  220.  
  221. <?php
  222.  
  223. class Yourpackage_Yourmodule_Model_Observer extends Mage_Core_Model_Abstract
  224. {
  225. public function SaveDropdownAfterProductSave($observer)
  226. {
  227. $product = Mage::registry('product');
  228. if (Mage::registry('catalog_product_save_commit_after' . $product->getId())) {
  229. return;
  230. }
  231. Mage::register('catalog_product_save_commit_after' . $product->getId(), true);
  232. $model = Mage::getModel('yourmodule/fixedbundle');
  233. $optionCollection = $product->getTypeInstance(TRUE)->getOptionsCollection($product);
  234. $bundleOptions = $product->getBundleOptionsData();
  235. if (!empty($bundleOptions)) {
  236. $storeId = (int)$product->getData('store_id');
  237. foreach ($bundleOptions as $option) {
  238. $optionId = (int)$option['option_id'];
  239. $id = (int)$option['fixedbundle_id'];
  240. $isDeleted = (int)$option['delete'];
  241. //use to set option_id for new options in our module
  242. if ($optionId <= 0 || $optionId == "" || is_null($optionId)) {
  243. foreach ($optionCollection as $new) {
  244. if($new['type'] == $option['type'] && ( $new['title'] == $option['title'] || $new['default_title'] == $option['title'] )){
  245. $optionId = $new['option_id'];
  246. }
  247. }
  248. }
  249. $data = array(
  250. 'option_id' => $optionId,
  251. 'store_id' => $storeId,
  252. 'is_fixed_bundle' => $option['is_fixed_bundle']
  253. );
  254. $optionFixed = Mage::getModel('yourmodule/fixedbundle')->load($optionId, "option_id");
  255. // id exist means already there is an entry in custom table
  256. try {
  257. if ($isDeleted == 1) {
  258. $model->setId($id)->delete();
  259. } else {
  260. if ($optionFixed->getId() != ""){
  261. $model->load($id);
  262. $model->addData($data);
  263. $model->setId($id)->save();
  264. } else {
  265. $model->setData($data);
  266. $insertId = $model->save()->getId();
  267. }
  268. }
  269. } catch (Exception $e) {
  270. Mage::logException($e);
  271. }
  272. }
  273. }
  274. return $this;
  275. }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement