# Modify "Add to shopping cart" button in plugin

**URL:** <https://forum.shopware.com/t/modify-add-to-shopping-cart-button-in-plugin/43723>\
**Category:** International (English Only)\
**Tags:** programming\
**Created:** [14. Februar 2017 um 21:36 UTC](https://forum.shopware.com/t/modify-add-to-shopping-cart-button-in-plugin/43723 "2017-02-14T21:36:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![edo](https://avatars.discourse-cdn.com/v4/letter/e/e5b9ba/32.png) [@edo](https://forum.shopware.com/u/edo)\
**Post date:** [14. Februar 2017 um 21:36 UTC](https://forum.shopware.com/t/modify-add-to-shopping-cart-button-in-plugin/43723/1 "2017-02-14T21:36:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![derwunner](https://avatars.discourse-cdn.com/v4/letter/d/e5b9ba/32.png) [@derwunner](https://forum.shopware.com/u/derwunner)\
**Post date:** [14. Februar 2017 um 23:29 UTC](https://forum.shopware.com/t/modify-add-to-shopping-cart-button-in-plugin/43723/2 "2017-02-14T23:29:11Z")

</div>

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.

&nbsp;

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:&nbsp;[shopware/index.tpl at 5.2 · shopware/shopware · GitHub](https://github.com/shopware/shopware/blob/5.2/themes/Frontend/Bare/frontend/detail/index.tpl#L111)

&nbsp;

&nbsp;

Best Regards,

&nbsp;

derwunner

---

<div class="post-metadata">

**Author:** ![edo](https://avatars.discourse-cdn.com/v4/letter/e/e5b9ba/32.png) [@edo](https://forum.shopware.com/u/edo)\
**Post date:** [15. Februar 2017 um 20:40 UTC](https://forum.shopware.com/t/modify-add-to-shopping-cart-button-in-plugin/43723/3 "2017-02-15T20:40:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![derwunner](https://avatars.discourse-cdn.com/v4/letter/d/e5b9ba/32.png) [@derwunner](https://forum.shopware.com/u/derwunner)\
**Post date:** [17. Februar 2017 um 14:21 UTC](https://forum.shopware.com/t/modify-add-to-shopping-cart-button-in-plugin/43723/4 "2017-02-17T14:21:37Z")

</div>

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.
