Modify "Add to shopping cart" button in plugin

How can I modify the „Add to shopping cart“ button in my plugin to work for an arbitrary article?

In my plugin, I first get some articles directly from the database and then I pass them to a frontend view.
How can I now show the „Add to shopping cart“ button for one (or more) of these articles?

I found the defalut button in the frontend_detail_buy_button smarty block (themes/Frontend/Bare/frontend/detail/buy.tpl), but I don’t know how to modify it for an arbitrary article.

My version of Shopware is 5.2.8.

That is not much complicated. You just need to subsribe the PostDispatch Detail Event. See below my code (assuming your namespaced 5.2 is called MyCompany):

file custom/plugins/CompanyPlugin/CompanyPlugin.php:

 'onPostDispatchDetail'
        ];
    }

    public function install(InstallContext $context)
    {
        // prepare plugin, e. g. create own db tables
    }

    public function uninstall(UninstallContext $context)
    {
        // clean up, e. g. drop plugin db tables
    }

    public function onPostDispatchDetail(\Enlight_Event_EventArgs $arguments)
    {
        /**
         * @var $controller \Shopware_Controllers_Frontend_Index
         */
        $controller = $arguments->getSubject();
        /**
         * @var $request \Zend_Controller_Request_Http
         */
        $request = $controller->Request();
        /**
         * @var $response \Zend_Controller_Response_Http
         */
        $response = $controller->Response();

        /**
         * @var $view \Enlight_View_Default
         */
        $view = $controller->View();

        // do your database article check here, in sArticles you will find the most article data you may need that are already fetched from the database by shopware itself (you are in after action event)
        $sArticle = $view->getAssign('sArticle');

        // add your own plugin template here, that it overrides the themes' template:
        $view->Engine()->addTemplateDir(
            $this->getPath() . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'Themes' . DIRECTORY_SEPARATOR
        );

        $sArticle['someCustomVar'] = 'hello article detail page!';
        $view->assign('someCustomVar', $sArticle);
    }
}

Then you need to create the file index.tpl under custom/plugins/CompanyPlugin/Resources/Themes/frontend/detail/index.tpl

Because Shopware will look up for templates in the path custom/plugins/CompanyPlugin/Resources/Themes, as we defined earlier in our main Plugin class, we need to place it there relative to the Bare’s Theme folder structure. Otherwise Shopware will not find your template and will use the Theme’s template.

 

You may want to inherit from the Bare’s template, because you want just to override one block, not the whole file. So, just override this block: shopware/index.tpl at 5.2 · shopware/shopware · GitHub

 

 

Best Regards,

 

derwunner

I already got my plugin bootstrap file and custom tpl file in the plugin directory.
I actually subscribed to the Enlight_Controller_Action_PostDispatchSecure_Frontend event and I want my custom „Add to shopping cart“ button in my frontend/listing/index.tpl smarty file.

The problem is that I don’t know how to make the button dynamic, i.e. to work for some specific article.

You need to create the basic action template smarty file. So, you need to create frontend/detail/index.tpl and then inherit this file from the bare theme. Also create the smarty file in the same location as the bare theme does, which holds the buy button html. Then you need to over write this block and it schould work.

So, assume your plugin has the following theme files:

custom/plugins/CompanyPlugin/Resources/Themes/frontend/detail/index.tpl and

custom/plugins/CompanyPlugin/Resources/Themes/frontend/detail/buy.tpl

They both inherit from its parent. And in buy.tpl you just over write the block with the „buy now“ button.