Der Bereich Shopware Labs ist die Plattform für alle Entwickler. Hier findet man technische Dokumentationen und zahlreiche Tipps und Tricks rund um das Thema Programmieren. In dieser Rubrik stellen außerdem die Entwickler der shopware AG neue und experimentelle Lösungsansätze vor. Neue Funktionen, die in dieser Rubrik bereitgestellt werden, sind teilweise auch für zukünftige Releases geplant. Die Funktionen können dann ohne Programmierkenntnisse zukünftig direkt im Shopware Backend konfiguriert werden oder werden über Plugins bereitgestellt. Informationen über neue, geplante Funktionen finden Sie in unserer Roadmap.
Bitte beachten Sie, dass die hier bereitgestellten Lösungsansätze nicht offiziell supportet werden und nur eingebaut werden sollten, sofern Sie über das entsprechende, technische Wissen verfügen.
Beispiel-Plugins Shopware Developer Schulung
0 KommentareInhaltsverzeichnis
- 1 Allgemein
- 2 Example 1: Hello World mit Ausgabe Firebug und simpler Aufruf des Controllers
- 2.1 Shopware\Plugins\Local\Frontend\Example1\Bootstrap.php
- 2.2 Shopware\Plugins\Local\Frontend\Example1\Controller.php
- 3 Example 2: Hello World mit Ausgabe Template
- 3.1 Shopware\Plugins\Local\Frontend\Example2\Bootstrap.php
- 3.2 Shopware\Plugins\Local\Frontend\Example2\plugin.tpl
- 4 Example 3: XML-Datei nach Bestellabschluss im Filesystem ablegen und Event und Hook-Beispiel
- 4.1 Shopware\Plugins\Local\Frontend\Example3\Boostrap.php
- 5 Example 4: Hello World im Backend
- 5.1 Shopware\Plugins\Local\Frontend\Example4\Boostrap.php
- 5.2 Shopware\Plugins\Local\Frontend\Example4\Example4.php
- 5.3 Shopware\Plugins\Local\Frontend\Example4\templates\backend\plugins\Example4\index.tpl
- 5.4 Shopware\Plugins\Local\Frontend\Example4\templates\backend\plugins\Example4\skeleton.tpl
- 6 Example 5: "Kunden haben gerade gekauft" als Slider auf Startseite
- 6.1 Shopware\Plugins\Local\Frontend\Example5\Bootstrap.php
- 6.2 Shopware\Plugins\Local\Frontend\Example5\Example5.php
- 6.3 Shopware\Plugins\Local\Frontend\Example5\frontend\plugins\Example5\index.tpl
- 7 Example 6: Eigener Frontend-Controller mit Anbindung API
- 7.1 Shopware\Plugins\Local\Frontend\Example6\Bootstrap.php
- 7.2 Shopware\Plugins\Local\Frontend\Example6\Example6.php
- 8 Example 7: Eigener Cronjob als Plugin
- 8.1 Shopware\Plugins\Local\Frontend\Example7\Bootstrap.php
- 9 Example 8: PDF-Rechnung nach Bestellung per eMail
- 9.1 Shopware\Plugins\Local\Frontend\Example8\Bootstrap.php
Allgemein
Auf den Schulungen zum "Shopware Certified Developer" werden eine Reihe von Beispiel-Plugins implementiert, die wir auf dieser Seite bereitstellen.
Example 1: Hello World mit Ausgabe Firebug und simpler Aufruf des Controllers
Shopware\Plugins\Local\Frontend\Example1\Bootstrap.php
<?php /** * This is the standard Bootstrap Class of the Plugin. * It is core element of each plugins and it overrides the needed * methods out of the Shopware_Components_Plugin_Bootstrap * * @copyright Copyright (c) 2011, Shopware AG * @author $Author$ * @package Shopware * @subpackage Components_Plugin * @creation_date 24.11.11 15:30 * @version $Id$ */ class Shopware_Plugins_Frontend_Example1_Bootstrap extends Shopware_Components_Plugin_Bootstrap { /** * this derived method is called automatically each time the plugin will be installed * * @return bool | if false is return the installation will failed */ public function install() { $event = $this->createEvent( 'Enlight_Controller_Action_PostDispatch_Frontend_Listing', 'onPostDispatchListing' ); $this->subscribeEvent($event); $event = $this->createEvent( 'Enlight_Controller_Dispatcher_ControllerPath_Frontend_Example1', 'onGetControllerPathFrontend' ); $this->subscribeEvent($event); return true; } /** * just points to the path of this controller * * @static * @param Enlight_Event_EventArgs $args * @return string */ static function onGetControllerPathFrontend(Enlight_Event_EventArgs $args) { return dirname(__FILE__).'/Controller.php'; } /** * is called after the listing action is finished * * @static * @param Enlight_Event_EventArgs $args * @return void */ static function onPostDispatchListing(Enlight_Event_EventArgs $args) { $request = $args->getSubject()->Request(); $response = $args->getSubject()->Response(); $view = $args->getSubject()->View(); //Debugging; hierfür muss Firebug und die Extension Firephp im Browser installiert sein. // Shopware()->Log()->log("Hello World",Zend_Log::INFO); } }
Shopware\Plugins\Local\Frontend\Example1\Controller.php
<?php /** * This is a standard Controller Class. * This class provides advanced functions for the plugin * * @copyright Copyright (c) 2011, Shopware AG * @author $Author$ * @package Shopware * @subpackage Controllers_Frontend * @creation_date 24.11.11 15:30 * @version $Id$ */ class Shopware_Controllers_Frontend_Example1 extends Enlight_Controller_Action { /** * Disable template renderer for not given actions * @return void */ public function preDispatch() { if(in_array($this->Request()->getActionName(), array('index','foo'))) { Shopware()->Plugins()->Controller()->ViewRenderer()->setNoRender(); } } /** * index action is called if no other action is triggered * @return void */ public function indexAction(){ //Diese Action soll kein Template laden, sondern direkt Json formatierte Daten zurückgeben echo"Index Action"; } /** * own action which prints the request variable bar if exist * @return void */ public function fooAction(){ $this->View()->setTemplate(); $bar = $this->Request()->bar; $bar = htmlentities($bar); if (empty($bar)){ echo "Hello World"; }else { echo $bar; } } }
Example 2: Hello World mit Ausgabe Template
Shopware\Plugins\Local\Frontend\Example2\Bootstrap.php
<?php /** * This is the standard Bootstrap Class of the Plugin. * It is core element of each plugins and it overrides the needed * methods out of the Shopware_Components_Plugin_Bootstrap * * @copyright Copyright (c) 2011, Shopware AG * @author $Author$ * @package Shopware * @subpackage Components_Plugin * @creation_date 24.11.11 15:30 * @version $Id$ */ class Shopware_Plugins_Frontend_Example2_Bootstrap extends Shopware_Components_Plugin_Bootstrap { /** * this derived method is called automatically each time the plugin will be installed * * @return bool | if false is return the installation will failed */ public function install() { $event = $this->createEvent( 'Enlight_Controller_Action_PostDispatch', 'onPostDispatch' ); $this->subscribeEvent($event); return true; } /** * is called after every action is finished * * @static * @param Enlight_Event_EventArgs $args * @return void */ static function onPostDispatch(Enlight_Event_EventArgs $args) { //Der standard Pre/Post Dispatcher wird bei jedem Request aufgerufen (Frontend/Backend) $request = $args->getSubject()->Request(); $response = $args->getSubject()->Response(); $view = $args->getSubject()->View(); /* Man sollte die Funktion möglichst einschränken, da diese meist nicht immer ausgeführt werden soll. Dies ist wichtig um Seiteneffekte im System zu vermeiden. Möglichst mit $request->getModuleName() != 'frontend' nur die Module(frontend/backend) auswählen die für die Funktion verwendet werden. Nur die Controller mit $request->getControllerName() für die Funktion auswählen, an denen die Funktion ausgeführt werden soll */ if(!$request->isDispatched() || $response->isException() || $request->getModuleName() != 'frontend' || $request->getControllerName() != 'index' ) { return; } $view->extendsTemplate(dirname(__FILE__).'/plugin.tpl'); $view->extendsBlock("frontend_index_logo",'<h1>Hello World 2</h1>','replace'); } }
Shopware\Plugins\Local\Frontend\Example2\plugin.tpl
{block name="frontend_index_logo"} <h1>Hello World Foo</h1> {/block}
Example 3: XML-Datei nach Bestellabschluss im Filesystem ablegen und Event und Hook-Beispiel
Shopware\Plugins\Local\Frontend\Example3\Boostrap.php
<?php /** * This is the standard Bootstrap Class of the Plugin. * It is core element of each plugins and it overrides the needed * methods out of the Shopware_Components_Plugin_Bootstrap * * @copyright Copyright (c) 2011, Shopware AG * @author $Author$ * @package Shopware * @subpackage Components_Plugin * @creation_date 24.11.11 15:30 * @version $Id$ */ class Shopware_Plugins_Frontend_Example3_Bootstrap extends Shopware_Components_Plugin_Bootstrap { /** * this derived method is called automatically each time the plugin will be installed * * @return bool | if false is return the installation will failed */ public function install() { /** * Global events */ $event = $this->createEvent( 'Enlight_Controller_Action_PreDispatch', 'onPreDispatch' ); $this->subscribeEvent($event); $event = $this->createEvent( 'Enlight_Controller_Action_PostDispatch', 'onPostDispatch' ); $this->subscribeEvent($event); /** * Application events */ //Notify $event = $this->createEvent( 'Shopware_Modules_Order_SaveOrder_ProcessDetails', 'onSaveOrderProcessDetailsNotify' ); $this->subscribeEvent($event); //Notify-Until $event = $this->createEvent( 'Shopware_Modules_Basket_AddArticle_Start', 'onAddArticleStartNotifyUntil' ); $this->subscribeEvent($event); //Filter $event = $this->createEvent( 'Shopware_Modules_Order_SendMail_FilterVariables', 'onSendMailFilterVariablesFilter' ); $this->subscribeEvent($event); /** * Hooks */ $hookBefore = $this->createHook( 'sArticles', 'sGetArticleById', 'onArticleBefore', Enlight_Hook_HookHandler::TypeBefore, 0 ); $this->subscribeHook($hookBefore); $hookAfter = $this->createHook( 'sArticles', 'sGetArticleById', 'onArticleAfter', Enlight_Hook_HookHandler::TypeAfter, 0 ); $this->subscribeHook($hookAfter); // $hookReplace = $this->createHook( // 'sArticles', // 'sGetArticleById', // 'onArticleReplace', // Enlight_Hook_HookHandler::TypeReplace, // 0 // ); // $this->subscribeHook($hookReplace); $event = $this->createHook( 'sOrder', 'sSaveOrder', 'onOrder', Enlight_Hook_HookHandler::TypeAfter, 0 ); $this->subscribeHook($event); $form = $this->Form(); $form->setElement('text', 'orderpath', array('label'=>'Dateiname','value'=>'export.xml')); $form->save(); return true; } #################################################################################################################### #Global events###################################################################################################### #################################################################################################################### /** * is called before every action is finished * * @static * @param Enlight_Event_EventArgs $args * @return void */ public static function onPreDispatch(Enlight_Event_EventArgs $args) { $request = $args->getSubject()->Request(); $response = $args->getSubject()->Response(); $view = $args->getSubject()->View(); if($response->isException() || $request->getModuleName() != 'frontend' || $request->getControllerName() != 'checkout') { return; } //.......... //....... //... } /** * is called after every action is finished * * @static * @param Enlight_Event_EventArgs $args * @return void */ public static function onPostDispatch(Enlight_Event_EventArgs $args) { $request = $args->getSubject()->Request(); $response = $args->getSubject()->Response(); $view = $args->getSubject()->View(); if(!$request->isDispatched() || $response->isException() || $request->getModuleName() != 'frontend' || $request->getControllerName() != 'checkout') { return; } //.......... //....... //... } #################################################################################################################### #Application events################################################################################################# #################################################################################################################### /** * Notification Event is called when the specific code line is reached * @static * @param Enlight_Event_EventArgs $args * @return void */ public static function onSaveOrderProcessDetailsNotify(Enlight_Event_EventArgs $args) { // $orderInstance = $args->getSubject(); // $details = $args->getDetails(); // // echo "<pre>"; // print_r($details); // echo "</pre>"; } /** * Notify Until Event is called when the specific code line is reached * @static * @param Enlight_Event_EventArgs $args * @return void */ public static function onAddArticleStartNotifyUntil(Enlight_Event_EventArgs $args) { // $ordernumber = $args->getId(); // if('SW10056' == $ordernumber) { // return true; // } } /** * Filter Event is called when the specific code line is reached * @static * @param Enlight_Event_EventArgs $args * @return void */ public static function onSendMailFilterVariablesFilter(Enlight_Event_EventArgs $args) { // $variables = $args->getReturn(); // $variables["sComment"] = 'my new comment'; // return $variables; } #################################################################################################################### #Hooks############################################################################################################## #################################################################################################################### /** * called before the sGetArticleById function is called * @static * @param Enlight_Hook_HookArgs $args * @return void */ static function onArticleBefore (Enlight_Hook_HookArgs $args) { // $arguments = $args->getArgs(); // $id = $arguments[0]; // getArgs() liefert ein Array zurück - Key = Parameter Position // $id = 14; // $args->set('id',$id); // Setzen der ID - so kann man also beeinflussen, welche Parameter die Original-Funktion erhält } /** * called after the sGetArticleById function is called * @static * @param Enlight_Hook_HookArgs $args * @return void */ static function onArticleAfter (Enlight_Hook_HookArgs $args) { // $article = $args->getReturn(); // // Preis ist an dieser Stelle bereits formatiert, also Komma durch Punkt ersetzen, damit wir mit dem Ergebnis rechnen können // $article["price"] = str_replace(",",".",$article["price"]); // $article["price"]*= 1.1; // $article["price"] = $args->getSubject()->sFormatPrice($article["price"]); // Aufruf einer Methode aus der Original-Klasse, um den Preis entsprechend zu formatieren // $args->setReturn($article); // Rückgabe der Methode definieren } /** * called instead of the sGetArticleById function * @static * @param Enlight_Hook_HookArgs $args * @return void */ static function onArticleReplace (Enlight_Hook_HookArgs $args) { die("Artikel gesperrt"); } /** * called after of the sSaveOrder function * @static * @param Enlight_Hook_HookArgs $args * @return void */ static function onOrder (Enlight_Hook_HookArgs $args) { /* // Die Funktion saveOrder gibt die Bestellnummer zurück, // diese können wir per getReturn() abfangen $ordernumber = $args->getReturn(); // Zugriff auf Plugin-Konfiguration $config = Shopware()->Plugins()->Frontend()->Example3()->Config(); // Zugriff auf Export $export = Shopware()->Api()->Export(); // Über die ordernumber die ID der Bestellung auslesen $orderID = Shopware()->Db()->fetchOne("SELECT id FROM s_order WHERE ordernumber=?",array($ordernumber)); // Hauptdaten der Bestellung $order = current($export->sGetOrders(array("orderID"=>$orderID))); // Bestellpositionen $positions = $export->sOrderDetails(array("orderID"=>$orderID)); // Kundendaten $user = $export->sOrderCustomers (array("orderID"=>$orderID)); // XML-Export-Objekt erstellen $xml = Shopware()->Api()->convert->xml; // Unsere Bestellung in XML umwandeln $dump = $xml->encode(array("order"=>$order,"position"=>$positions,"user"=>$user)); file_put_contents(Shopware()->DocPath()."/uploads/".$config->orderpath,$dump); */ } }
Example 4: Hello World im Backend
Shopware\Plugins\Local\Frontend\Example4\Boostrap.php
<?php /** * This is the standard Bootstrap Class of the Plugin. * It is core element of each plugins and it overrides the needed * methods out of the Shopware_Components_Plugin_Bootstrap * * @copyright Copyright (c) 2011, Shopware AG * @author $Author$ * @package Shopware * @subpackage Components_Plugin * @creation_date 24.11.11 15:30 * @version $Id$ */ class Shopware_Plugins_Frontend_Example4_Bootstrap extends Shopware_Components_Plugin_Bootstrap { /** * this derived method is called automatically each time the plugin will be installed * * @return bool | if false is return the installation will failed */ public function install() { $event = $this->createEvent( 'Enlight_Controller_Dispatcher_ControllerPath_Backend_Example4', 'onGetControllerPathBackend' ); $this->subscribeEvent($event); $parent = $this->Menu()->findOneBy('label', 'Marketing'); $item = $this->createMenuItem(array( 'label' => 'Example4', 'onclick' => 'openAction(\'Example4\');', 'class' => 'ico2 layout', 'active' => 1, 'parent' => $parent, 'style' => 'background-position: 5px 5px;' )); $this->Menu()->addItem($item); $this->Menu()->save(); return true; } /** * just points to the path of this controller * * @static * @param Enlight_Event_EventArgs $args * @return string */ public static function onGetControllerPathBackend(Enlight_Event_EventArgs $args) { return dirname(__FILE__).'/Example4.php'; } }
Shopware\Plugins\Local\Frontend\Example4\Example4.php
<?php /** * This is a standard Controller Class. * This class provides advanced functions for the plugin * * @copyright Copyright (c) 2011, Shopware AG * @author m.schmaeing * @author st.hamann * @package Shopware * @subpackage Controllers_Frontend * @creation_date 24.11.11 15:30 * @version $Id$ */ class Shopware_Controllers_Backend_Example4 extends Enlight_Controller_Action { /** * this function is called initially and extended the template dir * @return void */ public function init(){ $this->View()->addTemplateDir(dirname(__FILE__)."/templates/"); } /** * index action is called if no other action is triggered * @return void */ public function indexAction(){ $this->View()->loadTemplate("backend/plugins/Example4/index.tpl"); } /** * this action loads the skeleton template * @return void */ public function skeletonAction(){ $this->View()->loadTemplate("backend/plugins/Example4/skeleton.tpl"); } /** * this is just the a test action * @return void */ public function testAction(){ $this->View()->foo = 'bar'; return $this->forward('index'); } }
Shopware\Plugins\Local\Frontend\Example4\templates\backend\plugins\Example4\index.tpl
{extends file="backend/index/parent.tpl"} {block name="backend_index_css" append} <!-- Common CSS --> <link href="{link file='engine/backend/css/icons4.css'}" rel="stylesheet" type="text/css" /> <link href="{link file='engine/backend/css/modules.css'}" rel="stylesheet" type="text/css" /> {/block} {block name="backend_index_body_inline"} <fieldset> <legend>Hello Window</legend> Verlinkung auf andere Action: <a href="{url action=test}">Klick</a> Test-Ausgabe Variable:{if $foo}<h1>{$foo}</h1>{/if} </fieldset> {/block}
Shopware\Plugins\Local\Frontend\Example4\templates\backend\plugins\Example4\skeleton.tpl
{ "init": { "title": "{s name='WindowTitle' force}Example 4{/s}", "width": 900, "height": 650, "id": "coupon", "minwidth": 800, "minheight": 650, "content": "", "loader": "action", "url": "{url action='index'|escape:'javascript'}", "help": "" } }
Example 5: "Kunden haben gerade gekauft" als Slider auf Startseite
Shopware\Plugins\Local\Frontend\Example5\Bootstrap.php
<?php /** * This is the standard Bootstrap Class of the Plugin. * It is core element of each plugins and it overrides the needed * methods out of the Shopware_Components_Plugin_Bootstrap * * @copyright Copyright (c) 2011, Shopware AG * @author $Author$ * @package Shopware * @subpackage Components_Plugin * @creation_date 24.11.11 15:30 * @version $Id$ */ class Shopware_Plugins_Frontend_Example5_Bootstrap extends Shopware_Components_Plugin_Bootstrap { /** * this derived method is called automatically each time the plugin will be installed * * @return bool | if false is return the installation will failed */ public function install() { $event = $this->createEvent( 'Enlight_Controller_Dispatcher_ControllerPath_Frontend_Example5', 'onGetControllerPathFrontend' ); $this->subscribeEvent($event); $event = $this->createEvent( 'Enlight_Controller_Action_PostDispatch_Frontend_Index', 'onPostDispatchIndex' ); $this->subscribeEvent($event); $form = $this->Form(); $form->setElement('text', 'max_articles', array('label'=>'Artikel Limit','value'=>'20')); $form->setElement('text', 'page_articles', array('label'=>'Artikel pro Seite','value'=>'3')); $form->save(); return true; } /** * just points to the path of this controller * * @static * @param Enlight_Event_EventArgs $args * @return string */ public static function onGetControllerPathFrontend(Enlight_Event_EventArgs $args) { return dirname(__FILE__).'/Example5.php'; } /** * is called after the index action is finished * * @static * @param Enlight_Event_EventArgs $args * @return void */ public static function onPostDispatchIndex(Enlight_Event_EventArgs $args) { $request = $args->getSubject()->Request(); $response = $args->getSubject()->Response(); $view = $args->getSubject()->View(); // $config = Shopware()->Plugins()->Frontend()->Recommendation()->Config(); $view->extendsTemplate(dirname(__FILE__).'/frontend/plugins/Example5/index.tpl'); } }
Shopware\Plugins\Local\Frontend\Example5\Example5.php
<?php /** * This is a standard Controller Class. * This class provides advanced functions for the plugin * * @copyright Copyright (c) 2011, Shopware AG * @author m.schmaeing * @author st.hamann * @package Shopware * @subpackage Controllers_Frontend * @creation_date 24.11.11 15:30 * @version $Id$ */ class Shopware_Controllers_Frontend_Example5 extends Enlight_Controller_Action { /** * index action is called if no other action is triggered * @return void */ public function indexAction(){ $config = Shopware()->Plugins()->Frontend()->Example5()->Config(); // Configuration $page = $this->Request()->pages ? $this->Request()->pages : 1; $maxArticles = $config->max_articles; $perPage = $config->page_articles; $sql = " SELECT DISTINCT articleID FROM s_order_details, s_order WHERE s_order_details.modus = 0 AND s_order.id = s_order_details.orderID ORDER BY ordertime DESC LIMIT $maxArticles"; $articles = Shopware()->Db()->fetchAll($sql); $articles = array_chunk($articles,$perPage); // Count pages $pages = count($articles); // Define current scope $articles = $articles[$page-1]; foreach ($articles as $article){ $tmpContainer = Shopware()->Modules()->Articles()->sGetPromotionById("fix",0,$article['articleID']); if (!empty($tmpContainer["articleName"])){ $result[] = $tmpContainer; } } $this->View()->articles = $result; $this->View()->pages = $pages; $this->View()->loadTemplate("frontend/plugins/recommendation/slide_articles.tpl"); } }
Shopware\Plugins\Local\Frontend\Example5\frontend\plugins\Example5\index.tpl
{block name='frontend_index_header_javascript_inline' prepend} jQuery(document).ready(function($) { $('.sliderBought').ajaxSlider('ajax', { 'url': '{url controller=Example5 action=index}', 'title': '{s name="IndexBoughtArticlesSlider"}Gerade gekauft:{/s}', 'headline': true, 'navigation': false, 'scrollSpeed': 800, 'rotate': true, 'width':628, 'containerCSS': { 'marginTop': '0px', 'marginBottom': '15px' } }); }); {/block} {block name="frontend_home_index_text" append} <div class="sliderBought"></div> {/block} {block name="frontend_index_header_css_screen" append} <style type="text/css"> #center .sliderBought { width: 628px; } #center .sliderBought .ajaxSlider { width: 628px; height: 220px; position: relative; margin-top: 12px; margin-bottom: 15px; } #center .sliderBought .ajaxSlider .sliding_outer { position: relative; overflow: hidden; left: 26px; width: 576px; height: 190px; } </style> {/block}
Example 6: Eigener Frontend-Controller mit Anbindung API
Shopware\Plugins\Local\Frontend\Example6\Bootstrap.php
<?php /** * This is the standard Bootstrap Class of the Plugin. * It is core element of each plugins and it overrides the needed * methods out of the Shopware_Components_Plugin_Bootstrap * * @copyright Copyright (c) 2011, Shopware AG * @author $Author$ * @package Shopware * @subpackage Components_Plugin * @creation_date 24.11.11 15:30 * @version $Id$ */ class Shopware_Plugins_Frontend_Example6_Bootstrap extends Shopware_Components_Plugin_Bootstrap { /** * this derived method is called automatically each time the plugin will be installed * * @return bool | if false is return the installation will failed */ public function install() { $event = $this->createEvent( 'Enlight_Controller_Dispatcher_ControllerPath_Frontend_Example6', 'onGetControllerPathBackend' ); $this->subscribeEvent($event); return true; } /** * just points to the path of this controller * * @static * @param Enlight_Event_EventArgs $args * @return string */ public static function onGetControllerPathBackend(Enlight_Event_EventArgs $args) { return dirname(__FILE__).'/Example6.php'; } }
Shopware\Plugins\Local\Frontend\Example6\Example6.php
<?php /** * This is a standard Controller Class. * This class provides advanced functions for the plugin * * @copyright Copyright (c) 2011, Shopware AG * @author m.schmaeing * @author st.hamann * @package Shopware * @subpackage Controllers_Frontend * @creation_date 24.11.11 15:30 * @version $Id$ */ class Shopware_Controllers_Frontend_Example6 extends Enlight_Controller_Action { /** * Disable template renderer for not given actions * @return void */ public function preDispatch() { if(in_array($this->Request()->getActionName(), array('index'))) { Shopware()->Plugins()->Controller()->ViewRenderer()->setNoRender(); } } /** * index action is called if no other action is triggered * @return void */ public function indexAction(){ $api = Shopware()->Api()->Import(); //Artikel Ölwechsel ID = 130 $api->sArticle(array("articleID"=>130,"instock"=>rand(32,64))); echo "Import erfolgreich"; } }
Example 7: Eigener Cronjob als Plugin
Shopware\Plugins\Local\Frontend\Example7\Bootstrap.php
<?php /** * This is the standard Bootstrap Class of the Plugin. * It is core element of each plugins and it overrides the needed * methods out of the Shopware_Components_Plugin_Bootstrap * * @copyright Copyright (c) 2011, Shopware AG * @author $Author$ * @package Shopware * @subpackage Components_Plugin * @creation_date 24.11.11 15:30 * @version $Id$ */ class Shopware_Plugins_Frontend_Example7_Bootstrap extends Shopware_Components_Plugin_Bootstrap { /** * this derived method is called automatically each time the plugin will be installed * * @return bool | if false is return the installation will failed */ public function install() { $event = $this->createEvent( 'Shopware_CronJob_Example7', 'onRun' ); $this->subscribeEvent($event); //Cronjobname, Controllername, Interval, aktiv $this->subscribeCron("Example7","Example7",10,true); return true; } /** * called if the specified cronjob is triggered * @static * @param Shopware_Components_Cron_CronJob $job * @return void */ public static function onRun(Shopware_Components_Cron_CronJob $job) { $api = Shopware()->Api()->Import(); $stock = rand(32,64); $api->sArticle(array("articleID"=>130,"instock"=>$stock)); echo "Set instock of article 130 to $stock\n"; } }
Example 8: PDF-Rechnung nach Bestellung per eMail
Shopware\Plugins\Local\Frontend\Example8\Bootstrap.php
<?php /** * This is the standard Bootstrap Class of the Plugin. * It is core element of each plugins and it overrides the needed * methods out of the Shopware_Components_Plugin_Bootstrap * * @copyright Copyright (c) 2011, Shopware AG * @author $Author$ * @package Shopware * @subpackage Components_Plugin * @creation_date 24.11.11 15:30 * @version $Id$ */ class Shopware_Plugins_Frontend_Example8_Bootstrap extends Shopware_Components_Plugin_Bootstrap { /** * this derived method is called automatically each time the plugin will be installed * @return bool */ public function install() { $event = $this->createHook('sOrder', 'sSaveOrder', 'onOrder', Enlight_Hook_HookHandler::TypeAfter, 0); $this->subscribeHook($event); return true; } /** * called after of the sSaveOrder function * @static * @param Enlight_Hook_HookArgs $args * @return void */ static function onOrder(Enlight_Hook_HookArgs $args) { /* * Die Funktion saveOrder gibt die Bestellnummer zurück, * diese können wir per getReturn() abfangen */ $ordernumber = $args->getReturn(); // Über die ordernumber die ID der Bestellung auslesen $orderID = Shopware()->Db()->fetchOne("SELECT id FROM s_order WHERE ordernumber=?", array($ordernumber)); $document = Shopware_Components_Document::initDocument($orderID, 0, array("netto" => false, "date" => date("Y-m-d H:i:s"), "shippingCostsAsPosition" => true, "_renderer" => "pdf")); $document->render(); $email = $document->_order->user->email; $getDocument = Shopware()->Db()->fetchRow(" SELECT * FROM s_order_documents WHERE orderID = ? ", array($orderID)); //erstellt ein neues Mail Objekt damit die Mailfunktion keine alten Daten beinhaltet. $mail = new Enlight_Components_Mail(); $mail->IsHTML(0); $mail->From = Shopware()->Config()->Mail; // Absender = Mail Shopbetreiber $mail->FromName = Shopware()->Config()->Mail; // Absender = Mail Shopbetreiber $mail->Subject = "Ihre Rechnung zur Bestellung $ordernumber"; // Betreff $mail->Body = "Im Anhang finden Sie Ihre Rechnung"; // Inhalt $mail->ClearAddresses(); // Vorherige Adressen entfernen // Die in der Plugin-Konfiguration eingetragene Adresse als Empfänger nehmen $mail->AddAddress($email, ''); $mail->addAttachment(Shopware()->DocPath() . "files/documents/" . $getDocument["hash"] . ".pdf", $getDocument["docID"] . ".pdf"); // Mail versenden $mail->Send(); } }
Artikel-PDF erstellen
Artikel bewerten
Kommentare:
Artikel kommentieren
Weitere interessante Artikel:
Bestell-Nr.: SW1448
Lieferzeit ca. 5 Tage
Preise inkl. gesetzlicher
MwSt. zzgl. Versandkosten*
Preise inkl. gesetzlicher
MwSt. + Versandkosten*
Kategorien: