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

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.