Magento: Parent Role id ‚G4‘ does not exist (or other ids)

2. September 2011 at 11:35

Magento reads the rows from the database table admin_role in the wrong“ order. That is it reads the user entry before the parent group (the role) is loaded. This happends because the user rows has a tree_level = 1, when they should have tree_level = 2 or more.

Solution:

To fix a broken admin-interface, run the following query in the magento database:
UPDATE admin_role SET tree_level = 2 WHERE role_type = “U”;

To prevent the error from happening again:
Open up the file app/code/core/Mage/Admin/Model/Mysql4/User.php

On line 162 (or close to that line) you find a row that says:
‘tree_level’ => $row[’tree_level’] + 1,

change this line to:
‘tree_level’ => $row[’tree_level’] + 2,

Magento: Page Cache anpassen

23. August 2011 at 11:22

Das Caching könnt ihr in Magento wie folgt beeinflussen:

protected function _construct()
{
$this->addData(array(
'cache_lifetime'    => 900,
'cache_tags'        => array(Mage_Catalog_Model_Product::CACHE_TAG),
'cache_key'            => $this->getCacheKey()
));
}
public function getCacheKey()
{
return $this->getRequest()->getRequestUri().$this->getCacheCurrencyCode();
}
//retreive current currency code
public function getCacheCurrencyCode()
{
return Mage::app()->getStore()->getCurrentCurrencyCode();
}

De-Aktivieren könnt ihr das Caching wie folgt:

protected function _construct()
{
$this->addData(array(
'cache_lifetime'    => null,
'cache_tags'        => array(Mage_Catalog_Model_Product::CACHE_TAG)
));
}

Magento: system.xml um weitere Felder erweitern – Daten-Typen

19. Juli 2011 at 15:44

Folgende Typen / Möglichkeiten gibt es in Magento um im Backend Datums-Typen Anzuzeigen (weg über system.xml):

<use_calendar translate="label">
<label>Use JavaScript Calendar</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</use_calendar>
<date_fields_order translate="label">
<label>Date Fields Order</label>
<frontend_type>select</frontend_type>
<frontend_model>adminhtml/catalog_form_renderer_config_dateFieldsOrder</frontend_model>
<sort_order>2</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</date_fields_order>
<time_format translate="label">
<label>Time Format</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_catalog_timeFormat</source_model>
<sort_order>3</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</time_format>
<year_range translate="label comment">
<label>Year Range</label>
<comment>Use four-digit year format.</comment>
<frontend_type>text</frontend_type>
<frontend_model>adminhtml/catalog_form_renderer_config_yearRange</frontend_model>
<sort_order>4</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</year_range>

Magento: Category & Store Group & Store View über Setup Script anlegen

29. Juni 2011 at 11:12

magento ecommerce logo

Der folgende Script zeigt wie automatisiert über ein PHP-Setup-Script in Magento eine Category angelegt werden kann, welche dann als Default-Category einer neuen Store-Group zugewiesen wird, welche samt einer neuen Store-View angelegt wird.

$installer = $this;
$installer->startSetup();
				$data = array(
								'name' 							=> 'New Category',
								'url_key'    				=> 'root', //'de',
				      	'description' 			=> 'test category',
				      	'meta_description' 	=> 'test category',
								'meta_keywords'			=> 'categories_meta',
								'display_mode'      => Mage_Catalog_Model_Category::DM_PRODUCT,
								'default_sort_by'   => Mage::getModel("catalog/category")->getDefaultSortBy(),
				      	'available_sort_by' => Mage::getModel("catalog/category")->getDefaultSortBy(),
				      	'is_active'					=> 1,
						    'is_anchor'         => 0,
						    'include_in_menu'   => 0,
						    'parent_id'         => 1,
						    'path'              => 1, //Mage::getModel("catalog/category")->load(1)->getPath(),
						    'attribute_set_id'  => Mage::getModel("catalog/category")->getDefaultAttributeSetId(),
								'display_mode'	=> array( '0' => 'show_products'),
								'custom_design_apply' => 1
				        );
				Mage::app()->getStore()->load(0);
				$cat = Mage::getModel('catalog/category');
				$cat->addData($data);
				$parentId = Mage_Catalog_Model_Category::TREE_ROOT_ID;
			  $parentCategory = Mage::getModel('catalog/category')->load($parentId);
			  $cat->setPath($parentCategory->getPath());
			//	$cat->setStoreId(Mage::app()->getStore()->getId());
				$cat->setStoreId(0);
				$cat->setAttributeSetId($cat->getDefaultAttributeSetId());
				try {
				    $cat->save();
				    $newCategoryId = $cat->getId();
				} catch (Exception $e) {
				    Mage::logException($e->getMessage());
				    return;
				}
			}
		if($newCategoryId != '') {
			$groupModel = Mage::getModel('core/store_group');
	    $group = array(
	        'website_id' => 1,
	        'name' => 'New Store Gropu',
	        'root_category_id' => $newCategoryId,
	    );
	    $groupModel->setData($group);
	    $groupModel->setId(null);
	    $groupModel->save();
	    Mage::dispatchEvent('store_group_save', array('group' => $groupModel));
	    $storeModel = Mage::getModel('core/store');
	    $store = array(
	        'group_id' => $groupModel->getId(),
	        'name' => 'New Store',
	        'code' => 'new_store',
	        'is_active' => 1,
	        'sort_order' => 2,
	    );
	    $storeModel->setData($store);
	    $storeModel->setId(null);
	    $eventName = 'store_add';
	    $groupModel = Mage::getModel('core/store_group')->load($storeModel->getGroupId());
	    $storeModel->setWebsiteId($groupModel->getWebsiteId());
	    $storeModel->save();
	    Mage::app()->reinitStores(); // or Mage::app()->getConfig()->reinit();
	    Mage::dispatchEvent($eventName, array('store'=>$storeModel));
		}
