public function overrideAttribute(Mage_Catalog_Model_Resource_Eav_Attribute $attribute) { if (!empty($this->_attributeOverrides[$attribute->getAttributeCode()])) { $data = $this->_attributeOverrides[$attribute->getAttributeCode()]; if (isset($data['options_method']) && method_exists($this, $data['options_method'])) { $data['filter_options'] = $this->{$data['options_method']}(); } $attribute->addData($data); return true; } return false; }
/** * Add filter by attribute price * * @param Mage_CatalogSearch_Model_Advanced $object * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute * @param string|array $value */ public function addPriceFilter($object, $attribute, $value) { if (empty($value['from']) && empty($value['to'])) { return false; } if (Mage::helper('solr')->isEnabled()) { $typeConverter = new DMC_Solr_Model_SolrServer_Adapter_Product_TypeConverter(); $code = $attribute->getAttributeCode(); $items = $typeConverter->getItems(); $select = $object->getProductCollection()->getSelect(); $rate = 1; if (!empty($value['currency'])) { $rate = Mage::app()->getStore()->getBaseCurrency()->getRate($value['currency']); } if (strlen($value['from']) > 0) { $from = $value['from'] * $rate; } else { $from = 0; } if (strlen($value['to']) > 0) { $to = $value['to'] * $rate; } else { $to = 99999999; } $select->where($items[$attribute->getFrontend()->getInputType()]['solr_index_prefix'] . $typeConverter::SUBPREFIX_INDEX . 'price:[' . $from . ' TO ' . $to . ']'); } else { $adapter = $this->_getReadAdapter(); $conditions = array(); if (strlen($value['from']) > 0) { $conditions[] = $adapter->quoteInto('price_index.min_price %s * %s >= ?', $value['from']); } if (strlen($value['to']) > 0) { $conditions[] = $adapter->quoteInto('price_index.min_price %s * %s <= ?', $value['to']); } if (!$conditions) { return false; } $object->getProductCollection()->addPriceData(); $select = $object->getProductCollection()->getSelect(); $response = $this->_dispatchPreparePriceEvent($select); $additional = join('', $response->getAdditionalCalculations()); $rate = 1; if (!empty($value['currency'])) { $rate = Mage::app()->getStore()->getBaseCurrency()->getRate($value['currency']); } foreach ($conditions as $condition) { $select->where(sprintf($condition, $additional, $rate)); } } return true; }
/** * Append search params to the form * * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute Attribute currently edited * @param Varien_Data_Form $form Form the * * @return Smile_ElasticSearch_Model_Adminhtml_Catalog_Product_Attribute_Edit_Form_Search */ public function addSearchParams($attribute, $form) { $fieldset = $this->_getFieldset($form); $fieldset->addField('search_weight', 'text', array('name' => 'search_weight', 'label' => Mage::helper('smile_elasticsearch')->__('Search Weight'), 'class' => 'validate-number validate-greater-than-zero', 'default' => 1), 'is_searchable'); $fieldset->addField('is_used_in_autocomplete', 'select', array('name' => 'is_used_in_autocomplete', 'label' => Mage::helper('smile_elasticsearch')->__('Used in autocomplete'), 'values' => Mage::getModel('adminhtml/system_config_source_yesno')->toOptionArray()), 'search_weight'); $fieldset->addField('is_snowball_used', 'select', array('name' => 'is_snowball_used', 'label' => Mage::helper('smile_elasticsearch')->__('Use language analysis'), 'values' => Mage::getModel('adminhtml/system_config_source_yesno')->toOptionArray()), 'is_used_in_autocomplete'); $fieldset->addField('is_fuzziness_enabled', 'select', array('name' => 'is_fuzziness_enabled', 'label' => Mage::helper('smile_elasticsearch')->__('Enable fuzziness'), 'values' => Mage::getModel('adminhtml/system_config_source_yesno')->toOptionArray()), 'is_snowball_used'); $fieldset->addField('fuzziness_value', 'text', array('name' => 'fuzziness_value', 'label' => Mage::helper('smile_elasticsearch')->__('Fuzziness'), 'class' => 'validate-number validate-number-range number-range-0-1', 'note' => implode('</br>', array(Mage::helper('smile_elasticsearch')->__('A number between 0 and 1.'), Mage::helper('smile_elasticsearch')->__('See doc at <a href="%s" target="_blank">here</a> for more information', 'http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/common-options.html#_string_fields')))), 'is_fuzziness_enabled'); $fieldset->addField('fuzziness_prefix_length', 'text', array('name' => 'fuzziness_prefix_length', 'label' => Mage::helper('smile_elasticsearch')->__('Fuzzy prefix range'), 'class' => 'validate-digits validate-digits-range digits-range-0-9', 'note' => implode('</br>', array(Mage::helper('smile_elasticsearch')->__('A number between 0 and 9.'), Mage::helper('smile_elasticsearch')->__('Min.common prefix between original term and fuzzy matched one.')))), 'fuzziness_value'); if ($attribute->getAttributeCode() == 'name') { $form->getElement('is_searchable')->setDisabled(1); $form->getElement('is_used_in_autocomplete')->setDisabled(1); $form->getElement('is_used_in_autocomplete')->setValue(1); } return $this; }
/** * Retrieve filter array * * @param Enterprise_Search_Model_Resource_Collection $collection * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute * @param string|array $value * @return array */ protected function _getSearchParam($collection, $attribute, $value) { if (empty($value) || isset($value['from']) && empty($value['from']) && isset($value['to']) && empty($value['to'])) { return false; } $localeCode = Mage::app()->getStore()->getConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE); $languageCode = Mage::helper('enterprise_search')->getLanguageCodeByLocaleCode($localeCode); $languageSuffix = $languageCode ? '_' . $languageCode : ''; $field = $attribute->getAttributeCode(); $backendType = $attribute->getBackendType(); $frontendInput = $attribute->getFrontendInput(); if ($frontendInput == 'multiselect') { $field = 'attr_multi_' . $field; } elseif ($frontendInput == 'select' || $frontendInput == 'boolean') { $field = 'attr_select_' . $field; } elseif ($backendType == 'decimal') { $field = 'attr_decimal_' . $field; } elseif ($backendType == 'datetime') { $field = 'attr_datetime_' . $field; if (is_array($value)) { foreach ($value as &$val) { if (!is_empty_date($val)) { $date = new Zend_Date($val, Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT)); $val = $date->toString(Zend_Date::ISO_8601) . 'Z'; } } } else { if (!is_empty_date($value)) { $date = new Zend_Date($value, Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT)); $value = $date->toString(Zend_Date::ISO_8601) . 'Z'; } } } elseif (in_array($backendType, $this->_textFieldTypes)) { $field .= $languageSuffix; } if ($attribute->usesSource()) { $attribute->setStoreId(Mage::app()->getStore()->getId()); foreach ($value as &$val) { $val = $attribute->getSource()->getOptionText($val); } } if (empty($value)) { return array(); } else { return array($field => $value); } }
/** * Define if attribute should be visible for passed user type * * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute * @param string $userType * @return bool */ protected function _isAttributeVisible(Mage_Catalog_Model_Resource_Eav_Attribute $attribute, $userType) { $isAttributeVisible = false; if ($userType == Mage_Api2_Model_Auth_User_Admin::USER_TYPE) { $isAttributeVisible = $attribute->getIsVisible(); } else { $systemAttributesForNonAdmin = array('sku', 'name', 'short_description', 'description', 'tier_price', 'meta_title', 'meta_description', 'meta_keyword'); if ($attribute->getIsUserDefined()) { $isAttributeVisible = $attribute->getIsVisibleOnFront(); } else { if (in_array($attribute->getAttributeCode(), $systemAttributesForNonAdmin)) { $isAttributeVisible = true; } } } return (bool) $isAttributeVisible; }
/** * Append search params to the form * * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute Attribute currently edited * @param Varien_Data_Form $form Form the * * @return Smile_ElasticSearch_Model_Adminhtml_Catalog_Product_Attribute_Edit_Form_Search */ public function addSearchParams($attribute, $form) { $fieldset = $this->_getFieldset($form); $fieldset->addField('search_weight', 'text', array('name' => 'search_weight', 'label' => Mage::helper('smile_elasticsearch')->__('Search Weight'), 'class' => 'validate-number validate-greater-than-zero', 'value' => '1', 'default' => 1), 'is_searchable'); $fieldset->addField('is_used_in_autocomplete', 'select', array('name' => 'is_used_in_autocomplete', 'label' => Mage::helper('smile_elasticsearch')->__('Used in autocomplete'), 'values' => Mage::getModel('adminhtml/system_config_source_yesno')->toOptionArray()), 'search_weight'); $fieldset->addField('is_displayed_in_autocomplete', 'select', array('name' => 'is_displayed_in_autocomplete', 'label' => Mage::helper('smile_elasticsearch')->__('Display in autocomplete'), 'values' => Mage::getModel('adminhtml/system_config_source_yesno')->toOptionArray()), 'is_used_in_autocomplete'); $fieldset->addField('is_snowball_used', 'select', array('name' => 'is_snowball_used', 'label' => Mage::helper('smile_elasticsearch')->__('Use language analysis'), 'values' => Mage::getModel('adminhtml/system_config_source_yesno')->toOptionArray()), 'is_displayed_in_autocomplete'); $fieldset->addField('is_fuzziness_enabled', 'select', array('name' => 'is_fuzziness_enabled', 'label' => Mage::helper('smile_elasticsearch')->__('Enable fuzziness'), 'values' => Mage::getModel('adminhtml/system_config_source_yesno')->toOptionArray()), 'is_snowball_used'); $fieldset->addField('facet_min_coverage_rate', 'text', array('name' => 'facet_min_coverage_rate', 'label' => Mage::helper('smile_elasticsearch')->__('Facet coverage rate'), 'class' => 'validate-digits validate-digits-range digits-range-0-100', 'value' => '90', 'note' => Mage::helper('smile_elasticsearch')->__('Ex: Brand facet will be displayed only if 90% of the product have a brand.')), 'is_fuzziness_enabled'); $fieldset->addField('facets_max_size', 'text', array('name' => 'facets_max_size', 'label' => Mage::helper('smile_elasticsearch')->__('Facet max. size'), 'class' => 'validate-digits validate-greater-than-zero', 'value' => '10', 'note' => implode('</br>', array(Mage::helper('smile_elasticsearch')->__('Max number of values returned by a facet query.')))), 'facet_min_coverage_rate'); $fieldset->addField('facets_sort_order', 'select', array('name' => 'facets_sort_order', 'label' => Mage::helper('smile_elasticsearch')->__('Facet sort order'), 'values' => array(array('value' => Smile_ElasticSearch_Model_Catalog_Layer_Filter_Attribute::SORT_ORDER_COUNT, 'label' => Mage::helper('smile_elasticsearch')->__('Result count')), array('value' => Smile_ElasticSearch_Model_Catalog_Layer_Filter_Attribute::SORT_ORDER_TERM, 'label' => Mage::helper('smile_elasticsearch')->__('Name')), array('value' => Smile_ElasticSearch_Model_Catalog_Layer_Filter_Attribute::SORT_ORDER_RELEVANCE, 'label' => Mage::helper('smile_elasticsearch')->__('Relevance')))), 'facets_max_size'); if ($attribute->getAttributeCode() == 'name') { $form->getElement('is_searchable')->setDisabled(1); $form->getElement('is_used_in_autocomplete')->setDisabled(1); $form->getElement('is_used_in_autocomplete')->setValue(1); } return $this; }
/** * Append search params to the form * * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute Attribute currently edited * @param Varien_Data_Form $form Form the * * @return Smile_ElasticSearch_Model_Adminhtml_Catalog_Product_Attribute_Edit_Form_Search */ public function addSearchParams($attribute, $form) { $fieldset = $this->_getFieldset($form); $fieldset->addField('search_weight', 'text', array('name' => 'search_weight', 'label' => Mage::helper('smile_elasticsearch')->__('Search Weight'), 'class' => 'validate-number validate-greater-than-zero', 'value' => '1', 'default' => 1), 'is_searchable'); $fieldset->addField('is_used_in_autocomplete', 'select', array('name' => 'is_used_in_autocomplete', 'label' => Mage::helper('smile_elasticsearch')->__('Used in autocomplete'), 'values' => Mage::getModel('adminhtml/system_config_source_yesno')->toOptionArray()), 'search_weight'); $fieldset->addField('is_snowball_used', 'select', array('name' => 'is_snowball_used', 'label' => Mage::helper('smile_elasticsearch')->__('Use language analysis'), 'values' => Mage::getModel('adminhtml/system_config_source_yesno')->toOptionArray()), 'is_used_in_autocomplete'); $fieldset->addField('is_fuzziness_enabled', 'select', array('name' => 'is_fuzziness_enabled', 'label' => Mage::helper('smile_elasticsearch')->__('Enable fuzziness'), 'values' => Mage::getModel('adminhtml/system_config_source_yesno')->toOptionArray()), 'is_snowball_used'); $fieldset->addField('fuzziness_value', 'text', array('name' => 'fuzziness_value', 'label' => Mage::helper('smile_elasticsearch')->__('Fuzziness'), 'class' => 'validate-number validate-number-range number-range-0-1', 'value' => '0.75', 'note' => implode('</br>', array(Mage::helper('smile_elasticsearch')->__('A number between 0 and 1.'), Mage::helper('smile_elasticsearch')->__('See doc at <a href="%s" target="_blank">here</a> for more information', 'http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/common-options.html#_string_fields')))), 'is_fuzziness_enabled'); $fieldset->addField('fuzziness_prefix_length', 'text', array('name' => 'fuzziness_prefix_length', 'label' => Mage::helper('smile_elasticsearch')->__('Fuzzy prefix range'), 'class' => 'validate-digits validate-digits-range digits-range-0-9', 'value' => '2', 'note' => implode('</br>', array(Mage::helper('smile_elasticsearch')->__('A number between 0 and 9.'), Mage::helper('smile_elasticsearch')->__('Min.common prefix between original term and fuzzy matched one.')))), 'fuzziness_value'); $fieldset->addField('facet_min_coverage_rate', 'text', array('name' => 'facet_min_coverage_rate', 'label' => Mage::helper('smile_elasticsearch')->__('Facet coverage rate'), 'class' => 'validate-digits validate-digits-range digits-range-0-100', 'value' => '90', 'note' => Mage::helper('smile_elasticsearch')->__('Ex: Brand facet will be displayed only if 90% of the product have a brand.')), 'fuzziness_prefix_length'); $fieldset->addField('facets_max_size', 'text', array('name' => 'facets_max_size', 'label' => Mage::helper('smile_elasticsearch')->__('Facets Max Size'), 'class' => 'validate-digits validate-greater-than-zero', 'value' => '1000', 'note' => implode('</br>', array(Mage::helper('smile_elasticsearch')->__('Max number of values returned by a facet query.')))), 'facet_min_coverage_rate'); $fieldset->addField('facets_sort_order', 'select', array('name' => 'facets_sort_order', 'label' => Mage::helper('smile_elasticsearch')->__('Facet sort order'), 'values' => array(array('value' => Smile_ElasticSearch_Model_Resource_Engine_Elasticsearch_Query_Facet_Terms::SORT_ORDER_COUNT, 'label' => Mage::helper('smile_elasticsearch')->__('By results number')), array('value' => Smile_ElasticSearch_Model_Resource_Engine_Elasticsearch_Query_Facet_Terms::SORT_ORDER_TERM, 'label' => Mage::helper('smile_elasticsearch')->__('By name')))), 'facets_max_size'); if ($attribute->getAttributeCode() == 'name') { $form->getElement('is_searchable')->setDisabled(1); $form->getElement('is_used_in_autocomplete')->setDisabled(1); $form->getElement('is_used_in_autocomplete')->setValue(1); } return $this; }
/** * Return Product Attribute Store Label * Set attribute name like frontend lable for custom attributes (which wasn't defined by Google) * * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute * @param int $storeId Store View Id * @return string Attribute Store View Label or Attribute code */ public function getAttributeLabel($attribute, $storeId) { $attributeId = $attribute->getId(); $frontendLabel = $attribute->getFrontend()->getLabel(); if (is_array($frontendLabel)) { $frontendLabel = array_shift($frontendLabel); } if (!isset($this->_attributeLabels[$attributeId])) { $this->_attributeLabels[$attributeId] = $attribute->getStoreLabels(); } if (isset($this->_attributeLabels[$attributeId][$storeId])) { return $this->_attributeLabels[$attributeId][$storeId]; } else { if (!empty($frontendLabel)) { return $frontendLabel; } else { return $attribute->getAttributeCode(); } } }
/** * Return Product Attribute Store Label * * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute * @param int $storeId Store View Id * @return string Attribute Store View Label or Attribute code */ protected function _getAttributeLabel($attribute, $storeId) { $frontendLabel = $attribute->getFrontend()->getLabel(); if (is_array($frontendLabel)) { $frontendLabel = array_shift($frontendLabel); } if (!$this->_translations) { $moduleName = Mage_Catalog_Model_Entity_Attribute::MODULE_NAME; $separator = Mage_Core_Model_Translate::SCOPE_SEPARATOR; $this->_translations = Mage::getModel('core/translate_string')->load($moduleName . $separator . $frontendLabel)->getStoreTranslations(); } if (isset($this->_translations[$storeId])) { return $this->_translations[$storeId]; } else { return $attribute->getAttributeCode(); } }
/** * Returns attribute field name (localized if needed). * * @depreated not in use * * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute * @param string $locale * @return string */ public function getLocaleAttributeFieldName(Mage_Catalog_Model_Resource_Eav_Attribute $attribute, $locale = null) { if (!$attribute instanceof Mage_Catalog_Model_Resource_Eav_Attribute) { return ''; } $attributeCode = $attribute->getAttributeCode(); if ($attributeCode != 'score' && !in_array($attribute->getBackendType(), $this->_nonLocaleTypes)) { if (null === $locale) { $locale = $this->getLocaleCode(); } $languageCode = $this->getLanguageCodeByLocaleCode($locale); $languageSuffix = $languageCode ? '_' . $languageCode : ''; $attributeCode .= $languageSuffix; } return $attributeCode; }
/** * Retrieve Product Attribute Value * * @param Mage_Catalog_Model_Product $product * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute * @return string */ public function getProductAttributeValue($product, $attribute) { if (!$product->hasData($attribute->getAttributeCode())) { return Mage::helper('Mage_Catalog_Helper_Data')->__('N/A'); } if ($attribute->getSourceModel() || in_array($attribute->getFrontendInput(), array('select', 'boolean', 'multiselect'))) { //$value = $attribute->getSource()->getOptionText($product->getData($attribute->getAttributeCode())); $value = $attribute->getFrontend()->getValue($product); } else { $value = $product->getData($attribute->getAttributeCode()); } return (string) $value == '' ? Mage::helper('Mage_Catalog_Helper_Data')->__('No') : $value; }
/** * Retrieve attribute field name * * @param Mage_Catalog_Model_Resource_Eav_Attribute|string $attribute * @param string $target - default|sort|nav * * @return string|bool */ public function getSearchEngineFieldName($attribute, $target = 'default') { $fieldName = $attribute->getAttributeCode(); if ($fieldName == 'price') { return $this->getPriceFieldName(); } return $fieldName; }
* If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@magentocommerce.com so we can send you a copy immediately. * * DISCLAIMER * * Do not edit or add to this file if you wish to upgrade Magento to newer * versions in the future. If you wish to customize Magento for your * needs please refer to http://www.magentocommerce.com for more information. * * @category Magento * @package Mage_Catalog * @subpackage Test * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com) * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */ /* Create attribute */ $installer = new Mage_Catalog_Model_Resource_Setup('catalog_setup'); $attribute = new Mage_Catalog_Model_Resource_Eav_Attribute(); $attribute->setData(array('attribute_code' => 'attribute_with_option', 'entity_type_id' => $installer->getEntityTypeId('catalog_product'), 'is_global' => 1, 'frontend_input' => 'select', 'is_filterable' => 1, 'option' => array('value' => array('option_0' => array(0 => 'Option Label'))), 'backend_type' => 'int')); $attribute->save(); /* Assign attribute to attribute set */ $installer->addAttributeToGroup('catalog_product', 'Default', 'General', $attribute->getId()); /* Create simple products per each option */ $options = new Mage_Eav_Model_Resource_Entity_Attribute_Option_Collection(); $options->setAttributeFilter($attribute->getId()); foreach ($options as $option) { $product = new Mage_Catalog_Model_Product(); $product->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE)->setAttributeSetId($installer->getAttributeSetId('catalog_product', 'Default'))->setWebsiteIds(array(1))->setName('Simple Product ' . $option->getId())->setSku('simple_product_' . $option->getId())->setPrice(10)->setCategoryIds(array(2))->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)->setStockData(array('use_config_manage_stock' => 1, 'qty' => 5, 'is_in_stock' => 1))->save(); Mage::getSingleton('Mage_Catalog_Model_Product_Action')->updateAttributes(array($product->getId()), array($attribute->getAttributeCode() => $option->getId()), $product->getStoreId()); }
/** * Retrieve Product Attribute Value * * @param Mage_Catalog_Model_Product $product * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute * @return string */ public function getProductAttributeValue($product, $attribute) { if (!$product->hasData($attribute->getAttributeCode())) { return ' '; } if ($attribute->getSourceModel() || in_array($attribute->getFrontendInput(), array('select', 'boolean', 'multiselect'))) { //$value = $attribute->getSource()->getOptionText($product->getData($attribute->getAttributeCode())); $value = $attribute->getFrontend()->getValue($product); } else { $value = $product->getData($attribute->getAttributeCode()); } return $value ? $value : ' '; }
/** * Retrieve attribute field's name for sorting * * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute * * @return string */ public function getAttributeSolrFieldName($attribute) { $attributeCode = $attribute->getAttributeCode(); if ($attributeCode == 'score') { return $attributeCode; } $field = $attributeCode; $backendType = $attribute->getBackendType(); $frontendInput = $attribute->getFrontendInput(); if ($frontendInput == 'multiselect') { $field = 'attr_multi_' . $field; } elseif ($frontendInput == 'select' || $frontendInput == 'boolean') { $field = 'attr_select_' . $field; } elseif ($backendType == 'decimal') { $field = 'attr_decimal_' . $field; } elseif (in_array($backendType, $this->_textFieldTypes)) { $locale = Mage::app()->getStore()->getConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE); $languageCode = $this->getLanguageCodeByLocaleCode($locale); $languageSuffix = $languageCode ? '_' . $languageCode : ''; $field .= $languageSuffix; } return $field; }
* versions in the future. If you wish to customize Magento for your * needs please refer to http://www.magentocommerce.com for more information. * * @category Magento * @package Mage_Catalog * @subpackage integration_tests * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com) * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */ /* Create attribute */ $installer = new Mage_Catalog_Model_Resource_Setup('catalog_setup'); $attribute = new Mage_Catalog_Model_Resource_Eav_Attribute(); $attribute->setData(array('attribute_code' => 'test_configurable', 'entity_type_id' => $installer->getEntityTypeId('catalog_product'), 'is_global' => 1, 'is_user_defined' => 1, 'frontend_input' => 'select', 'is_unique' => 0, 'is_required' => 1, 'is_configurable' => 1, 'is_searchable' => 0, 'is_visible_in_advanced_search' => 0, 'is_comparable' => 0, 'is_filterable' => 0, 'is_filterable_in_search' => 0, 'is_used_for_promo_rules' => 0, 'is_html_allowed_on_front' => 1, 'is_visible_on_front' => 0, 'used_in_product_listing' => 0, 'used_for_sort_by' => 0, 'frontend_label' => array(0 => 'Test Configurable'), 'option' => array('value' => array('option_0' => array(0 => 'Option 1'), 'option_1' => array(0 => 'Option 2')), 'order' => array('option_0' => 1, 'option_1' => 2)), 'backend_type' => 'int')); $attribute->save(); /* Assign attribute to attribute set */ $installer->addAttributeToGroup('catalog_product', 'Default', 'General', $attribute->getId()); /* Create simple products per each option */ $options = new Mage_Eav_Model_Resource_Entity_Attribute_Option_Collection(); $options->setAttributeFilter($attribute->getId()); $attributeValues = array(); $productsData = array(); foreach ($options as $option) { $product = new Mage_Catalog_Model_Product(); $product->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE)->setId($option->getId() * 10)->setAttributeSetId($installer->getAttributeSetId('catalog_product', 'Default'))->setWebsiteIds(array(1))->setName('Configurable Option' . $option->getId())->setSku('simple_' . $option->getId())->setPrice(10)->setTestConfigurable($option->getId())->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE)->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)->setStockData(array('use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1))->save(); $dataOption = array('label' => 'test', 'attribute_id' => $attribute->getId(), 'value_index' => $option->getId(), 'is_percent' => false, 'pricing_value' => 5); $productsData[$product->getId()] = array($dataOption); $attributeValues[] = $dataOption; } $product = new Mage_Catalog_Model_Product(); $product->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE)->setId(1)->setAttributeSetId($installer->getAttributeSetId('catalog_product', 'Default'))->setWebsiteIds(array(1))->setName('Configurable Product')->setSku('configurable')->setPrice(100)->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)->setStockData(array('use_config_manage_stock' => 1, 'is_in_stock' => 1))->setConfigurableProductsData($productsData)->setConfigurableAttributesData(array(array('attribute_id' => $attribute->getId(), 'attribute_code' => $attribute->getAttributeCode(), 'frontend_label' => 'test', 'values' => $attributeValues)))->save();
/** * Add filter by indexable attribute * * @param Mage_CatalogSearch_Model_Advanced $object * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute * @param string|array $value */ public function addIndexableAttributeFilter($object, $attribute, $value) { if (is_string($value) && strlen($value) == 0) { return false; } if ($attribute->getIndexType() == 'decimal') { $table = $this->getTable('catalog/product_index_eav_decimal'); } else { $table = $this->getTable('catalog/product_index_eav'); } $tableAlias = 'ast_' . $attribute->getAttributeCode(); $storeId = Mage::app()->getStore()->getId(); $select = $object->getProductCollection()->getSelect(); $select->distinct(true); $select->join(array($tableAlias => $table), "e.entity_id={$tableAlias}.entity_id AND {$tableAlias}.attribute_id={$attribute->getAttributeId()}" . " AND {$tableAlias}.store_id={$storeId}", array()); if (is_array($value) && (isset($value['from']) || isset($value['to']))) { if (isset($value['from']) && !empty($value['from'])) { $select->where("{$tableAlias}.`value` >= ?", $value['from']); } if (isset($value['to']) && !empty($value['to'])) { $select->where("{$tableAlias}.`value` <= ?", $value['to']); } return true; } $select->where("{$tableAlias}.`value` IN(?)", $value); return true; }
/** * Retrieve array with products counts per price range * * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute * @return array */ public function getPriceRangeFromAttribute($attribute) { $ret = 0; if (!$attribute) { return $ret; } // hook, it is need for 'union' and this attribute defined in the 'price' field if ($attribute->getAttributeCode() == 'price') { $label = 'price'; } else { $label = 'attribute_' . $attribute->getId(); } $vals = array(); $res = $this->getSearchResult(); if (!empty($res['facets'])) { foreach ($res['facets'] as $facet) { if ($facet['attribute'] == $label) { if (!empty($facet['buckets'])) { foreach ($facet['buckets'] as $bucket) { // Example //~ [value] => 1000-2000 //~ [title] => 1000 - 2000 //~ [from] => 1000 //~ [to] => 2000 //~ [count] => 2 return $bucket['to'] - $bucket['from']; } } } } } return $ret; }
/** * Retrieve data to be insterted after processing attribute * * @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute * @param int $storeId * @return array */ protected function getInsertValues($attribute, $storeId) { $collection = Mage::getResourceModel('eav/entity_attribute_option_collection')->setStoreFilter($storeId)->setPositionOrder('asc')->setAttributeFilter($attribute->getId())->load(); $options = $collection->toOptionArray(); $data = array(); foreach ($options as $option) { // Generate url value $urlValue = $this->getHelper()->transliterate($option['label']); // Check if this url key is taken and add -{count} $count = 0; $origUrlValue = $urlValue; do { $found = false; foreach ($data as $line) { if ($line['url_value'] == $urlValue) { $found = true; } } if ($found) { $urlValue = $origUrlValue . '-' . ++$count; } } while ($found); $data[] = array('attribute_code' => $attribute->getAttributeCode(), 'attribute_id' => $attribute->getId(), 'store_id' => $storeId, 'option_id' => $option['value'], 'url_key' => $this->getHelper()->transliterate($attribute->getStoreLabel($storeId)), 'url_value' => $urlValue); } return $data; }
/** * Check if specified attribute is allowed. * List of allowed attributes has higher priority over list of ignored and * blacklisted attributes. * * @param Mage_Catalog_Model_Resource_Eav_Attribute $attr Atrribute * @param array $allow Key based list of allowed attributes * @param array $ignore Key based list of attributes to ignore */ protected function _isAllowedAttribute($attr, $allow = array(), $ignore = array()) { $code = $attr->getAttributeCode(); if ($allow && !isset($allow[$code]) || isset($ignore[$code]) || isset($this->_blacklist[$code])) { return false; } return $attr->getIsVisible() || isset($this->_whitelist[$code]); }