# Replacing a protected function of Backend Article Controller through Hook

**URL:** <https://forum.shopware.com/t/replacing-a-protected-function-of-backend-article-controller-through-hook/56358>\
**Category:** International (English Only)\
**Tags:** programming\
**Created:** [9. November 2018 um 19:04 UTC](https://forum.shopware.com/t/replacing-a-protected-function-of-backend-article-controller-through-hook/56358 "2018-11-09T19:04:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chaul](https://avatars.discourse-cdn.com/v4/letter/c/ed8c4c/32.png) [@chaul](https://forum.shopware.com/u/chaul)\
**Post date:** [9. November 2018 um 19:04 UTC](https://forum.shopware.com/t/replacing-a-protected-function-of-backend-article-controller-through-hook/56358/1 "2018-11-09T19:04:02Z")

</div>

Hello Guys,&nbsp;

I am trying to replace the function “Shopware\Controllers\Backend\Article::prepareVariantData” through plugin however I am even not started my journey. I created a plugin and under “Subscriber” of my plugin I create a file in which&nbsp;

public static function getSubscribedEvents()  
&nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; return [  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ‘Shopware\Controllers\Backend\Article::prepareVariantData::replace’ =\> ‘replacedPrepareVariantData’  
&nbsp; &nbsp; &nbsp; &nbsp; ];  
&nbsp; &nbsp; }

&nbsp;

public function replacedPrepareVariantData(\Enlight\_Hook\_HookArgs $args)  
&nbsp; &nbsp; {

&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$args-\>setReturn($args);

&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$controller = $args-\>getSubject();

&nbsp; &nbsp; &nbsp; &nbsp; $view = $controller-\>View();  
&nbsp; &nbsp; &nbsp; &nbsp; $request = $controller-\>Request();

&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\Doctrine\Common\Util\Debug::dump($request, 2, true, false);  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//var\_dump($result);

&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$variant = $request-\>getParam(‘variant’);  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$detailData = $request-\>getParam(‘detailData’);  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$counter = $request-\>getParam(‘counter’);  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$dependencies = $request-\>ggetParamet(‘dependencies’);  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$priceVariations = $request-\>getParamet(‘priceVariations’);  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$allOptions = $request-\>getParam(‘allOptions’);  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$originals = $request-\>getParam(‘originals’);  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$article = $request-\>getParam(‘article’);  
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;$mergeType = $request-\>getParam(‘mergeType’);

&nbsp; &nbsp; &nbsp; &nbsp; $optionsModels = ;  
&nbsp; &nbsp; &nbsp; &nbsp; $tax = $article-\>getTax();  
&nbsp; &nbsp; &nbsp; &nbsp; $optionIds = ;

&nbsp; &nbsp; &nbsp; &nbsp; //iterate the original ids to get the new variant name  
&nbsp; &nbsp; &nbsp; &nbsp; foreach ($originals as $id) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $optionId = $variant[‘o’ . $id . ‘Id’];

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //first we push the option ids in an one dimensional array to check  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $optionIds = $optionId;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $optionsModels = $allOptions[$optionId];  
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; $abortVariant = false;  
&nbsp; &nbsp; &nbsp; &nbsp; foreach ($dependencies as $dependency) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (in\_array($dependency[‘parentId’], $optionIds) && in\_array($dependency[‘childId’], $optionIds)) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $abortVariant = true;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; //if the user selects the “merge variants” generation type, we have to check if the current variant already exist.  
&nbsp; &nbsp; &nbsp; &nbsp; if ($mergeType === 2 && $abortVariant === false) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query = $this-\>getRepository()-\>getDetailsForOptionIdsQuery($article-\>getId(), $optionsModels);  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $exist = $query-\>getArrayResult();  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $abortVariant = !empty($exist);  
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; if ($abortVariant) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;  
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; //create the new variant data  
&nbsp; &nbsp; &nbsp; &nbsp; $variantData = [  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ‘active’ =\> 1,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ‘configuratorOptions’ =\> $optionsModels,  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ‘purchasePrice’ =\> $detailData[‘purchasePrice’],  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ‘lastStock’ =\> $detailData[‘lastStock’],  
&nbsp; &nbsp; &nbsp; &nbsp; ];

&nbsp; &nbsp; &nbsp; &nbsp; if ($mergeType == 1 && $counter == 0) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $variantData[‘number’] = $detailData[‘number’];  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++$counter;  
&nbsp; &nbsp; &nbsp; &nbsp; } else {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $variantData[‘kind’] = 2;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $variantData[‘number’] = $detailData[‘number’] . ‘.’ . $counter;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++$counter;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } while ($this-\>orderNumberExist($variantData[‘number’]));  
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; $prices = $this-\>prepareVariantPrice($detailData, $priceVariations, $optionIds, $tax);  
&nbsp; &nbsp; &nbsp; &nbsp; if ($prices) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $variantData[‘prices’] = $prices;  
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; return $variantData;  
&nbsp; &nbsp; }

&nbsp;

I have to alter the function but I am not confirmed weather it is working or not because It is not throughing dump also in proxy class it is not creating the hook entry. Please help.

---

<div class="post-metadata">

**Author:** ![davit\_sargsyan](https://avatars.discourse-cdn.com/v4/letter/d/7ba0ec/32.png) [@davit\_sargsyan](https://forum.shopware.com/u/davit_sargsyan)\
**Post date:** [11. November 2020 um 12:42 UTC](https://forum.shopware.com/t/replacing-a-protected-function-of-backend-article-controller-through-hook/56358/2 "2020-11-11T12:42:21Z")

</div>

Try writing the event name with underscores&nbsp;Shopware\_Controllers\_…
