Magento neue Kategorie mit CMS Block hinzufügen (add category programmatically)

Gepostet am März 5, 2015

Im folgenden Beispiel möchte ich zeigen, wie man per Script einen Block und eine Kategorie erstellt. Dies macht man z.B. in einem Data-Script bei der Installation einer selbst geschriebenen Extension.

1
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* ########## CREATE BLOCK ############## */
 
$store = Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
 
// Category (Brands)
$content = 
'<table class="brand-table">'
  .'<tr>'
    .'<td><a href="{{store direct_url="marken/jeld-wen.html"}}"><img src="{{media_url="wysiwyg/brands/Jeld_Wen.jpg"}}" alt="Jeld Wen" /></a></td>'
    .'<td><a href="{{store direct_url="marken/laba.html"}}"><img src="{{media_url="wysiwyg/brands/Laba.jpg"}}" alt="Laba" /></a></td>'
    .'<td><a href="{{store direct_url="marken/karcher-design.html"}}"><img src="{{media_url="wysiwyg/brands/Karcher_Design.jpg"}}" alt="Karcher Design" /></a></td>'
  .'</tr>'
  .'<tr>'
    .'<td><a href="{{store direct_url="marken/kelan.html"}}"><img src="{{media_url="wysiwyg/brands/Kelan.jpg"}}" alt="Kelan" /></a></td>'
    .'<td><a href="{{store direct_url="marken/deine-tuer.html"}}"><img src="{{media_url="wysiwyg/brands/Deine_Tuer.jpg"}}" alt="Deine Tür" /></a></td>'
    .'<td><a href="{{store direct_url="marken/concept.html"}}"><img src="{{media_url="wysiwyg/brands/Concept.jpg"}}" alt="Concept" /></a></td>'
  .'</tr>'
  .'<tr>'
    .'<td><a href="{{store direct_url="marken/lenz-design.html"}}"><img src="{{media_url="wysiwyg/brands/LenzDesign.jpg"}}" alt="Lenz Design" /></a></td>'
    .'<td><a href="{{store direct_url="marken/piktura.html"}}"><img src="{{media_url="wysiwyg/brands/Piktura.jpg"}}" alt="Piktura" /></a></td>'
    .'<td>&nbsp;</td>'
  .'</tr>'
.'</table>';
 
$block = Mage::getModel('cms/block');
$block->setTitle('Marken');
$block->setIdentifier('brands');
$block->setStores(0);
$block->setIsActive(1);
$block->setContent($content);
$block->save();
$blockId = $block->getId();
 
 
/* ########## CREATE FIRST CATEGORY ############## */
 
$parentCategory = Mage::getModel('catalog/category')->load(2); // set your parent cat-id
$category = new Mage_Catalog_Model_Category();
$category->setName('Marken');
$category->setUrlKey('marken');
$category->setIsActive(1);
$category->setDisplayMode('PAGE');
$category->setLandingPage($blockId);
$category->setPageLayout('one_column');
$category->setCustomLayoutUpdate(
'<reference name="root">
    <action method="setTemplate"><template>page/1column.phtml</template></action>
</reference>
 
<catalog_category_default translate="label">
    <remove name="left"/>
</catalog_category_default>');
$category->setIsAnchor(0); //for active achor
$category->setStoreId($store);
$category->setPosition(1);
$category->setPath($parentCategory->getPath());
$category->save();
$parentId = $category->getId();
 
 
/* ########## CREATE BLOCK FOR SEC. CATEGORY ############## */
 
// Category (Brand: Jeld Wen)
$content = 
'<div class="teaser brands">
    <div><h3><b>Marken</b> | Jeld Wen </h3></div>
    <img src="{{media_url="wysiwyg/brands/Banner_Logo_Jeld_Wen.jpg"}}" alt="Jeld Wen" />
</div>';
 
$block = Mage::getModel('cms/block');
$block->setTitle('Marken - Jeld Wen');
$block->setIdentifier('brands_jeld_wen');
$block->setStores(0);
$block->setIsActive(1);
$block->setContent($content);
$block->save();
$blockId = $block->getId();
 
 
/* ########## CREATE SEC. CATEGORY ############## */
 
$parentCategory = Mage::getModel('catalog/category')->load($parentId);
$category = new Mage_Catalog_Model_Category();
$category->setName('Jeld Wen');
$category->setUrlKey('jeld-wen');
$category->setIsActive(1);
$category->setPosition(1);
$category->setDisplayMode('PRODUCTS_AND_PAGE');
$category->setLandingPage($blockId);
$category->setIsAnchor(1); //for active achor
$category->setStoreId($store);
$category->setPath($parentCategory->getPath());
$category->save();

Leave a Reply

 

Posts of Current Category



Facebook: