Magneto 2 How to call Helper function in observer fileFile Name for custom helperHow to call helper method in .phtml filehow do I call function(Helper) from xml in magentoMagento 2: How to call helper function in ControllerMagento 2 - How to call Helper function via BlockCall block function from helperMagento 2: call helper function in ui component formMagento 1.9 problem call HelperHow to call helper function observer in magento2Magento2: How can call observer function in controller file?
What about orion ISS missions?
Color coding Alerts
Can I say "guess what" to acknowledge new information?
Decode the Dreaded Alphabet Cypher™️
Why does General Grievous say “Ah yes, the negotiator?”
Is there a conflict between YAGNI and SRP?
What are the possible punishments for an impeached USA president?
Sending non-work emails to colleagues. Is it rude?
Rite of Winter: How to Stop Crescian Couples from Mutual Assassination
Commander Damage Stacking & Player Control
Can I freely use 'here is' instead of 'there is' if I'm in the place where that thing is?
How to compare the signature of two functions?
Why is Euler's Formula for Planar Graph Not Working Here?
Is it normal to not be able to work 8 hours a day?
How long must a US passport be valid when visiting Brazil?
How are names of enharmonic notes determined?
Highlight setext-style (asciidoctor/markdown) headers
Search for arbitrary files but only list matches in results once
Can I apply for a passport in the country I'm in so I can travel to my home country?
Do the holes in Jacquard loom punched cards represent input data or program code?
Are these pigtails inside the panel and outside a junction box allowed?
Should high school teachers say “real numbers” before teaching complex numbers?
Had Ravan tried would he have been able to lift Angad's feet?
Is Communism intrinsically Authoritarian?
Magneto 2 How to call Helper function in observer file
File Name for custom helperHow to call helper method in .phtml filehow do I call function(Helper) from xml in magentoMagento 2: How to call helper function in ControllerMagento 2 - How to call Helper function via BlockCall block function from helperMagento 2: call helper function in ui component formMagento 1.9 problem call HelperHow to call helper function observer in magento2Magento2: How can call observer function in controller file?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
I am new to Magento development and I am using a custom MySQL query in some places like in observer/plugin etc.
These codes are repeating for all these files. so I want to make a new reusable function/class and want to call this method instead of writing the same thing in all files.
I don't know even to create controller and all, could you please help me to create this?
$modelMaterials = $objectManager->create('VendorModuleModelGrid');
$dataMaterials=$modelMaterials->getCollection()->addFieldToFilter('entity_id', array('in' => array($material_id)))->setPageSize(1)->setOrder('entity_id','DESC');;
if($dataMaterials->getSize())
$materialsarr = $dataMaterials->getFirstItem();
$weight = $subject->getWeight();
$fineWeight = $data->getFineWeight();
$metalPrice = (double)$materialsarr->getAmount() * ((float)$weight + (float)$fineWeight);
$metalPrice += (double)$data->getSurcharge();
$metalValue = (double)$materialsarr->getAmount() * ((float)$weight + (float)$fineWeight);
$price = (double)$materialsarr->getAmount() * (double)$weight;
$tx_rates = (double)$materialsarr->getTax() / 100;
$tax = $tx_rates * (double)$metalPrice;
$specialPrice = $subject->getSpecialPrice();
magento2 module event-observer helper
add a comment
|
I am new to Magento development and I am using a custom MySQL query in some places like in observer/plugin etc.
These codes are repeating for all these files. so I want to make a new reusable function/class and want to call this method instead of writing the same thing in all files.
I don't know even to create controller and all, could you please help me to create this?
$modelMaterials = $objectManager->create('VendorModuleModelGrid');
$dataMaterials=$modelMaterials->getCollection()->addFieldToFilter('entity_id', array('in' => array($material_id)))->setPageSize(1)->setOrder('entity_id','DESC');;
if($dataMaterials->getSize())
$materialsarr = $dataMaterials->getFirstItem();
$weight = $subject->getWeight();
$fineWeight = $data->getFineWeight();
$metalPrice = (double)$materialsarr->getAmount() * ((float)$weight + (float)$fineWeight);
$metalPrice += (double)$data->getSurcharge();
$metalValue = (double)$materialsarr->getAmount() * ((float)$weight + (float)$fineWeight);
$price = (double)$materialsarr->getAmount() * (double)$weight;
$tx_rates = (double)$materialsarr->getTax() / 100;
$tax = $tx_rates * (double)$metalPrice;
$specialPrice = $subject->getSpecialPrice();
magento2 module event-observer helper
add a comment
|
I am new to Magento development and I am using a custom MySQL query in some places like in observer/plugin etc.
These codes are repeating for all these files. so I want to make a new reusable function/class and want to call this method instead of writing the same thing in all files.
I don't know even to create controller and all, could you please help me to create this?
$modelMaterials = $objectManager->create('VendorModuleModelGrid');
$dataMaterials=$modelMaterials->getCollection()->addFieldToFilter('entity_id', array('in' => array($material_id)))->setPageSize(1)->setOrder('entity_id','DESC');;
if($dataMaterials->getSize())
$materialsarr = $dataMaterials->getFirstItem();
$weight = $subject->getWeight();
$fineWeight = $data->getFineWeight();
$metalPrice = (double)$materialsarr->getAmount() * ((float)$weight + (float)$fineWeight);
$metalPrice += (double)$data->getSurcharge();
$metalValue = (double)$materialsarr->getAmount() * ((float)$weight + (float)$fineWeight);
$price = (double)$materialsarr->getAmount() * (double)$weight;
$tx_rates = (double)$materialsarr->getTax() / 100;
$tax = $tx_rates * (double)$metalPrice;
$specialPrice = $subject->getSpecialPrice();
magento2 module event-observer helper
I am new to Magento development and I am using a custom MySQL query in some places like in observer/plugin etc.
These codes are repeating for all these files. so I want to make a new reusable function/class and want to call this method instead of writing the same thing in all files.
I don't know even to create controller and all, could you please help me to create this?
$modelMaterials = $objectManager->create('VendorModuleModelGrid');
$dataMaterials=$modelMaterials->getCollection()->addFieldToFilter('entity_id', array('in' => array($material_id)))->setPageSize(1)->setOrder('entity_id','DESC');;
if($dataMaterials->getSize())
$materialsarr = $dataMaterials->getFirstItem();
$weight = $subject->getWeight();
$fineWeight = $data->getFineWeight();
$metalPrice = (double)$materialsarr->getAmount() * ((float)$weight + (float)$fineWeight);
$metalPrice += (double)$data->getSurcharge();
$metalValue = (double)$materialsarr->getAmount() * ((float)$weight + (float)$fineWeight);
$price = (double)$materialsarr->getAmount() * (double)$weight;
$tx_rates = (double)$materialsarr->getTax() / 100;
$tax = $tx_rates * (double)$metalPrice;
$specialPrice = $subject->getSpecialPrice();
magento2 module event-observer helper
magento2 module event-observer helper
edited Sep 20 at 12:22
Sumit
3,7404 silver badges24 bronze badges
3,7404 silver badges24 bronze badges
asked Sep 20 at 9:08
Sanjun DevSanjun Dev
3699 bronze badges
3699 bronze badges
add a comment
|
add a comment
|
3 Answers
3
active
oldest
votes
The Magento 2 helper is the class which is used for adding functionalities to several features and can be used anywhere in the entire website. They are created as Singleton (single instances of objects) which can be called in controllers, views, models, templates etc.
Create Magento 2 Custom Module
Add module.xml file in app/code/Magenticians/Mymodule/etc
and copy the following code in it:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magenticians_Mymodule" setup_version="1.0.1">
</module>
</config>
Add registration.php in app/code/Magenticians/Mymodule
and copy the following code in it:
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Magenticians_Mymodule',
__DIR__
);
Create Magento 2 Helper
Now add Data.php file in app/code/Magenticians/Mymodule/Helper
and copy the following code in it:
<?php
namespace MagenticiansMymoduleHelper;
use MagentoFrameworkAppHelperAbstractHelper;
class Data extends AbstractHelper
/**
* @var MagentoFrameworkAppHttpContext
*/
private $httpContext;
public function __construct(
MagentoFrameworkAppHelperContext $context,
MagentoFrameworkAppHttpContext $httpContext
)
parent::__construct($context);
$this->httpContext = $httpContext;
public function isLoggedIn()
$isLoggedIn = $this->httpContext->getValue(MagentoCustomerModelContext::CONTEXT_AUTH);
return $isLoggedIn;
and observer file how to helper class call
<?php
namespace MagenticiansMymoduleObserver;
use MagentoFrameworkEventObserverInterface;
class Mymodulename implements ObserverInterface
...
protected $dataHelper;
...
public function __construct(
...
MagenticiansMymoduleHelperData $dataHelper
...
)
...
$this->dataHelper = $dataHelper;
...
public function ...()
$helper = $this->dataHelper->isLoggedIn();
...
?>
Great! this is what i am looking for, but can you provide a example for calling this from a observer/plugin? now you gave me the phtml file.
– Sanjun Dev
Sep 20 at 9:18
add a comment
|
Create a helper class and define in function in helper. Then you can use where ever in those Magento project.
Helpers are usually used as elements that are global and always available.
Magento 2 Helper contains functions and methods that are commonly used throughout the application. Methods that are declared as Helpers can be called from any template file, block, model, controller class
or even from another helper in Magento 2.
For more detail refer: https://www.mageplaza.com/devdocs/create-helper-class-magento-2.html
Updated solution:
you can get helper class valuea in template like below code
$this->helper('VendorModuleHelperData')->methodname();
add a comment
|
For using common function, the best approach is to create a custom module and use Magento 2 Helper which gives you one of the best opportunities to override core files and pre-defined functions of the Magento 2 platform.
The Magento 2 helper is the class which is used for adding functionalities to several features and can be used anywhere in the entire website. They are created as Singleton (single instances of objects) which can be called in controllers, views, models, templates etc.
For example, You can use the above code in your helper and inject your helper class anywhere in the code to get the data from the code.
For more detailed information you can visit this link.
EDIT:
You can use the below code in your observer for adding helper class.
<?php
namespace CustomModuleObserver;
use MagentoFrameworkEventObserverInterface;
class SalableObserver implements ObserverInterface
protected $dataHelper;
public function __construct(
CustomModuleHelperData $dataHelper
)
$this->dataHelper = $dataHelper;
public function yourFunction()
$helper = $this->dataHelper;
?>
Hope it helps!!!
add a comment
|
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f290352%2fmagneto-2-how-to-call-helper-function-in-observer-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The Magento 2 helper is the class which is used for adding functionalities to several features and can be used anywhere in the entire website. They are created as Singleton (single instances of objects) which can be called in controllers, views, models, templates etc.
Create Magento 2 Custom Module
Add module.xml file in app/code/Magenticians/Mymodule/etc
and copy the following code in it:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magenticians_Mymodule" setup_version="1.0.1">
</module>
</config>
Add registration.php in app/code/Magenticians/Mymodule
and copy the following code in it:
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Magenticians_Mymodule',
__DIR__
);
Create Magento 2 Helper
Now add Data.php file in app/code/Magenticians/Mymodule/Helper
and copy the following code in it:
<?php
namespace MagenticiansMymoduleHelper;
use MagentoFrameworkAppHelperAbstractHelper;
class Data extends AbstractHelper
/**
* @var MagentoFrameworkAppHttpContext
*/
private $httpContext;
public function __construct(
MagentoFrameworkAppHelperContext $context,
MagentoFrameworkAppHttpContext $httpContext
)
parent::__construct($context);
$this->httpContext = $httpContext;
public function isLoggedIn()
$isLoggedIn = $this->httpContext->getValue(MagentoCustomerModelContext::CONTEXT_AUTH);
return $isLoggedIn;
and observer file how to helper class call
<?php
namespace MagenticiansMymoduleObserver;
use MagentoFrameworkEventObserverInterface;
class Mymodulename implements ObserverInterface
...
protected $dataHelper;
...
public function __construct(
...
MagenticiansMymoduleHelperData $dataHelper
...
)
...
$this->dataHelper = $dataHelper;
...
public function ...()
$helper = $this->dataHelper->isLoggedIn();
...
?>
Great! this is what i am looking for, but can you provide a example for calling this from a observer/plugin? now you gave me the phtml file.
– Sanjun Dev
Sep 20 at 9:18
add a comment
|
The Magento 2 helper is the class which is used for adding functionalities to several features and can be used anywhere in the entire website. They are created as Singleton (single instances of objects) which can be called in controllers, views, models, templates etc.
Create Magento 2 Custom Module
Add module.xml file in app/code/Magenticians/Mymodule/etc
and copy the following code in it:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magenticians_Mymodule" setup_version="1.0.1">
</module>
</config>
Add registration.php in app/code/Magenticians/Mymodule
and copy the following code in it:
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Magenticians_Mymodule',
__DIR__
);
Create Magento 2 Helper
Now add Data.php file in app/code/Magenticians/Mymodule/Helper
and copy the following code in it:
<?php
namespace MagenticiansMymoduleHelper;
use MagentoFrameworkAppHelperAbstractHelper;
class Data extends AbstractHelper
/**
* @var MagentoFrameworkAppHttpContext
*/
private $httpContext;
public function __construct(
MagentoFrameworkAppHelperContext $context,
MagentoFrameworkAppHttpContext $httpContext
)
parent::__construct($context);
$this->httpContext = $httpContext;
public function isLoggedIn()
$isLoggedIn = $this->httpContext->getValue(MagentoCustomerModelContext::CONTEXT_AUTH);
return $isLoggedIn;
and observer file how to helper class call
<?php
namespace MagenticiansMymoduleObserver;
use MagentoFrameworkEventObserverInterface;
class Mymodulename implements ObserverInterface
...
protected $dataHelper;
...
public function __construct(
...
MagenticiansMymoduleHelperData $dataHelper
...
)
...
$this->dataHelper = $dataHelper;
...
public function ...()
$helper = $this->dataHelper->isLoggedIn();
...
?>
Great! this is what i am looking for, but can you provide a example for calling this from a observer/plugin? now you gave me the phtml file.
– Sanjun Dev
Sep 20 at 9:18
add a comment
|
The Magento 2 helper is the class which is used for adding functionalities to several features and can be used anywhere in the entire website. They are created as Singleton (single instances of objects) which can be called in controllers, views, models, templates etc.
Create Magento 2 Custom Module
Add module.xml file in app/code/Magenticians/Mymodule/etc
and copy the following code in it:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magenticians_Mymodule" setup_version="1.0.1">
</module>
</config>
Add registration.php in app/code/Magenticians/Mymodule
and copy the following code in it:
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Magenticians_Mymodule',
__DIR__
);
Create Magento 2 Helper
Now add Data.php file in app/code/Magenticians/Mymodule/Helper
and copy the following code in it:
<?php
namespace MagenticiansMymoduleHelper;
use MagentoFrameworkAppHelperAbstractHelper;
class Data extends AbstractHelper
/**
* @var MagentoFrameworkAppHttpContext
*/
private $httpContext;
public function __construct(
MagentoFrameworkAppHelperContext $context,
MagentoFrameworkAppHttpContext $httpContext
)
parent::__construct($context);
$this->httpContext = $httpContext;
public function isLoggedIn()
$isLoggedIn = $this->httpContext->getValue(MagentoCustomerModelContext::CONTEXT_AUTH);
return $isLoggedIn;
and observer file how to helper class call
<?php
namespace MagenticiansMymoduleObserver;
use MagentoFrameworkEventObserverInterface;
class Mymodulename implements ObserverInterface
...
protected $dataHelper;
...
public function __construct(
...
MagenticiansMymoduleHelperData $dataHelper
...
)
...
$this->dataHelper = $dataHelper;
...
public function ...()
$helper = $this->dataHelper->isLoggedIn();
...
?>
The Magento 2 helper is the class which is used for adding functionalities to several features and can be used anywhere in the entire website. They are created as Singleton (single instances of objects) which can be called in controllers, views, models, templates etc.
Create Magento 2 Custom Module
Add module.xml file in app/code/Magenticians/Mymodule/etc
and copy the following code in it:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magenticians_Mymodule" setup_version="1.0.1">
</module>
</config>
Add registration.php in app/code/Magenticians/Mymodule
and copy the following code in it:
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Magenticians_Mymodule',
__DIR__
);
Create Magento 2 Helper
Now add Data.php file in app/code/Magenticians/Mymodule/Helper
and copy the following code in it:
<?php
namespace MagenticiansMymoduleHelper;
use MagentoFrameworkAppHelperAbstractHelper;
class Data extends AbstractHelper
/**
* @var MagentoFrameworkAppHttpContext
*/
private $httpContext;
public function __construct(
MagentoFrameworkAppHelperContext $context,
MagentoFrameworkAppHttpContext $httpContext
)
parent::__construct($context);
$this->httpContext = $httpContext;
public function isLoggedIn()
$isLoggedIn = $this->httpContext->getValue(MagentoCustomerModelContext::CONTEXT_AUTH);
return $isLoggedIn;
and observer file how to helper class call
<?php
namespace MagenticiansMymoduleObserver;
use MagentoFrameworkEventObserverInterface;
class Mymodulename implements ObserverInterface
...
protected $dataHelper;
...
public function __construct(
...
MagenticiansMymoduleHelperData $dataHelper
...
)
...
$this->dataHelper = $dataHelper;
...
public function ...()
$helper = $this->dataHelper->isLoggedIn();
...
?>
edited Sep 20 at 10:22
answered Sep 20 at 9:14
Rakesh DongaRakesh Donga
3,3998 silver badges28 bronze badges
3,3998 silver badges28 bronze badges
Great! this is what i am looking for, but can you provide a example for calling this from a observer/plugin? now you gave me the phtml file.
– Sanjun Dev
Sep 20 at 9:18
add a comment
|
Great! this is what i am looking for, but can you provide a example for calling this from a observer/plugin? now you gave me the phtml file.
– Sanjun Dev
Sep 20 at 9:18
Great! this is what i am looking for, but can you provide a example for calling this from a observer/plugin? now you gave me the phtml file.
– Sanjun Dev
Sep 20 at 9:18
Great! this is what i am looking for, but can you provide a example for calling this from a observer/plugin? now you gave me the phtml file.
– Sanjun Dev
Sep 20 at 9:18
add a comment
|
Create a helper class and define in function in helper. Then you can use where ever in those Magento project.
Helpers are usually used as elements that are global and always available.
Magento 2 Helper contains functions and methods that are commonly used throughout the application. Methods that are declared as Helpers can be called from any template file, block, model, controller class
or even from another helper in Magento 2.
For more detail refer: https://www.mageplaza.com/devdocs/create-helper-class-magento-2.html
Updated solution:
you can get helper class valuea in template like below code
$this->helper('VendorModuleHelperData')->methodname();
add a comment
|
Create a helper class and define in function in helper. Then you can use where ever in those Magento project.
Helpers are usually used as elements that are global and always available.
Magento 2 Helper contains functions and methods that are commonly used throughout the application. Methods that are declared as Helpers can be called from any template file, block, model, controller class
or even from another helper in Magento 2.
For more detail refer: https://www.mageplaza.com/devdocs/create-helper-class-magento-2.html
Updated solution:
you can get helper class valuea in template like below code
$this->helper('VendorModuleHelperData')->methodname();
add a comment
|
Create a helper class and define in function in helper. Then you can use where ever in those Magento project.
Helpers are usually used as elements that are global and always available.
Magento 2 Helper contains functions and methods that are commonly used throughout the application. Methods that are declared as Helpers can be called from any template file, block, model, controller class
or even from another helper in Magento 2.
For more detail refer: https://www.mageplaza.com/devdocs/create-helper-class-magento-2.html
Updated solution:
you can get helper class valuea in template like below code
$this->helper('VendorModuleHelperData')->methodname();
Create a helper class and define in function in helper. Then you can use where ever in those Magento project.
Helpers are usually used as elements that are global and always available.
Magento 2 Helper contains functions and methods that are commonly used throughout the application. Methods that are declared as Helpers can be called from any template file, block, model, controller class
or even from another helper in Magento 2.
For more detail refer: https://www.mageplaza.com/devdocs/create-helper-class-magento-2.html
Updated solution:
you can get helper class valuea in template like below code
$this->helper('VendorModuleHelperData')->methodname();
edited Sep 20 at 9:23
answered Sep 20 at 9:12
Arunprabakaran MArunprabakaran M
2,3115 silver badges25 bronze badges
2,3115 silver badges25 bronze badges
add a comment
|
add a comment
|
For using common function, the best approach is to create a custom module and use Magento 2 Helper which gives you one of the best opportunities to override core files and pre-defined functions of the Magento 2 platform.
The Magento 2 helper is the class which is used for adding functionalities to several features and can be used anywhere in the entire website. They are created as Singleton (single instances of objects) which can be called in controllers, views, models, templates etc.
For example, You can use the above code in your helper and inject your helper class anywhere in the code to get the data from the code.
For more detailed information you can visit this link.
EDIT:
You can use the below code in your observer for adding helper class.
<?php
namespace CustomModuleObserver;
use MagentoFrameworkEventObserverInterface;
class SalableObserver implements ObserverInterface
protected $dataHelper;
public function __construct(
CustomModuleHelperData $dataHelper
)
$this->dataHelper = $dataHelper;
public function yourFunction()
$helper = $this->dataHelper;
?>
Hope it helps!!!
add a comment
|
For using common function, the best approach is to create a custom module and use Magento 2 Helper which gives you one of the best opportunities to override core files and pre-defined functions of the Magento 2 platform.
The Magento 2 helper is the class which is used for adding functionalities to several features and can be used anywhere in the entire website. They are created as Singleton (single instances of objects) which can be called in controllers, views, models, templates etc.
For example, You can use the above code in your helper and inject your helper class anywhere in the code to get the data from the code.
For more detailed information you can visit this link.
EDIT:
You can use the below code in your observer for adding helper class.
<?php
namespace CustomModuleObserver;
use MagentoFrameworkEventObserverInterface;
class SalableObserver implements ObserverInterface
protected $dataHelper;
public function __construct(
CustomModuleHelperData $dataHelper
)
$this->dataHelper = $dataHelper;
public function yourFunction()
$helper = $this->dataHelper;
?>
Hope it helps!!!
add a comment
|
For using common function, the best approach is to create a custom module and use Magento 2 Helper which gives you one of the best opportunities to override core files and pre-defined functions of the Magento 2 platform.
The Magento 2 helper is the class which is used for adding functionalities to several features and can be used anywhere in the entire website. They are created as Singleton (single instances of objects) which can be called in controllers, views, models, templates etc.
For example, You can use the above code in your helper and inject your helper class anywhere in the code to get the data from the code.
For more detailed information you can visit this link.
EDIT:
You can use the below code in your observer for adding helper class.
<?php
namespace CustomModuleObserver;
use MagentoFrameworkEventObserverInterface;
class SalableObserver implements ObserverInterface
protected $dataHelper;
public function __construct(
CustomModuleHelperData $dataHelper
)
$this->dataHelper = $dataHelper;
public function yourFunction()
$helper = $this->dataHelper;
?>
Hope it helps!!!
For using common function, the best approach is to create a custom module and use Magento 2 Helper which gives you one of the best opportunities to override core files and pre-defined functions of the Magento 2 platform.
The Magento 2 helper is the class which is used for adding functionalities to several features and can be used anywhere in the entire website. They are created as Singleton (single instances of objects) which can be called in controllers, views, models, templates etc.
For example, You can use the above code in your helper and inject your helper class anywhere in the code to get the data from the code.
For more detailed information you can visit this link.
EDIT:
You can use the below code in your observer for adding helper class.
<?php
namespace CustomModuleObserver;
use MagentoFrameworkEventObserverInterface;
class SalableObserver implements ObserverInterface
protected $dataHelper;
public function __construct(
CustomModuleHelperData $dataHelper
)
$this->dataHelper = $dataHelper;
public function yourFunction()
$helper = $this->dataHelper;
?>
Hope it helps!!!
edited Sep 20 at 9:25
answered Sep 20 at 9:13
SumitSumit
3,7404 silver badges24 bronze badges
3,7404 silver badges24 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f290352%2fmagneto-2-how-to-call-helper-function-in-observer-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown