Mage_Catalog_Model_Product_Option Class Reference

Inheritance diagram for Mage_Catalog_Model_Product_Option:

Mage_Core_Model_Abstract Varien_Object

List of all members.

Public Member Functions

 addValue (Mage_Catalog_Model_Product_Option_Value $value)
 getValueById ($valueId)
 getValues ()
 getValueInstance ()
 addOption ($option)
 getOptions ()
 setOptions ($options)
 unsetOptions ()
 getProduct ()
 setProduct (Mage_Catalog_Model_Product $product=null)
 getGroupByType ($type=null)
 groupFactory ($type)
 saveOptions ()
 getPrice ($flag=false)
 deletePrices ($option_id)
 deleteTitles ($option_id)
 getProductOptionCollection (Mage_Catalog_Model_Product $product)
 getValuesCollection ()
 getOptionValuesByOptionId ($optionIds, $store_id)
 prepareOptionForDuplicate ()
 duplicate ($oldProductId, $newProductId)
 getSearchableData ($productId, $storeId)

Public Attributes

const OPTION_GROUP_TEXT = 'text'
const OPTION_GROUP_FILE = 'file'
const OPTION_GROUP_SELECT = 'select'
const OPTION_GROUP_DATE = 'date'
const OPTION_TYPE_FIELD = 'field'
const OPTION_TYPE_AREA = 'area'
const OPTION_TYPE_FILE = 'file'
const OPTION_TYPE_DROP_DOWN = 'drop_down'
const OPTION_TYPE_RADIO = 'radio'
const OPTION_TYPE_CHECKBOX = 'checkbox'
const OPTION_TYPE_MULTIPLE = 'multiple'
const OPTION_TYPE_DATE = 'date'
const OPTION_TYPE_DATE_TIME = 'date_time'
const OPTION_TYPE_TIME = 'time'

Protected Member Functions

 _construct ()
 _afterSave ()

Protected Attributes

 $_product
 $_options = array()
 $_valueInstance
 $_values = array()


Detailed Description

Definition at line 34 of file Option.php.


Member Function Documentation

_afterSave (  )  [protected]

Processing object after save data

Returns:
Mage_Core_Model_Abstract

Reimplemented from Mage_Core_Model_Abstract.

Definition at line 284 of file Option.php.

00285     {
00286         $this->getValueInstance()->unsetValues();
00287         if (is_array($this->getData('values'))) {
00288             foreach ($this->getData('values') as $value) {
00289                 $this->getValueInstance()->addValue($value);
00290             }
00291 
00292             $this->getValueInstance()->setOption($this)
00293                 ->saveValues();
00294         } elseif ($this->getGroupByType($this->getType()) == self::OPTION_GROUP_SELECT) {
00295             Mage::throwException(Mage::helper('catalog')->__('Select type options required values rows.'));
00296         }
00297 
00298         return parent::_afterSave();
00299     }

_construct (  )  [protected]

Enter description here...

Reimplemented from Varien_Object.

Definition at line 60 of file Option.php.

00061     {
00062         $this->_init('catalog/product_option');
00063     }

addOption ( option  ) 

Add option for save it

Parameters:
array $option
Returns:
Mage_Catalog_Model_Product_Option

Definition at line 116 of file Option.php.

00117     {
00118         $this->_options[] = $option;
00119         return $this;
00120     }

addValue ( Mage_Catalog_Model_Product_Option_Value value  ) 

Add value of option to values array

Parameters:
Mage_Catalog_Model_Product_Option_Value $value
Returns:
Mage_Catalog_Model_Product_Option

Definition at line 71 of file Option.php.

00072     {
00073         $this->_values[$value->getId()] = $value;
00074         return $this;
00075     }

deletePrices ( option_id  ) 

Delete prices of option

Parameters:
int $option_id
Returns:
Mage_Catalog_Model_Product_Option

Definition at line 324 of file Option.php.

00325     {
00326         $this->getResource()->deletePrices($option_id);
00327         return $this;
00328     }

deleteTitles ( option_id  ) 

Delete titles of option

Parameters:
int $option_id
Returns:
Mage_Catalog_Model_Product_Option

Definition at line 336 of file Option.php.

00337     {
00338         $this->getResource()->deleteTitles($option_id);
00339         return $this;
00340     }

duplicate ( oldProductId,
newProductId 
)

Duplicate options for product

Parameters:
int $oldProductId
int $newProductId
Returns:
Mage_Catalog_Model_Product_Option

Definition at line 417 of file Option.php.

00418     {
00419         $this->getResource()->duplicate($this, $oldProductId, $newProductId);
00420 
00421         return $this;
00422     }

getGroupByType ( type = null  ) 

Get group name of option by given option type

Parameters:
string $type
Returns:
array

Definition at line 183 of file Option.php.

00184     {
00185         if (is_null($type)) {
00186             $type = $this->getType();
00187         }
00188         $optionGroupsToTypes = array(
00189             self::OPTION_TYPE_FIELD => self::OPTION_GROUP_TEXT,
00190             self::OPTION_TYPE_AREA => self::OPTION_GROUP_TEXT,
00191             self::OPTION_TYPE_FILE => self::OPTION_GROUP_FILE,
00192             self::OPTION_TYPE_DROP_DOWN => self::OPTION_GROUP_SELECT,
00193             self::OPTION_TYPE_RADIO => self::OPTION_GROUP_SELECT,
00194             self::OPTION_TYPE_CHECKBOX => self::OPTION_GROUP_SELECT,
00195             self::OPTION_TYPE_MULTIPLE => self::OPTION_GROUP_SELECT,
00196             self::OPTION_TYPE_DATE => self::OPTION_GROUP_DATE,
00197             self::OPTION_TYPE_DATE_TIME => self::OPTION_GROUP_DATE,
00198             self::OPTION_TYPE_TIME => self::OPTION_GROUP_DATE,
00199         );
00200 
00201         return isset($optionGroupsToTypes[$type])?$optionGroupsToTypes[$type]:'';
00202     }

getOptions (  ) 

Get all options

Returns:
array

Definition at line 127 of file Option.php.

00128     {
00129         return $this->_options;
00130     }

getOptionValuesByOptionId ( optionIds,
store_id 
)

Get collection of values by given option ids

Parameters:
array $optionIds
int $store_id
Returns:
unknown

Definition at line 381 of file Option.php.

00382     {
00383         $collection = Mage::getModel('catalog/product_option_value')
00384             ->getValuesByOption($optionIds, $this->getId(), $store_id);
00385 
00386         return $collection;
00387     }

getPrice ( flag = false  ) 

Return price. If $flag is true and price is percent return converted percent to price

Parameters:
bool $flag
Returns:
decimal

Definition at line 308 of file Option.php.

00309     {
00310         if ($flag && $this->getPriceType() == 'percent') {
00311             $basePrice = $this->getProduct()->getFinalPrice();
00312             $price = $basePrice*($this->_getData('price')/100);
00313             return $price;
00314         }
00315         return $this->_getData('price');
00316     }

getProduct (  ) 

Retrieve product instance

Returns:
Mage_Catalog_Model_Product

Definition at line 160 of file Option.php.

00161     {
00162         return $this->_product;
00163     }

getProductOptionCollection ( Mage_Catalog_Model_Product product  ) 

get Product Option Collection

Parameters:
Mage_Catalog_Model_Product $product
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Option_Collection

Definition at line 348 of file Option.php.

00349     {
00350         $collection = $this->getCollection()
00351             ->addFieldToFilter('product_id', $product->getId())
00352             ->addTitleToResult($product->getStoreId())
00353             ->addPriceToResult($product->getStoreId())
00354             ->setOrder('sort_order', 'asc')
00355             ->setOrder('title', 'asc')
00356             ->addValuesToResult($product->getStoreId());
00357 
00358         return $collection;
00359     }

getSearchableData ( productId,
storeId 
)

Retrieve option searchable data

Parameters:
int $productId
int $storeId
Returns:
array

Definition at line 431 of file Option.php.

00432     {
00433         return $this->_getResource()->getSearchableData($productId, $storeId);
00434     }

getValueById ( valueId  ) 

Get value by given id

Parameters:
int $valueId
Returns:
Mage_Catalog_Model_Product_Option_Value

Definition at line 83 of file Option.php.

00084     {
00085         if (isset($this->_values[$valueId])) {
00086             return $this->_values[$valueId];
00087         }
00088 
00089         return null;
00090     }

getValueInstance (  ) 

Retrieve value instance

Returns:
Mage_Catalog_Model_Product_Option_Value

Definition at line 102 of file Option.php.

00103     {
00104         if (!$this->_valueInstance) {
00105             $this->_valueInstance = Mage::getSingleton('catalog/product_option_value');
00106         }
00107         return $this->_valueInstance;
00108     }

getValues (  ) 

Definition at line 92 of file Option.php.

00093     {
00094         return $this->_values;
00095     }

getValuesCollection (  ) 

Get collection of values for current option

Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Option_Value_Collection

Definition at line 366 of file Option.php.

00367     {
00368         $collection = $this->getValueInstance()
00369             ->getValuesCollection($this);
00370 
00371         return $collection;
00372     }

groupFactory ( type  ) 

Group model factory

Parameters:
string $type Option type
Returns:
Mage_Catalog_Model_Product_Option_Group_Abstract

Definition at line 210 of file Option.php.

00211     {
00212         $group = $this->getGroupByType($type);
00213         if (!empty($group)) {
00214             return Mage::getModel('catalog/product_option_type_' . $group);
00215         }
00216         Mage::throwException(Mage::helper('catalog')->__('Wrong option type to get group instance.'));
00217     }

prepareOptionForDuplicate (  ) 

Prepare array of options for duplicate

Returns:
array

Definition at line 394 of file Option.php.

00395     {
00396         $this->setProductId(null);
00397         $this->setOptionId(null);
00398         $newOption = $this->__toArray();
00399         if ($_values = $this->getValues()) {
00400             $newValuesArray = array();
00401             foreach ($_values as $_value) {
00402                 $newValuesArray[] = $_value->prepareValueForDuplicate();
00403             }
00404             $newOption['values'] = $newValuesArray;
00405         }
00406 
00407         return $newOption;
00408     }

