Call function from one module to another module

Call function from one module to another module
  • If you want to call one specific function or method which is present in different module or you want to call custom function which is present into module into theme files then follow below example
  • For example let's call one function which calculate discount from old price and new price of the product and you want to call that function into product listing page

1.) First create a function into specific module : Tridhyatech\DiscountModule\Helper

namespace Tridhyatech \ DiscountModule \Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper

{

public function __construct(

\Magento\Framework\App\Helper\Context $context

)

{

parent::__construct($context);

}

public function getDiscount(

\Magento\Catalog\Model\Product $product,

$priceType = null,

$renderZone = \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,

array $arguments = []

)

{

$getPrice = $product->getPrice();

$price = $product->getFinalPrice(); //Your Price

if($getPrice != 0)

{

$discount = 100 - round(($price/$getPrice)*100);

}

else

{

$discount = 0;

}

return $discount;

}

}

2.) Now call the function into theme file : Magento_Catalog/templates/product/list.phtml

$helper = $this->helper(‘Tridhyatech\DiscountModule\Helper\Data');

if($helper->getDiscount($_product) != 0) {

".$helper->getDiscount($_product)."%OFF"; ?>

}

NOTE : here $_product is the variable which holds the product details.



Copyright © 2024 Tridhya Tech Limited, Inc. All rights reserved.