I found a working code that exports the attributes to the browser interface,
like this:
this is the code:'
<?php
chdir(dirname(__FILE__));
require_once 'app/Mage.php';
Mage::app();
umask(0);
$productModel = Mage::getModel('Mage_Catalog_Model_Product');
$categoryModel = Mage::getModel('Mage_Catalog_Model_Category');
$resource = Mage::getModel('core/resource');
$db = $resource->getConnection('core_write');
$attributes = Mage::getSingleton('eav/config')
->getEntityType(Mage_Catalog_Model_Product::ENTITY)->getAttributeCollection();
foreach($attributes as $attribute) {
#if(!$attribute->getIsUserDefined()) continue;
if($attribute->getEntityTypeId() != 4) continue;
if($attribute->getFrontendInput() != 'select' && $attribute->getFrontendInput() != 'multiselect') continue;
echo $attribute->getFrontendLabel(). ' (' . $attribute->getAttributeCode() . ')' . "\n";
foreach ($attribute->getSource()->getAllOptions(false) as $option) {
if(empty($option['label'])) continue;
echo "," . $option['label']."\n";
}
echo "================,=================\n";
}
I would like to export this data to a csv, but I dont know how.
As another question I would also like to export the attribute sets and not only the attributes, is this possible?
Thanks in advance!
Related
I want to display all categories and their subcategories in primary nav menu.
When i hover on cateory, it should display its subcategories.
I want to implement this functionality without loading Mage::getModel('catalog/category') in foreach loop.
as you want to write code in phtml file use below code which create tree structure of category.
<ul>
<?php
$obj = new Mage_Catalog_Block_Navigation();
$storeCategories = $obj->getStoreCategories();
Mage::registry('current_category') ? $currentCategoryId = Mage::registry('current_category')->getId() : $currentCategoryId='';
foreach ($storeCategories as $_category):
?>
<li>
<strong><?php echo $_category->getName(); ?></strong>
<?php $categoryChildren = $_category->getChildren(); ?>
<?php if($categoryChildren->count()) : ?>
<ul>
<?php foreach($categoryChildren as $_categoryChild) : ?>
<?php $_categoryChildModel = Mage::getModel('catalog/category')->load($_categoryChild->getId());?>
<?php $categoryGrandchildren=$_categoryChild->getChildren(); ?>
<li>
<?php
$currentCategoryId===$_categoryChild->getId() ? $bold="style=\"font-weight:bold\"" : $bold='';
echo ' ' . '<a href="' . $_categoryChildModel->getUrl() . '"' . $bold . '>' . $_categoryChild->getName() . '(' . $_categoryChildModel->getProductCollection()->count() . ')</a>';
?>
</li>
<?php if($categoryGrandchildren->count()) : ?>
<?php foreach($categoryGrandchildren as $_categoryGrandchild) : ?>
<?php $_categoryGrandchildModel = Mage::getModel('catalog/category')->load($_categoryGrandchild->getId());?>
<li>
<?php
$currentCategoryId===$_categoryChild->getId() ? $bold="style=\"font-weight:bold\"" : $bold='';
echo ' ' . '<a href="' . $_categoryGrandchildModel->getUrl() . '"' . $bold . '>' . $_categoryGrandchild->getName() . '(' . $_categoryGrandchildModel->getProductCount() . ')</a>';
?>
</li>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach ?>
</ul>
and using css and HTML you can achieve your goal to display sub menu on hover of main menu.
Still let me if you need any other help.
Thanks
ok so I just did this and I figured I'd search to see if anyone was wondering how to achieve this. The trick to this is
Mage::getResourceSingleton('catalog/category')->getAttributeRawValue($categoryEntityId,array('name','level','url_key','path','is_active'),Mage::app()->getStore());
This does not load the category model lets take a look at what it is actually doing.
Go to app/code/core/Mage/Catalog/Model/Resource/Abstract
public function getAttributeRawValue($entityId, $attribute, $store)
{
if (!$entityId || empty($attribute)) {
return false;
}
if (!is_array($attribute)) {
$attribute = array($attribute);
}
$attributesData = array();
$staticAttributes = array();
$typedAttributes = array();
$staticTable = null;
$adapter = $this->_getReadAdapter();
foreach ($attribute as $_attribute) {
/* #var $attribute Mage_Catalog_Model_Entity_Attribute */
$_attribute = $this->getAttribute($_attribute);
if (!$_attribute) {
continue;
}
$attributeCode = $_attribute->getAttributeCode();
$attrTable = $_attribute->getBackend()->getTable();
$isStatic = $_attribute->getBackend()->isStatic();
if ($isStatic) {
$staticAttributes[] = $attributeCode;
$staticTable = $attrTable;
} else {
/**
* That structure needed to avoid farther sql joins for getting attribute's code by id
*/
$typedAttributes[$attrTable][$_attribute->getId()] = $attributeCode;
}
}
/**
* Collecting static attributes
*/
if ($staticAttributes) {
$select = $adapter->select()->from($staticTable, $staticAttributes)
->where($this->getEntityIdField() . ' = :entity_id');
$attributesData = $adapter->fetchRow($select, array('entity_id' => $entityId));
}
/**
* Collecting typed attributes, performing separate SQL query for each attribute type table
*/
if ($store instanceof Mage_Core_Model_Store) {
$store = $store->getId();
}
$store = (int)$store;
if ($typedAttributes) {
foreach ($typedAttributes as $table => $_attributes) {
$select = $adapter->select()
->from(array('default_value' => $table), array('attribute_id'))
->where('default_value.attribute_id IN (?)', array_keys($_attributes))
->where('default_value.entity_type_id = :entity_type_id')
->where('default_value.entity_id = :entity_id')
->where('default_value.store_id = ?', 0);
$bind = array(
'entity_type_id' => $this->getTypeId(),
'entity_id' => $entityId,
);
if ($store != $this->getDefaultStoreId()) {
$valueExpr = $adapter->getCheckSql('store_value.value IS NULL',
'default_value.value', 'store_value.value');
$joinCondition = array(
$adapter->quoteInto('store_value.attribute_id IN (?)', array_keys($_attributes)),
'store_value.entity_type_id = :entity_type_id',
'store_value.entity_id = :entity_id',
'store_value.store_id = :store_id',
);
$select->joinLeft(
array('store_value' => $table),
implode(' AND ', $joinCondition),
array('attr_value' => $valueExpr)
);
$bind['store_id'] = $store;
} else {
$select->columns(array('attr_value' => 'value'), 'default_value');
}
$result = $adapter->fetchPairs($select, $bind);
foreach ($result as $attrId => $value) {
$attrCode = $typedAttributes[$table][$attrId];
$attributesData[$attrCode] = $value;
}
}
}
if (sizeof($attributesData) == 1) {
$_data = each($attributesData);
$attributesData = $_data[1];
}
return $attributesData ? $attributesData : false;
}
As you can see no model loading happening just retrieving specific pieces of info. Also being part of the resource abstract means all catalog resource models (I haven't checked other resource models but, I wouldn't be too surprised to find this in others) have this available.
If you use this in an override of Mage_Catalog_Block_Navigation you can then call all of the info you need about any category without loading the model. To traverse the tree however, you have to do terrible things.
You can use 'path'(explode on /) to easily retrieve parents but you will need to get dirty in order to retrieve children categories so something like this to get Children.
$childrenQuery = "SELECT entity_id FROM catalog_category_entity WHERE path REGEXP '^.*\/" . $categoryId . "\/[[:digit:]]?[[:digit:]]?[[:digit:]]?[[:digit:]]?$'";
$resource = Mage::getSingleton('core/resource');
$readCxn = $resource->getConnection('core/read');
$children = $readCxn->fetchAll($childrenQuery);
if ($children[0]) {
return $children;
} else {
return;
}
The overall difficulty is that all model and resource model functions will expect an instance of a category object to make them all work with just the entity_id is definitely possible just a pain.
So I would not recommend doing this in general the only reason I did this is because the default magento root category in my case was not the actual functional root of categories (fun eh). If you are using a standard root category I would recommend using the Helper functions and retrieving the info from cache.
Either way from here all you have to do complete your functions in Mage_Catalog_Block_Navigation and assemble your menu in a template. And there you go; complete category menu without ever accessing a model->load.
Try This Code
<?php
require_once("app/Mage.php");
Mage::app();
function getChildCategories($category, $First = false) {
$sub = $First ? $category : $category->getChildren();
foreach ($sub as $child) {
$_categories[] = [ "name" => $child->getName(), "id" => $child->getId(), "children" => getChildCategories($child) ];
}
return $_categories;
};
function CategoriesTree($category, $First = false) {
$sub = $First ? $category : $category->getChildren();
echo "<pre>";
foreach ($sub as $child) {
echo $child->getName(); ;
CategoriesTree($child);
}
}
$_categories = Mage::helper('catalog/category')->getStoreCategories();
$categories = getChildCategories($_categories, true);
CategoriesTree($_categories, true);
?>
db_connection.php
<?php
define('_HOST_NAME', 'localhost');
define('_DATABASE_USER_NAME', 'root');
define('_DATABASE_PASSWORD', '');
define('_DATABASE_NAME', 'test_database');
$dbConnection = new mysqli(_HOST_NAME, _DATABASE_USER_NAME, _DATABASE_PASSWORD, _DATABASE_NAME);
if ($dbConnection->connect_error) {
trigger_error('Connection Failed: ' . $dbConnection->connect_error, E_USER_ERROR);
}
$_GLOBAL['dbConnection'] = $dbConnection;
?>
function.php
<?php
function categoryParentChildTree($parent = 0, $spacing = '', $category_tree_array = '') {
global $dbConnection;
$parent = $dbConnection->real_escape_string($parent);
if (!is_array($category_tree_array))
$category_tree_array = array();
$sqlCategory = "SELECT id,name,parent_id FROM tbl_categories WHERE parent_id = $parent ORDER BY id ASC";
$resCategory=$dbConnection->query($sqlCategory);
if ($resCategory->num_rows > 0) {
while($rowCategories = $resCategory->fetch_assoc()) {
$category_tree_array[] = array("id" => $rowCategories['id'], "name" => $spacing . $rowCategories['name']);
$category_tree_array = categoryParentChildTree($rowCategories['id'], ' '.$spacing . '- ', $category_tree_array);
}
}
return $category_tree_array;
}
?>
index.php
<?php
require_once 'db_connection.php';
require_once 'functions.php';
$categoryList = categoryParentChildTree();
foreach($categoryList as $key => $value){
echo $value['name'].'<br>';
}
?>
can anyone help me to translating this so i can use on codeigniter? recursive function
sorry im new on codeigniter
please help
I want to sort media folder files by filename.I tried with
$collection = $this->getCollection($path)
->setCollectDirs(false)
->setCollectFiles(true)
->setCollectRecursively(false)
->setOrder('filename', Varien_Data_Collection::SORT_ORDER_ASC);
but it is not case sensitive.It sorts all upper case words first then lowercase. (Apple,Bat,apple)
Please help !!!
You have to rewrite lib/Varien/Data/Collection/Filesystem.php
and change function
protected function _usort($a, $b)
{
foreach ($this->_orders as $key => $direction) {
$result = $a[$key] > $b[$key] ? 1 : ($a[$key] < $b[$key] ? -1 : 0);
return (self::SORT_ORDER_ASC === strtoupper($direction) ? $result : -$result);
break;
}
}
Please try this:
- create a test.php file in magento root directory.
- change the name of media folder to $sub_dir variable.
<?php
require_once 'app/Mage.php';
Mage::app();
$file = new Varien_Io_File();
$sub_dir = "wysiwyg/new";
$dir = Mage::getBaseDir('media') . DS . $sub_dir . DS;
$file->open(array('path' => $dir));
$fileDetails = $file->ls();
$allFiles = array();
foreach ($fileDetails as $value) {
$allFiles[] = $value['text'];
}
echo "<pre>";
print_r($allFiles);
echo "</pre>";
?>
Attempting to work out a solution to incorporate rel="alternate" tags into a multi-language Mage store. Note: categories and products have customised URL keys for each store
The below code works properly and gives me the correct full URL for a product, in the format of http://www.storename.com/storecode/product.phtml
$url = Mage::getModel('catalog/product')->setStoreId($storeId)->load($product->getId())->getProductUrl();
But the below does not work for categories. It gives the same store code for each URL, instead of the correct store code for each store in the foreach:
$url = Mage::getModel('catalog/category')->setStoreId($storeId)->load($category->getId())->getUrl();
In head.phtml I've so far got:
<?php
$url = '';
$pageType = Mage::app()->getFrontController()->getRequest()->getControllerName();
foreach (Mage::app()->getWebsites() as $website) {
foreach ($website->getGroups() as $group) {
$stores = $group->getStores();
foreach ($stores as $store) {
$storeId = $store->getStoreId();
switch ($pageType) {
case 'product':
$product = Mage::registry('current_product');
$url = Mage::getModel('catalog/product')->setStoreId($storeId)->load($product->getId())->getProductUrl();
break;
case 'category':
$category = Mage::registry('current_category');
/* Below is the code that isn't working properly */
$url = Mage::getModel('catalog/category')->setStoreId($storeId)->load($category->getId())->getUrl();
break;
default:
$url = 'def';
break;
}
echo '<link rel="alternate" href="' . $url . '" hreflang="' . $store->getConfig('general/locale/code') . '"/>' . "\n";
}
}
}
?>
Any advice or help would be appreciated.
Here's the quickest solution I found:
$url = $store->getCurrentUrl(false) . Mage::getModel('catalog/category')->setStoreId($storeId)->load($category->getId())->getUrlKey() . '.html';
So I've been adding tags you add to articles in Joomla!, which works fine. But now I want to show the tags in the article list layout that is default in Joomla.
I found and made an override for the list-layout and tried to add the tags code from a single article layout to the list-layout. Underneath is the code I tried to add in the list-layout. But none of the tags are shown in the layout..
<?php
// set tags
$tags = '';
if (!empty($this->item->tags->itemTags)) {
JLoader::register('TagsHelperRoute', JPATH_BASE . '/components/com_tags/helpers/route.php');
foreach ($this->item->tags->itemTags as $i => $tag) {
if (in_array($tag->access, JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id')))) {
if($i > 0) $tags .= ', ';
$tags .= ''.$this->escape($tag->title).'';
}
}
}
$args['tags'] = $tags;
?>
If this isn't clear, I can try to explain it a different way.
Your php works in the sense that it builds a set of "tag" links but it doesn't actually echo it out to the page. You need to add this line either at the end of your code or somewhere after, where you want to display the tags.
echo $tags;
e.g.
<?php
// set tags
$tags = '';
if (!empty($this->item->tags->itemTags)) {
JLoader::register('TagsHelperRoute', JPATH_BASE . '/components/com_tags/helpers/route.php');
foreach ($this->item->tags->itemTags as $i => $tag) {
if (in_array($tag->access, JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id')))) {
if($i > 0) $tags .= ', ';
$tags .= ''.$this->escape($tag->title).'';
}
}
}
$args['tags'] = $tags;
echo $tags;
?>
I'm not sure what you're using $args for either, it could probably be removed, unless you're using somewhere else.