1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42:
43: class Mage_Catalog_Model_Product_Option_Value extends Mage_Core_Model_Abstract
44: {
45: protected $_values = array();
46:
47: protected $_product;
48:
49: protected $_option;
50:
51: protected function _construct()
52: {
53: $this->_init('catalog/product_option_value');
54: }
55:
56: public function addValue($value)
57: {
58: $this->_values[] = $value;
59: return $this;
60: }
61:
62: public function getValues()
63: {
64: return $this->_values;
65: }
66:
67: public function setValues($values)
68: {
69: $this->_values = $values;
70: return $this;
71: }
72:
73: public function unsetValues()
74: {
75: $this->_values = array();
76: return $this;
77: }
78:
79: public function setOption(Mage_Catalog_Model_Product_Option $option)
80: {
81: $this->_option = $option;
82: return $this;
83: }
84:
85: public function unsetOption()
86: {
87: $this->_option = null;
88: return $this;
89: }
90:
91: 92: 93: 94: 95:
96: public function getOption()
97: {
98: return $this->_option;
99: }
100:
101: public function setProduct($product)
102: {
103: $this->_product = $product;
104: return $this;
105: }
106:
107: public function getProduct()
108: {
109: if (is_null($this->_product)) {
110: $this->_product = $this->getOption()->getProduct();
111: }
112: return $this->_product;
113: }
114:
115: public function saveValues()
116: {
117: foreach ($this->getValues() as $value) {
118: $this->setData($value)
119: ->setData('option_id', $this->getOption()->getId())
120: ->setData('store_id', $this->getOption()->getStoreId());
121:
122: if ($this->getData('option_type_id') == '-1') {
123: $this->unsetData('option_type_id');
124: } else {
125: $this->setId($this->getData('option_type_id'));
126: }
127:
128: if ($this->getData('is_delete') == '1') {
129: if ($this->getId()) {
130: $this->deleteValues($this->getId());
131: $this->delete();
132: }
133: } else {
134: $this->save();
135: }
136: }
137: return $this;
138: }
139:
140: 141: 142: 143: 144: 145: 146:
147: public function getPrice($flag=false)
148: {
149: if ($flag && $this->getPriceType() == 'percent') {
150: $basePrice = $this->getOption()->getProduct()->getFinalPrice();
151: $price = $basePrice*($this->_getData('price')/100);
152: return $price;
153: }
154: return $this->_getData('price');
155: }
156:
157: 158: 159: 160: 161: 162:
163: public function getValuesCollection(Mage_Catalog_Model_Product_Option $option)
164: {
165: $collection = Mage::getResourceModel('catalog/product_option_value_collection')
166: ->addFieldToFilter('option_id', $option->getId())
167: ->getValues($option->getStoreId());
168:
169: return $collection;
170: }
171:
172: public function getValuesByOption($optionIds, $option_id, $store_id)
173: {
174: $collection = Mage::getResourceModel('catalog/product_option_value_collection')
175: ->addFieldToFilter('option_id', $option_id)
176: ->getValuesByOption($optionIds, $store_id);
177:
178: return $collection;
179: }
180:
181: public function deleteValue($option_id)
182: {
183: $this->getResource()->deleteValue($option_id);
184: return $this;
185: }
186:
187: public function deleteValues($option_type_id)
188: {
189: $this->getResource()->deleteValues($option_type_id);
190: return $this;
191: }
192:
193: 194: 195: 196: 197:
198: public function prepareValueForDuplicate()
199: {
200: $this->setOptionId(null);
201: $this->setOptionTypeId(null);
202:
203: return $this->__toArray();
204: }
205:
206: 207: 208: 209: 210: 211: 212:
213: public function duplicate($oldOptionId, $newOptionId)
214: {
215: $this->getResource()->duplicate($this, $oldOptionId, $newOptionId);
216: return $this;
217: }
218: }
219: