0

I want to extend app/code/Magedelight/SMSNotification/Helper/Data.php this file in my custom module.

I have created a module.

My di.xml file app/code/Hl/Smsnotification/etc/di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magedelight\SMSNotification\Helper\Data" type="Hl\Smsnotification\Helper\Smsdata" />
</config>

My Helper File app/code/Hl/Smsnotification/Helper/Smsdata.php

<?php

namespace Hl\Smsnotification\Helper;

use Magento\Framework\App\Helper\Context;
use Magento\Framework\Locale\CurrencyInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Model\ScopeInterface;

use Magedelight\SMSNotification\Helper\Data as MainHelper;

class Smsdata extends MainHelper
{    
private $storeManager;
private $localecurrency;

public function __construct(
    Context $context,
    CurrencyInterface $localeCurrency,
    StoreManagerInterface $storeManager
) {
    $this->storeManager = $storeManager;
    $this->localecurrency = $localeCurrency;
    parent::__construct($context);
}

/**  @return array */
public function getOrderData($order)
{
    $_order = $order->getOrder($order);
    if ($order->getGrandTotal()) {
        $total =$order->getGrandTotal();
    } else {
        $total = $order->getPayment()->getAmountOrdered();
    }
    
    $currency_code = $order->getOrderCurrencyCode();
    $currency_symbol = $this->localecurrency->getCurrency($currency_code)
                            ->getSymbol();

    $orderData          =   [
        'firstname'     =>  $order->getCustomerFirstname(),
        'lastname'      =>  $order->getCustomerLastname(),
        'order_id'      =>  ($_order) ? $_order->getIncrementId() : $order->getIncrementId(),
        'total'         =>  $currency_symbol.round($total, 2),
        'orderitem'     =>  $this->getOrderedItems($order->getAllItems()),
        'orderitemsizes'     =>  $this->getOrderedItemsdata($order->getAllVisibleItems()),
        'store'         => $this->getCurrentStoreName()
    ];
    return $orderData;
}

/**  @return array */
public function getOrderedData($order)
{
    $_order = $order->getOrder($order);
    if ($order->getGrandTotal()) {
        $total =$order->getGrandTotal();
    } else {
        $total = $order->getPayment()->getAmountOrdered();
    }
    
    $currency_code = $order->getOrderCurrencyCode();
    $currency_symbol = $this->localecurrency->getCurrency($currency_code)
                            ->getSymbol();

    $orderData          =   [
        'firstname'     =>  $order->getCustomerFirstname(),
        'lastname'      =>  $order->getCustomerLastname(),
        'order_id'      =>  ($_order) ? $_order->getIncrementId() : $order->getIncrementId(),
        'total'         =>  $currency_symbol.round($total, 2),
        'orderitem'     =>  $this->getOrderedItems($order->getAllVisibleItems()),
        'orderitemsizes'     =>  $this->getOrderedItemsdata($order->getAllVisibleItems()),
        'store'         => $this->getCurrentStoreName()
    ];
    return $orderData;
}

public function getOrderedItemsdata($items)
{
    $order_items_sizes = [];
    foreach ($items as $item) {
        if($item->getProductOptions()){
            $order_items_sizes[] = $item->getProductOptions()['attributes_info'][1]['value'];
        }else{
            $order_items_sizes[] = '';
        }
    }

    return implode(",", $order_items_sizes);
}

public function setOrderMesageText($message, $data)
{
    $keywords   = [
        '{firstname}',
        '{lastname}',
        '{order_id}',
        '{total}',
        '{orderitem}',
        '{store}',
        '{orderitemsizes}'
    ];
    $message = str_replace($keywords, $data, $message);
    return $message;
}
}
?>

The product is added to cart but on checkout page it gives me an error

Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: Hl\Smsnotification\Helper\Smsdata, Argument 2 passed to Hl\Smsnotification\Helper\Smsdata::__construct() must implement interface Magento\Framework\Locale\CurrencyInterface, instance of Magento\Directory\Model\Currency given, called in /var/www/html/handlfashion-new/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 121

File path app/code/Magedelight/SMSNotification/Helper/Data.php

<?php
/**
 * Magedelight
 * Copyright (C) 2019 Magedelight <info@magedelight.com>
 *
 * @category Magedelight
 * @package Magedelight_SMSNotification
 * @copyright Copyright (c) 2019 Mage Delight (http://www.magedelight.com/)
 * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License,version 3 (GPL-3.0)
 * @author Magedelight <info@magedelight.com>
 */

namespace Magedelight\SMSNotification\Helper;

use Magento\Framework\App\Helper\Context;
use Magento\Framework\Locale\CurrencyInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Model\ScopeInterface;

class Data extends \Magento\Framework\App\Helper\AbstractHElper
{
    const XML_PATH_ENABLE = 'magedelightsmsnotification/general/enable';
    const XML_PATH_GATEWAY = 'magedelightsmsnotification/general/api_gateway';
    const XML_PATH_API_ENDPOINT = 'magedelightsmsnotification/general/api_endpoints';
    const XML_PATH_API_CREDENTIAL = 'magedelightsmsnotification/general/api_credential';
    const XML_PATH_API_TO = 'magedelightsmsnotification/general/api_to';
    const XML_PATH_API_MESSAGE = 'magedelightsmsnotification/general/api_message';
    const XML_PATH_API_COUNTRY = 'magedelightsmsnotification/general/api_country';
    const XML_PATH_API_ADDITINAL_PARAM = 'magedelightsmsnotification/general/api_params';
    const XML_PATH_API_ENDPOINT_STATUS = 'magedelightsmsnotification/general/api_endpoints_status';
    const XML_PATH_API_INITIAL_STATUS = 'magedelightsmsnotification/general/intial_status';
    const XML_PATH_API_FIAL_STATUS = 'magedelightsmsnotification/general/fail_status';
    const XML_PATH_API_ERROR_KEY = 'magedelightsmsnotification/general/error_key';
    const XML_PATH_ACCOUNT_SID = 'magedelightsmsnotification/general/account_sid';
    const XML_PATH_AUTH_TOKEN = 'magedelightsmsnotification/general/auth_token';
    const XML_PATH_TWILIO_PHONE = 'magedelightsmsnotification/general/twilio_phone';
    const XML_PATH_DEFAULT_COUNRTY = 'magedelightsmsnotification/general/default_country';
    const XML_PATH_NOTIFYADMIN_ENABLE = 'magedelightsmsnotification/adminSms/notifyadmin';
    const XML_PATH_ADMIN_CONTACT = 'magedelightsmsnotification/adminSms/admin_no';
    const XML_PATH_ADMIN_EVENTS = 'magedelightsmsnotification/adminSms/admin_events';
    const XML_PATH_ADMIN_NOTIFYFAILURE = 'magedelightsmsnotification/adminSms/failurenotification';
    const XML_PATH_ADMIN_NOTIFYTOMAIL = 'magedelightsmsnotification/adminSms/notifytomail';
    const XML_PATH_ADMIN_NOTIFYFROMMAIL = 'magedelightsmsnotification/adminSms/notifymailsender';
    const XML_PATH_CUSTOMER_EVENTS = 'magedelightsmsnotification/customerSms/customer_events';
    const XML_PATH_SMSLOG_ENABLE = 'magedelightsmsnotification/smslog/enable';
    const XML_PATH_SELECT_CUSTOMER_NO = 'magedelightsmsnotification/customerSms/customer_no';
    const XML_PATH_PHONE_MAX_LENGTH = 'magedelightsmsnotification/customerSms/phone_max';
    const XML_PATH_PHONE_MIN_LENGTH = 'magedelightsmsnotification/customerSms/phone_min';
    const XML_PATH_PHONE_NOTICE = 'magedelightsmsnotification/customerSms/phone_notice';
    const XML_PATH_SMSLOG_CRON_ENABLE = 'magedelightsmsnotification/smslog/cron_enable';
    const XML_PATH_BULKSMS_URL = 'magedelightsmsnotification/general/bilksmsurl';
    const XML_PATH_BULKSMS_USER = 'magedelightsmsnotification/general/bilksmsusername';
    const XML_PATH_BULKSMS_PASSWORD = 'magedelightsmsnotification/general/bilksmspassword';
    const XML_PATH_SMS_TYPE = 'magedelightsmsnotification/general/sms_on_event';
    const XML_PATH_API_GET_REQUEST = 'magedelightsmsprofile/general/api_get_request';
    const XML_PATH_ATTRIBUTE = 'magedelightsmsnotification/general/attribute';
    const XML_PATH_SMS_PHONE_ATTRIBUTE = 'magedelightsmsnotification/general/sms_phone_attr';

    /**  @var StoreManagerInterface */
    private $storeManager;

    /**  @var CurrencyInterface */
    private $localecurrency;
    
    /**
     * Constructor
     * @param Context $context
     * @param CurrencyInterface $localeCurrency
     * @param StoreManagerInterface $storeManager
     */

    public function __construct(
        Context $context,
        CurrencyInterface $localeCurrency,
        StoreManagerInterface $storeManager
    ) {
        $this->storeManager = $storeManager;
        $this->localecurrency = $localeCurrency;
        parent::__construct($context);
    }

    /** @return bool */

    public function getModuleStatus()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_ENABLE,
            ScopeInterface::SCOPE_STORE
        );
    }

    /**  @return string */

    public function getApiGateway()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_GATEWAY,
            ScopeInterface::SCOPE_STORE
        );
    }

   /**  @return string */

    public function getApiEndPoints()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_API_ENDPOINT,
            ScopeInterface::SCOPE_STORE
        );
    }

    /**  @return string */

    public function getApiCredential()
    {
        $credential_array = [];
        $credential = $this->scopeConfig->getValue(
            self::XML_PATH_API_CREDENTIAL,
            ScopeInterface::SCOPE_STORE
        );
        $paramArray =  explode(',', $credential);
        $credential_array[preg_replace('/^([^:]*).*$/', '$1', $paramArray[0])]  = substr($paramArray[0], strpos($paramArray[0], ":") + 1);
        $credential_array[preg_replace('/^([^:]*).*$/', '$1', $paramArray[1])]  = substr($paramArray[1], strpos($paramArray[1], ":") + 1);

        return $credential_array;
    }
    /**  @return string */

    public function getApiRequiredCountryCode()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_API_COUNTRY,
            ScopeInterface::SCOPE_STORE
        );
    }

    /**  @return array */
    public function getApiParams()
    {
        $param = [];
        $data = $this->scopeConfig->getValue(
            self::XML_PATH_API_ADDITINAL_PARAM,
            ScopeInterface::SCOPE_STORE
        );
        $dataArray =  explode(',', $data);
        foreach ($dataArray as $dataArray) {
            $param[preg_replace('/^([^:]*).*$/', '$1', $dataArray)] = substr($dataArray, strpos($dataArray, ":") + 1);
        }
        return $param;
    }

    /**  @return string */

    public function getApiTo()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_API_TO,
            ScopeInterface::SCOPE_STORE
        );
    }

    /**  @return string */

    public function getApiMessage()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_API_MESSAGE,
            ScopeInterface::SCOPE_STORE
        );
    }

    /**  @return string */
    
    public function getUrlToChangeStatus()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_API_ENDPOINT_STATUS,
            ScopeInterface::SCOPE_STORE
        );
    }

    public function getApiIntialStatus()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_API_INITIAL_STATUS,
            ScopeInterface::SCOPE_STORE
        );
    }

    public function getApiFailureStatus()
    {
        $fail =  $this->scopeConfig->getValue(
            self::XML_PATH_API_FIAL_STATUS,
            ScopeInterface::SCOPE_STORE
        );
        return explode(',', $fail);
    }

    public function getApiAdditionalParam()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_API_ADDITINAL_DATA,
            ScopeInterface::SCOPE_STORE
        );
    }

    public function getApiErrorkey()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_API_ERROR_KEY,
            ScopeInterface::SCOPE_STORE
        );
    }

    /**  @return string */

    public function getAccountSID()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_ACCOUNT_SID,
            ScopeInterface::SCOPE_STORE
        );
    }

    /**  @return string */

    public function getAuthToken()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_AUTH_TOKEN,
            ScopeInterface::SCOPE_STORE
        );
    }

    /**  @return string */

    public function getTwilioPhone()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_TWILIO_PHONE,
            ScopeInterface::SCOPE_STORE
        );
    }

    /**  @return string */

    public function getDefaultCountry()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_DEFAULT_COUNRTY,
            ScopeInterface::SCOPE_STORE
        );
    }

    /**  @return string */

    public function getAdminContactNumbers()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_ADMIN_CONTACT,
            ScopeInterface::SCOPE_STORE
        );
    }
    
    /** @return bool */

    public function getNotifyFailureStatus()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_ADMIN_NOTIFYFAILURE,
            ScopeInterface::SCOPE_STORE
        );
    }
    
    /**  @return array */

    public function getNotifyToMail()
    {
        $toMail =  $this->scopeConfig->getValue(
            self::XML_PATH_ADMIN_NOTIFYTOMAIL,
            ScopeInterface::SCOPE_STORE
        );
        $toMail = explode(",", $toMail);
        return $toMail;
    }

    /**  @return string */

    public function getNotifySenderMail()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_ADMIN_NOTIFYFROMMAIL,
            ScopeInterface::SCOPE_STORE
        );
    }

    /**  @return array */

    public function getAdminEvents()
    {
        $admin_events = $this->scopeConfig->getValue(
            self::XML_PATH_ADMIN_EVENTS,
            ScopeInterface::SCOPE_STORE
        );

        $admin_events_array = explode(",", $admin_events);
        return $admin_events_array;
    }

    /**  @return array */

    public function getCustomerEvents()
    {
        $customer_events = $this->scopeConfig->getValue(
            self::XML_PATH_CUSTOMER_EVENTS,
            ScopeInterface::SCOPE_STORE
        );

        $customer_events_array = explode(",", $customer_events);
        return $customer_events_array;
    }

    /**  @return bool */

    public function getSmsLogEnable()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_SMSLOG_ENABLE,
            ScopeInterface::SCOPE_STORE
        );
    }

     /** @return bool */

    public function getNotifyAdmin()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_NOTIFYADMIN_ENABLE,
            ScopeInterface::SCOPE_STORE
        );
    }

    /**  @return string */

    public function geSelectedCustomerNumber()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_SELECT_CUSTOMER_NO,
            ScopeInterface::SCOPE_STORE
        );
    }

    /**  @return string */

    public function getCustomerNumberMaxLength()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_PHONE_MAX_LENGTH,
            ScopeInterface::SCOPE_STORE
        );
    }

    /**  @return string */

    public function getCustomerNumberMinLength()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_PHONE_MIN_LENGTH,
            ScopeInterface::SCOPE_STORE
        );
    }

    /**  @return string */

    public function getNoticeBelowTelephone()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_PHONE_NOTICE,
            ScopeInterface::SCOPE_STORE
        );
    }

    /** @return bool */

    public function getCronStatus()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_SMSLOG_CRON_ENABLE,
            ScopeInterface::SCOPE_STORE
        );
    }

    /**  @return string */

    public function getBulkSmsUserName()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_BULKSMS_USER,
            ScopeInterface::SCOPE_STORE
        );
    }

    /**  @return string */
    
    public function getBulkSmsPassword()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_BULKSMS_PASSWORD,
            ScopeInterface::SCOPE_STORE
        );
    }

    /**  @return string */
    
    public function getBulkSmsUrl()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_BULKSMS_URL,
            ScopeInterface::SCOPE_STORE
        );
    }

    public function getPhoneValidationClass()
    {
        $maxLength = $this->getCustomerNumberMaxLength();
        $minLength = $this->getCustomerNumberMinLength();
        $validateMaxLength ='';
        $validateMinLength ='';
        if ($maxLength) {
            $validateMaxLength ='maximum-length-'.$maxLength;
        }
        if ($minLength) {
            $validateMinLength ='minimum-length-'.$minLength;
        }
        return 'validate-number custom-validate-length '.$validateMaxLength .' '.$validateMinLength;
    }

    /**  @return array */
    public function getOrderData($order)
    {
        $_order = $order->getOrder($order);
        if ($order->getGrandTotal()) {
            $total =$order->getGrandTotal();
        } else {
            $total = $order->getPayment()->getAmountOrdered();
        }
        
        $currency_code = $order->getOrderCurrencyCode();
        $currency_symbol = $this->localecurrency->getCurrency($currency_code)
                                ->getSymbol();

        $orderData          =   [
            'firstname'     =>  $order->getCustomerFirstname(),
            'lastname'      =>  $order->getCustomerLastname(),
            'order_id'      =>  ($_order) ? $_order->getIncrementId() : $order->getIncrementId(),
            'total'         =>  $currency_symbol.round($total, 2),
            'orderitem'     =>  $this->getOrderedItems($order->getAllItems()),
            'orderitemsizes'     =>  $this->getOrderedItemsdata($order->getAllVisibleItems()),
            'store'         => $this->getCurrentStoreName()
        ];
        return $orderData;
    }

    /**  @return array */
    public function getOrderedData($order)
    {
        $_order = $order->getOrder($order);
        if ($order->getGrandTotal()) {
            $total =$order->getGrandTotal();
        } else {
            $total = $order->getPayment()->getAmountOrdered();
        }
        
        $currency_code = $order->getOrderCurrencyCode();
        $currency_symbol = $this->localecurrency->getCurrency($currency_code)
                                ->getSymbol();

        $orderData          =   [
            'firstname'     =>  $order->getCustomerFirstname(),
            'lastname'      =>  $order->getCustomerLastname(),
            'order_id'      =>  ($_order) ? $_order->getIncrementId() : $order->getIncrementId(),
            'total'         =>  $currency_symbol.round($total, 2),
            'orderitem'     =>  $this->getOrderedItems($order->getAllVisibleItems()),
            'orderitemsizes'     =>  $this->getOrderedItemsdata($order->getAllVisibleItems()),
            'store'         => $this->getCurrentStoreName()
        ];
        return $orderData;
    }

    /**  @return string */
    public function getCurrentStoreName()
    {
        return $this->storeManager->getStore()->getName();
    }

    /**  @return string */
    public function getCurrentStoreId()
    {
        return $this->storeManager->getStore()->getId();
    }

    /**  @return string */
    public function getOrderedItems($items)
    {
        $order_items = [];
        foreach ($items as $item) {
            $order_items[] = $item->getName();
        }

        return implode(",", $order_items);
    }

    public function getOrderedItemsdata($items)
    {
        $order_items_sizes = [];
        foreach ($items as $item) {
            if($item->getProductOptions()){
                $order_items_sizes[] = $item->getProductOptions()['attributes_info'][1]['value'];
            }else{
                $order_items_sizes[] = '';
            }
        }

        return implode(",", $order_items_sizes);
    }

    /**  @return string */

    public function setOrderMesageText($message, $data)
    {
        $keywords   = [
            '{firstname}',
            '{lastname}',
            '{order_id}',
            '{total}',
            '{orderitem}',
            '{store}',
            '{orderitemsizes}'
        ];
        $message = str_replace($keywords, $data, $message);
        return $message;
    }

    public function getExtensionKey()
    {
        return 'ek-magento2-sms-notification';
    }
    
    public function getExtensionDisplayName()
    {
        return 'SMSNotification';
    }

     /**  @return string */

    public function getSmsType()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_SMS_TYPE,
            ScopeInterface::SCOPE_STORE
        );
    }

    /** @return bool */

    public function getAPIRequestInGet()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_API_GET_REQUEST,
            ScopeInterface::SCOPE_STORE
        );
    }

    public function checkPhoneAttribute()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_SMS_PHONE_ATTRIBUTE,
            ScopeInterface::SCOPE_STORE
        );
    }
     /**  @return string */

    public function getCustomerAttributecode()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_ATTRIBUTE,
            ScopeInterface::SCOPE_STORE
        );
    }

}

Can anyone point out what is the error and how to resolve that.

Any help is appriciated.

3
  • can you share Magedelight\SMSNotification\Helper\Data.php file ?
    – Msquare
    Oct 12 at 7:29
  • @Msquare Added the file Please check.
    – Prits
    Oct 12 at 7:32
  • @Prits Worked below solution?
    – Bojjaiah
    Oct 12 at 9:15
1

Try this

<?php

namespace Hl\Smsnotification\Helper;

use Magento\Framework\App\Helper\Context;
use Magento\Framework\Locale\CurrencyInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Model\ScopeInterface;
use Magedelight\SMSNotification\Helper\Data as MainHelper;

class Smsdata extends MainHelper
{
    private $storeManager;
    private $localecurrency;

    public function __construct(
        Context $context,
        CurrencyInterface $localeCurrency,
        StoreManagerInterface $storeManager
    ) {
        parent::__construct($context,$localeCurrency,$storeManager);
        $this->localecurrency = $localeCurrency;
        $this->storeManager = $storeManager;
    }

    ..................................................
    ..................................................
    ..................................................

app/code/Hl/Smsnotification/etc/di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magedelight\SMSNotification\Helper\Data" type="Hl\Smsnotification\Helper\Smsdata" />
</config>
1

Update the Smsdata.php constructor as below and try again.

public function __construct(
    Context $context,
    CurrencyInterface $localeCurrency,
    StoreManagerInterface $storeManager
) {
    $this->storeManager = $storeManager;
    $this->localecurrency = $localeCurrency;
    parent::__construct($context, $localeCurrency, $storeManager);
}

we should define/specify the parent constructor parameters in the child class always.

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.