Search code examples
templatesshopware

Overriding Template in Shopware 4 from plugin


public function install() {
    $this->subscribeEvent(
        'Enlight_Controller_Action_PostDispatchSecure_Frontend',
        'onFrontendPostDispatch',
        0
    );

    return array('success' => true, 'invalidateCache' => array('frontend'));
}

public function onFrontendPostDispatch(\Enlight_Event_EventArgs $args)
{
    /** @var \Enlight_Controller_Action $controller */
    $controller = $args->get('subject');
    $view = $controller->View();

    $view->addTemplateDir(
        __DIR__ . '/Views'
    );
}

I had tried to run the plugin and override Template but Shopware does not see changes in a plugin.

I am creating new file in /Views/frontend/checkout/cart_footer.tpl in plugins root.

I am also insert

{extends file='parent:frontend/checkout/cart_footer.tpl'}

line in .tpl file but still no success.

Does any one know where is a problem?


Solution

  • This was very easy

    I just add one line

    $view->loadTemplate('frontend/plugins/checkout/cart.tpl');
    

    And change code little bit.

    I am change event from Enlight_Controller_Action_PostDispatchSecure_Frontend to Enlight_Controller_Action_PostDispatch_Frontend_Checkout

    and add $view->loadTemplate('frontend/plugins/checkout/cart.tpl'); This path is related from "/Views" folder which is declared in addTemplateDir method.

    Bellow is whole code, Enjoy :)

    public function install() {
        $this->subscribeEvent(
            'Enlight_Controller_Action_PostDispatch_Frontend_Checkout',
            'onFrontendPostDispatch'
        );
    
        return array('success' => true, 'invalidateCache' => array('frontend'));
    }
    
    
    public function onFrontendPostDispatch(\Enlight_Event_EventArgs $args)
    {
        /** @var \Enlight_Controller_Action $controller */
        $controller = $args->get('subject');
        $view = $controller->View();
    
        $view->addTemplateDir(
            __DIR__ . '/Views'
        );
    
        $view->loadTemplate('frontend/plugins/checkout/cart.tpl');
    }