Created
August 15, 2015 19:25
-
-
Save dnoegel/797453eb01f8465d07de to your computer and use it in GitHub Desktop.
(Shopware Plugin) If a customer hits a category that does not belong to the current subshob - redirect him / her to index page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* The Bootstrap class is the main entry point of any shopware plugin. | |
* | |
* Short function reference | |
* - install: Called a single time during (re)installation. Here you can trigger install-time actions like | |
* - creating the menu | |
* - creating attributes | |
* - creating database tables | |
* You need to return "true" or array('success' => true, 'invalidateCache' => array()) in order to let the installation | |
* be successfull | |
* | |
* - update: Triggered when the user updates the plugin. You will get passes the former version of the plugin as param | |
* In order to let the update be successful, return "true" | |
* | |
* - uninstall: Triggered when the plugin is reinstalled or uninstalled. Clean up your tables here. | |
*/ | |
class Shopware_Plugins_Frontend_SwagCategoryForward_Bootstrap extends Shopware_Components_Plugin_Bootstrap | |
{ | |
public function getVersion() | |
{ | |
return '1.0.0'; | |
} | |
public function getLabel() | |
{ | |
return 'SwagCategoryForward'; | |
} | |
public function uninstall() | |
{ | |
return true; | |
} | |
public function update($oldVersion) | |
{ | |
return true; | |
} | |
public function install() | |
{ | |
$this->subscribeEvent( | |
'Enlight_Controller_Action_PreDispatch_Frontend_Listing', 'onRedirectCategories' | |
); | |
return true; | |
} | |
public function onRedirectCategories(\Enlight_Event_EventArgs $args) | |
{ | |
/** @var \Enlight_Controller_Action $controller */ | |
$controller = $args->getSubject(); | |
$request = $controller->Request(); | |
$currentCategory = $request->getParam('sCategory'); | |
if (!$currentCategory) { | |
return; | |
} | |
if (!Shopware() || !Shopware()->Shop() || !Shopware()->Shop()->getCategory()) { | |
return; | |
} | |
$shopMainCategory = Shopware()->Shop()->getCategory(); | |
if (!$shopMainCategory || !$shopMainCategory->getId()) { | |
return; | |
} | |
$shopMainCategoryId = $shopMainCategory->getId(); | |
$sql = 'SELECT * FROM s_categories WHERE id=? and path LIKE ?'; | |
$result = Shopware()->Db()->fetchOne($sql, array($currentCategory, "%|{$shopMainCategoryId}|%")); | |
if (!$result) { | |
$controller->redirect( | |
array( | |
'controller' => 'index', | |
'action' => 'index', | |
), | |
array('code' => 301) | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment