Skip to main content
added 477 characters in body
Source Link
Sumit
  • 5k
  • 2
  • 20
  • 35

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 Custom\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class SalableObserver implements ObserverInterface
{
    protected $dataHelper;

    public function __construct(
        \Custom\Module\Helper\Data $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.

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 Custom\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class SalableObserver implements ObserverInterface
{
    protected $dataHelper;

    public function __construct(
        \Custom\Module\Helper\Data $dataHelper
    ) {
        $this->dataHelper = $dataHelper;
    }

    public function yourFunction(){
        $helper = $this->dataHelper;
    }
}

?>

Hope it helps!!!

Source Link
Sumit
  • 5k
  • 2
  • 20
  • 35

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.

Hope it helps!!!