$installer->endSetup();
$installer->installEntities();
$installer = $this;
$installer->startSetup(); 

$mobileCategory = Mage::getModel(‚catalog/category‘)->getCollection()
->addAttributeToSelect(‚id‘)
->addAttributeToFilter(’name‘, ‚mobile‘)
->addAttributeToSort(‚id‘, ‚ASC‘)
->distinct(true)
->load();

$mobileCategoryId = “;
if(count($mobileCategory) > 0) {
foreach ($mobileCategory as $value) {
$mobileCategoryId = $value->getId();
}
} else {
$data = array(
’name‘                             => ‚Mobile‘,
‚url_key‘                    => ‚root‘, //’de‘,
‚description‘             => ‚test category‘,
‚meta_description‘     => ‚test category‘,
‚meta_keywords‘            => ‚categories_meta‘,
‚display_mode‘      => Mage_Catalog_Model_Category::DM_PRODUCT,
‚default_sort_by‘   => Mage::getModel(„catalog/category“)->getDefaultSortBy(),
‚available_sort_by‘ => Mage::getModel(„catalog/category“)->getDefaultSortBy(),
‚is_active‘                    => 1,
‚is_anchor‘         => 0,
‚include_in_menu‘   => 0,
‚parent_id‘         => 1,
‚path‘              => 1, //Mage::getModel(„catalog/category“)->load(1)->getPath(),
‚attribute_set_id‘  => Mage::getModel(„catalog/category“)->getDefaultAttributeSetId(),
‚mdm_display_mode‘    => array( ‚0‘ => ’show_products‘),
‚custom_design_apply‘ => 1
);

Mage::app()->getStore()->load(0);
$cat = Mage::getModel(‚catalog/category‘);
$cat->addData($data);
$parentId = Mage_Catalog_Model_Category::TREE_ROOT_ID;
$parentCategory = Mage::getModel(‚catalog/category‘)->load($parentId);
$cat->setPath($parentCategory->getPath());
//    $cat->setStoreId(Mage::app()->getStore()->getId());
$cat->setStoreId(0);
$cat->setAttributeSetId($cat->getDefaultAttributeSetId());

try {
$cat->save();
$mobileCategoryId = $cat->getId();

} catch (Exception $e) {
Mage::logException($e->getMessage());
return;
}
}

if($mobileCategoryId != “) {
$groupModel = Mage::getModel(‚core/store_group‘);
$group = array(
‚website_id‘ => 1,
’name‘ => ‚Mobile‘,
‚root_category_id‘ => $mobileCategoryId, // 119

);

$groupModel->setData($group);
$groupModel->setId(null);
$groupModel->save();

Mage::dispatchEvent(’store_group_save‘, array(‚group‘ => $groupModel));

$storeModel = Mage::getModel(‚core/store‘);

$store = array(
‚group_id‘ => $groupModel->getId(),
’name‘ => ‚Mobile‘,
‚code‘ => ‚mobile‘,
‚is_active‘ => 1,
’sort_order‘ => 2,
);
$storeModel->setData($store);
$storeModel->setId(null);
$eventName = ’store_add‘;

$groupModel = Mage::getModel(‚core/store_group‘)->load($storeModel->getGroupId());
$storeModel->setWebsiteId($groupModel->getWebsiteId());
$storeModel->save();

Mage::app()->reinitStores(); // or Mage::app()->getConfig()->reinit();

Mage::dispatchEvent($eventName, array(’store’=>$storeModel));
}

$installer->endSetup();
$installer->installEntities();

Magento: Per PHP Setup Script neue Store View anlegen

28. Juni 2011 at 11:01

Per PHP Script eine neue Store View über z.B. Setup-Install-Script anlegen?
So geht’s:

$storeModel = Mage::getModel('core/store');
 $store = array(
 'group_id' => 1,
 'name' => 'New Store Name',
 'code' => 'store_xy',
 'is_active' => 1,
 'sort_order' => 2,
 );
 $storeModel->setData($store);
 $storeModel->setId(null);
 $eventName = 'store_add';
 $groupModel = Mage::getModel('core/store_group')->load($storeModel->getGroupId());
 $storeModel->setWebsiteId($groupModel->getWebsiteId());
 $storeModel->save();
 Mage::app()->reinitStores(); // or Mage::app()->getConfig()->reinit();
 Mage::dispatchEvent($eventName, array('store'=>$storeModel));

Magento: Artikel filtern nach bestimten Parametern

17. Juni 2011 at 10:49

Artikel könnt ihr wie folgt nach bestimmten Parametern filtern. Folgendes Beispiel zeigt die Filterung anhand des Preises aus bestimmten Unterkategorien:

<?php
$model = Mage::getModel('catalog/product');     // Direktzugriff auf Produkt als Methode
$collection = $model -> getCollection();    //Zugrif auf Eigenschaft
$collection -> addAttributeToSelect('name');    //Attributaktivierung - Name
//$collection  -> load();                      //Zeigt alle Artikel
$collection -> addFieldToFilter('price',array('from'=>'0','to'=>'40')); //Filtert alle Artikel von 0 bis 40€.
$collection -> getSelect();
$collection -> setOrder('price', 'ASC');
?>
<ul><?php foreach($collection as $product) : ?>
 <li>
 <?php echo $product ->getPrice(); echo "&euro; - "; ?>
 <?php echo $product ->getName(); echo " - ";?>
 <?php echo $product ->getColor(); echo " ";?>
 </li>
<?php  endforeach; ?>
</ul>

Magento – add produkt about remote URL in cart – Magento HowTo

18. Mai 2011 at 14:39

With this url structure you can add simply products to your cart in Magento:

http://www.yourdomain.com/checkout/cart/add?product=[id]&qty=[qty]

The [id] is the Magento ID of the product and [qty] is the Quantity of the product you wish to add.

Add a simple product with custom options to your cart in Magento:

http://www.yourdomain.com/checkout/cart/add?product=[id]&qty=[qty]&options[id]=[value]

The [id] is the Magento ID of the product and [qty] is the Quantity of the product you wish to add and the options[id]=[value] select your option of this product (color, size, etc.).

Magento Online Doku / Documentation

18. Mai 2011 at 14:33

Eine strukturierte Online Doku inkl. Datenstruktur, Klassenhierarchie, etc. findet ihr unter folgendem Link:

docs.databyte.at/magento/community/1.5/

Magento: Session Success or Failure Message after redirect – Magento HowTo

27. April 2011 at 10:14

Add a Success, Info or Error Message to the session and save this for re-direction in Magento

//A Success Message
 Mage::getSingleton('checkout/session')->addSuccess("Your cart has been updated successfully!");
 //A Error Message
 Mage::getSingleton('checkout/session')->addError("Your cart has been updated successfully!");
 //A Info Message (See link below)
 Mage::getSingleton('checkout/session')->addNotice("This is just a FYI message...");
 //These two lines are required to get it to work
 session_write_close(); //THIS LINE IS VERY IMPORTANT!
 $this->_redirect('checkout/cart');

Magento: PHP-Code in Content-Seiten (CMS) einbinden – HowTo

26. April 2011 at 18:00

Aus Sicherheitsgründen ist es nicht gestattet in Magento-CMS-Pages direkt PHP-Code einzubinden.

Über dem folgenden Weg ist dies möglich:

Im Template Ordner muss dazu eine Datei zum einbinden vorhanden oder angelegt werden. Z.B. die test.phtml welche im Ordner  /app/design/frontend/default/default/test/test.phtml abgelegt wird.

In dieser Datei kann, wie in den phtml-template-files üblich, php-code aufgerufen werden, z.b.:

<strong><?php echo "Hello World"; ?></strong>

In der CMS-Page ruft ihr diese Template-Datei wie folgt auf:

{{block type="core/template" template="test/test.phtml"}}