I want to call model method in other model file. i have created file (mobiledetect.php) in my "model folder" and i want to call this in service.php in in my custom function in same model folder.
please find my code here where i want call another model method. anyone have an idea for that please?
<?php
namespace Mymodule\Custom\Model;
use Mymodule\Custom\Api\ServiceInterface;
class Service implements ServiceInterface
{
/**
* @var \Magento\Checkout\Helper\Data
*/
private $checkoutHelper;
/**
* @var \Magento\Store\Model\ScopeInterface
*/
private $storeManager;
/**
* Constructor
* @param \Magento\Checkout\Helper\Data $checkoutHelper
* @param \Magento\Store\Model\ScopeInterface $defaultConfigProvider
*/
public function __construct(
\Magento\Checkout\Helper\Data $checkoutHelper,
\Magento\Store\Model\StoreManagerInterface $storeManager
) {
$this->checkoutHelper = $checkoutHelper;
$this->storeManager = $storeManager;
}
/**
* Return URL
* @return string
*/
public function redirect_url()
{
//want to call model here of mobiledetect.php
if(isMobile)
{
//Return statement modified by Ravikant Varma
return $this->checkoutHelper->getCheckout()->getCustomRedirectUrl();
}
else {
return $this->storeManager->getStore()->getUrl('checkout/onepage/success/');
}
}
}
Best Answer
To call the other model define the dependency in the constructor. Also define: private $path;
I have given example in above code that's how you can define it in constructor and call it and call any function in other model using $this->path->getFunctionName(); That way you can do this.
You can replace that details with your details to see it working.