1

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/');


 }

}

}
  • add the another model mobiledetect.php into your question. you need to inject that class. – Sukumar Gorai Jul 5 '18 at 12:00
1

To call the other model define the dependency in the constructor. Also define: private $path;

public function __construct(
\Magento\Checkout\Helper\Data $checkoutHelper,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Your\Mobile\Detect\File\Path $path
) {
    $this->path = $path;
$this->checkoutHelper = $checkoutHelper;
$this->storeManager = $storeManager;
 }

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.