saveOptions (  ) 

Save options.

Returns:
Mage_Catalog_Model_Product_Option

Definition at line 224 of file Option.php.

00225     {
00226         foreach ($this->getOptions() as $option) {
00227             $this->setData($option)
00228                 ->setData('product_id', $this->getProduct()->getId())
00229                 ->setData('store_id', $this->getProduct()->getStoreId());
00230 
00231             if ($this->getData('option_id') == '0') {
00232                 $this->unsetData('option_id');
00233             } else {
00234                 $this->setId($this->getData('option_id'));
00235             }
00236             $isEdit = (bool)$this->getId()? true:false;
00237 
00238             if ($this->getData('is_delete') == '1') {
00239                 if ($isEdit) {
00240                     $this->getValueInstance()->deleteValue($this->getId());
00241                     $this->deletePrices($this->getId());
00242                     $this->deleteTitles($this->getId());
00243                     $this->delete();
00244                 }
00245             } else {
00246                 if ($this->getData('previous_type') != '') {
00247                     $previousType = $this->getData('previous_type');
00248                     //if previous option has dfferent group from one is came now need to remove all data of previous group
00249                     if ($this->getGroupByType($previousType) != $this->getGroupByType($this->getData('type'))) {
00250 
00251                         switch ($this->getGroupByType($previousType)) {
00252                             case self::OPTION_GROUP_SELECT:
00253                                 $this->unsetData('values');
00254                                 if ($isEdit) {
00255                                     $this->getValueInstance()->deleteValue($this->getId());
00256                                 }
00257                                 break;
00258                             case self::OPTION_GROUP_FILE:
00259                                 $this->setData('file_extension', '');
00260                                 $this->setData('image_size_x', '0');
00261                                 $this->setData('image_size_y', '0');
00262                                 break;
00263                             case self::OPTION_GROUP_TEXT:
00264                                 $this->setData('max_characters', '0');
00265                                 break;
00266                             case self::OPTION_GROUP_DATE:
00267                                 break;
00268                         }
00269                         if ($this->getGroupByType($this->getData('type')) == self::OPTION_GROUP_SELECT) {
00270                             $this->setData('sku', '');
00271                             $this->unsetData('price');
00272                             $this->unsetData('price_type');
00273                             if ($isEdit) {
00274                                 $this->deletePrices($this->getId());
00275                             }
00276                         }
00277                     }
00278                 }
00279                 $this->save();            }
00280         }//eof foreach()
00281         return $this;
00282     }

setOptions ( options  ) 

Set options for array

Parameters:
array $options
Returns:
Mage_Catalog_Model_Product_Option

Definition at line 138 of file Option.php.

00139     {
00140         $this->_options = $options;
00141         return $this;
00142     }

setProduct ( Mage_Catalog_Model_Product product = null  ) 

Set product instance

Parameters:
Mage_Catalog_Model_Product $product
Returns:
Mage_Catalog_Model_Product_Option

Definition at line 171 of file Option.php.

00172     {
00173         $this->_product = $product;
00174         return $this;
00175     }

unsetOptions (  ) 

Set options to empty array

Returns:
Mage_Catalog_Model_Product_Option

Definition at line 149 of file Option.php.

00150     {
00151         $this->_options = array();
00152         return $this;
00153     }


Member Data Documentation

$_options = array() [protected]

Definition at line 54 of file Option.php.

$_product [protected]

Definition at line 52 of file Option.php.

$_valueInstance [protected]

Definition at line 56 of file Option.php.

$_values = array() [protected]

Definition at line 58 of file Option.php.

const OPTION_GROUP_DATE = 'date'

Definition at line 39 of file Option.php.

const OPTION_GROUP_FILE = 'file'

Definition at line 37 of file Option.php.

const OPTION_GROUP_SELECT = 'select'

Definition at line 38 of file Option.php.

Definition at line 36 of file Option.php.

const OPTION_TYPE_AREA = 'area'

Definition at line 42 of file Option.php.

const OPTION_TYPE_CHECKBOX = 'checkbox'

Definition at line 46 of file Option.php.

const OPTION_TYPE_DATE = 'date'

Definition at line 48 of file Option.php.

const OPTION_TYPE_DATE_TIME = 'date_time'

Definition at line 49 of file Option.php.

const OPTION_TYPE_DROP_DOWN = 'drop_down'

Definition at line 44 of file Option.php.

const OPTION_TYPE_FIELD = 'field'

Definition at line 41 of file Option.php.

const OPTION_TYPE_FILE = 'file'

Definition at line 43 of file Option.php.

const OPTION_TYPE_MULTIPLE = 'multiple'

Definition at line 47 of file Option.php.

const OPTION_TYPE_RADIO = 'radio'

Definition at line 45 of file Option.php.

const OPTION_TYPE_TIME = 'time'

Definition at line 50 of file Option.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:23:41 2009 for Magento by  doxygen 1.5.8