Introducing
Your new presentation assistant.
Refine, enhance, and tailor your content, source relevant images, and edit visuals quicker than ever before.
Trending searches
<?xml version="1.0"?>
<config>
<modules>
<Mm09_ViewCount>
<active>true</active>
<codePool>local</codePool>
<depends>
</depends>
</Mm09_ViewCount>
</modules>
</config>
<?xml version="1.0"?>
<config>
<modules>
<Mm09_ViewCount>
<version>0.1.0</version>
</Mm09_ViewCount>
</modules>
<global>
</global>
</config>
<?php
$this->startSetup();
$this->addAttribute('customer', 'product_view_count', array(
'label' => 'Product View Count',
'type' => 'int',
'visible' => 1,
'visible_on_front' => 0,
'required' => 0,
'user_defined' => 0, // or: 'group' => 'General',
'default' => 0
));
// $this->run("CREATE TABLE {$this->getTable('my_table')}") (...;
// $this->setConfigData('catalog/frontend/grid_per_page_values', '9,15,30'
$this->endSetup();
[model]_load_after
[model]_save_before
[model]_save_after
controller_action_predispatch_[module]_[controller]_[action]
<?php
class Mm09_ViewCount_Model_Observer
{
public function controllerActionLayoutRenderBeforeCatalogProductView($observer)
{
if (Mage::getSingleton('customer/session')->isLoggedIn())
{
$customer = Mage::getSingleton('customer/session')->getCustomer();
$counter = (int) $customer->getProductViewCount();
$customer->setProductViewCount($counter +1)->save();
}
}
}
public function catalogProductLoadAfter($observer)
{
$product = $observer->getEvent()->getProduct();
// Process product data
}
public function checkoutCartProductAddAfter($observer)
{
$product = $observer->getEvent()->getProduct();
$quoteItem = $observer->getEvent()->getQuoteItem();
if ($product->getIsEpic())
{
Mage::getSingleton('customer/session')->getCustomer()
->setAura(self::AURA_TYPE_IMBA);
}
}
<?php
class Mm09_ViewCount_Block_Adminhtml_Customer_Grid
extends Mage_Adminhtml_Block_Customer_Grid
{
protected function _preparePage()
{
$this->getCollection()->addAttributeToSelect('product_view_count');
return parent::_preparePage();
}
protected function _prepareColumns()
{
parent::_prepareColumns();
$actionColum = $this->getColumn('action');
unset($this->_columns['action']);
$this->addColumn('product_view_count', array(
'header' => Mage::helper('customer')->__('Product View Count'),
'width' => '90',
'index' => 'product_view_count',
));
$this->_columns['action'] = $actionColum;
return $this;
}
}
<catalog_product_view/>
<checkout_cart_index/>
<checkout_onepage_index/>
<checkout_onepage_review/>
und viele mehr...
<default/>
<customer_account/>
<customer_logged_in/>
<customer_logged_out/>
<PRODUCT_TYPE_configurable>
und viele mehr...
<?php
class Mm09_ViewCount_Block_Viewcount extends Mage_Core_Block_Template
{
protected function _toHtml()
{
if (! Mage::getSingleton('customer/session')->isLoggedIn()) return '';
return parent::_toHtml();
}
public function getProductsViewedText()
{
$count = (int) Mage::getSingleton('customer/session')->getCustomer()->getProductViewCount();
if ($count == 1)
{
$text = 'You have viewed %d product page.';
}
else
{
$text = 'You have viewed %d product pages.';
}
return $this->__($text, $count);
}
}
<div class="box base-mini viewcount">
<div class="head">
<h4><?php echo $this->__('Products Viewed') ?></h4>
</div>
<div class="content">
<p><?php echo $this->getProductsViewedText() ?></p>
</div>
</div>
Nicht brauchbar: print_r($object); var_dump($object);
Logging Einschalten: System > Konfiguration > Entwickler
Besser $object->getData() oder $object->toArray();
Mage::log($object->getData());
Mage::log(__METHOD__);
Mage::log(get_class($object));
Magic Getter & Magic Setter:
$customer->getProductViewCount();
Varien_Object::__get()
Varien_Object::getData('product_view_count')
$customer->setProductViewCount($count +1)
Varien_Object::__set()
Varien_Object::setData('product_view_count', 2)
Freier Programmierer
vinai@netzarbeiter.de
+49 173 - 31 33 444
Nicht alle Events in <frontend> und <adminhtml> verfügbar, manche gibt es nur in <global>
(Was ist das?)
Konvention: .../Mm09/MostViewed/Model/Observer.php
Webdeveloper-Dream of being a real Programmer
Wird gemapped auf
app/code/local/Mm09/ViewCount/Model/
Oft mit mehreren Parametern
Liste mit Events: http://www.magentocommerce.com/wiki/customizing_magento_using_event-observer_method#Events
Der Name für die Methode kann frei vergeben werden...
<?xml version="1.0"?>
<config>
<modules>
<Mm09_ViewCount>
<version>0.1.0</version>
</Mm09_ViewCount>
</modules>
<global>
<models>
<ViewCount>
<class>Mm09_ViewCount_Model</class>
</ViewCount>
</models>
<resources>
<ViewCount_setup>
<setup>
<module>Mm09_ViewCount</module>
<class>Mage_Eav_Model_Entity_Setup</class>
</setup>
</ViewCount_setup>
</resources>
</global>
<frontend>
<events>
<controller_action_layout_render_before_catalog_product_view>
<observers>
<ViewCount>
<type>singleton</type>
<class>ViewCount/observer</class>
<method>controllerActionLayoutRenderBeforeCatalogProductView</method>
</ViewCount>
</observers>
</controller_action_layout_render_before_catalog_product_view>
</events>
</frontend>
</config>
Check: ist das der längste Event Name?
Kann frei vergeben werden...
muss für den jeweiligen Event unique sein
Cron-Jobs
Mage::log() broken
Cache
Eintrag löschen = Install Script ausführen
Version herntersetzen = Upgrade Script(e) nochmal ausführen
<version>0.1.0</version>
...
<version>0.1.1</version>
...
<version>0.1.2</version>
varchar, int, decimal, text, datetime
$installer = $this;
Konvention... aber der Sinn?
$this = <class>Mage_Eav_Model_Entity_Setup</class>
.../sql/ViewCount_setup/mysql4-upgrade-0.1.0-0.1.1.php
.../sql/ViewCount_setup/mysql4-install-0.1.0.php
-------------------------------------------------------- (siehe config.xml) --------------------------------------------------------
Upgrade Script:
Mm09/ViewCount/sql/ViewCount_setup/
Install-Script:
Name des Moduls
Name des Setup Verzeichnisses
Product View Count hier einfügen
Modul Version
EAV - nicht alle Attribute werden automatisch geladen!
Neue Spalte einfügen
Letzte Spalte entfernen...
(weil es hübscher ist)
Action Spalte an letzter
Stelle wieder einfügen
mm09.2 in ffm
02.11.2009
von Vinai Kopp
Kurzbezeichnung aus
app/code/core/Mage/Adminhtml/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Mm09_ViewCount>
<version>0.1.0</version>
</Mm09_ViewCount>
</modules>
<global>
<models>
<ViewCount>
<class>Mm09_ViewCount_Model</class>
</ViewCount>
</models>
<blocks>
<adminhtml>
<rewrite>
<customer_grid>Mm09_ViewCount_Block_Adminhtml_Customer_Grid</customer_grid>
</rewrite>
</adminhtml>
</blocks>
<resources>
<ViewCount_setup>
<setup>
<module>Mm09_ViewCount</module>
<class>Mage_Eav_Model_Entity_Setup</class>
</setup>
</ViewCount_setup>
</resources>
</global>
<frontend>
<events>
<controller_action_layout_render_before_catalog_product_view>
<observers>
<ViewCount>
<type>singleton</type>
<class>ViewCount/observer</class>
<method>controllerActionLayoutRenderBeforeCatalogProductView</method>
</ViewCount>
</observers>
</controller_action_layout_render_before_catalog_product_view>
</events>
</frontend>
</config>
ViewCount wird gemapped auf
app/code/local/Mm09/ViewCount/Block/
<?xml version="1.0"?>
<config>
<modules>
<Mm09_ViewCount>
<version>0.1.0</version>
</Mm09_ViewCount>
</modules>
<global>
<models>
<ViewCount>
<class>Mm09_ViewCount_Model</class>
</ViewCount>
</models>
<blocks>
<ViewCount>
<class>Mm09_ViewCount_Block</class>
</ViewCount>
<adminhtml>
<rewrite>
<customer_grid>Mm09_ViewCount_Block_Adminhtml_Customer_Grid</customer_grid>
</rewrite>
</adminhtml>
</blocks>
<resources>
<ViewCount_setup>
<setup>
<module>Mm09_ViewCount</module>
<class>Mage_Eav_Model_Entity_Setup</class>
</setup>
</ViewCount_setup>
</resources>
</global>
<frontend>
<events>
<controller_action_layout_render_before_catalog_product_view>
<observers>
<ViewCount>
<type>singleton</type>
<class>ViewCount/observer</class>
<method>controllerActionLayoutRenderBeforeCatalogProductView</method>
</ViewCount>
</observers>
</controller_action_layout_render_before_catalog_product_view>
</events>
<layout>
<updates>
<ViewCount>
<file>viewcount.xml</file>
</ViewCount>
</updates>
</layout>
</frontend>
</config>
Frei zu vergeben aber einmalig
innerhalb <frontend> oder <adminhtml>
Konvention: lower-case für Layout und Template Dateien
"Layout Update Handle"
Innerhalb Layout XML
"name"
(Einmaling im zu Rendernden Layout)
Definiert in der layout/page.xml
Innerhalb PHP Block
Klassen und Templates
"as"
(Einmaling innerhalb des Parent Blocks)
<?xml version="1.0"?>
<layout version="0.1.0">
<default>
<reference name="right">
<block
type="ViewCount/viewcount"
name="viewcount"
as="viewcount"
template="mm09/viewcount/sidebar.phtml"/>
</reference>
</default>
</layout>
Mm09_ViewCount_Block_Viewcount
app/code/local/Mm09/ViewCount/Block/Viewcount.php
ein paar Beispiele...
app/
code/
local/
Mm09/
ViewCount/
[modul]_[controller]_[action]
spezialfälle
Yay! sprintf() Formatierungen in der Übersetzungs Methode!
app/design/frontend/default/default/template/mm09/viewcount/sidebar.phtml
Deutsches offizielles Magento Forum: http://www.magentocommerce.com/boards/viewforum/25/
IRC irc.freenode.net Channel #magento-de (deutsch) oder #magento (english)
Dokumentation?
t3n Magazin: regelmäßig Magento Artikel von mir und anderen
Liste mit Magento-Development Blogs http://blog.velite.de/essential-magento-developer-blogs/
... Zeitlos und immer Richtig: use the Source...!
Bücher:
Online-Shops mit Magento (Roman Zenner)
Magento - Das Handbuch für Entwickler (Visions new Media Entwickler)