0

I am getting errors in save method.

TypeError: Argument 1 passed to Codilar\OrderComment\Model\Api\OrderCommentUserRepository::save() must be an instance of Codilar\OrderComment\Model\OrderCommentUser, instance of Magento\Sales\Model\Order\Status\History\Interceptor given, called in /var/www/html/lal-magento/app/code/Codilar/OrderComment/Plugin/Sales/Model/Order.php on line 56 and defined in /var/www/html/lal-magento/app/code/Codilar/OrderComment/Model/Api/OrderCommentUserRepository.php:56 Stack trace: #0 /var/www/html/lal-magento/app/code/Codilar/OrderComment/Plugin/Sales/Model/Order.php(56): Codilar\OrderComment\Model\Api\OrderCommentUserRepository->save() #1

di.xml code is

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Model\Order\Status\History">
        <plugin name="AddAdminNameToComment" type="Codilar\OrderComment\Plugin\Sales\Model\Order" sortOrder="1" />
    </type>
    <preference for="Codilar\OrderComment\Api\OrderCommentUserRepositoryInterface" type="Codilar\OrderComment\Model\Api\OrderCommentUserRepository"/>
</config>

Plugin\Sales\Model\order.php code is

<?php
namespace Codilar\OrderComment\Plugin\Sales\Model;

use Codilar\OrderComment\Api\OrderCommentUserRepositoryInterface;
use Magento\Sales\Api\Data\OrderStatusHistoryInterface;
use Magento\Backend\Model\Auth\Session;

/**
 * Class Order
 * @package Codilar\OrderComment\Plugin\Sales\Model
 */
class Order
{
    /**
     * @var \Magento\Backend\Model\Auth\Session
     */
    protected $authSession;
    /**
     * @var OrderStatusHistoryInterface
     */
    private $orderStatusHistory;
    /**
     * @var OrderCommentUserRepositoryInterface
     */
    private $orderCommentUserRepository;

    /**
     * Order constructor.
     * @param \Magento\Backend\Model\Auth\Session $authSession
     * @param OrderStatusHistoryInterface $orderStatusHistory
     * @param OrderCommentUserRepositoryInterface $orderCommentUserRepository
     */
    public function __construct(
        Session $authSession,
        OrderStatusHistoryInterface $orderStatusHistory,
        OrderCommentUserRepositoryInterface $orderCommentUserRepository
    ){
        $this->authSession = $authSession;
        $this->orderStatusHistory = $orderStatusHistory;
        $this->orderCommentUserRepository = $orderCommentUserRepository;
    }

    /**
     * @param \Magento\Sales\Model\Order $subject
     * @param $comment
     * @param $status
     * @param $isVisibleOnFront
     */
    public function afterSave(\Magento\Sales\Model\Order\Status\History $subject, $result)
    {

        $adminId = $this->authSession->getUser()->getId();
        $model = $this->orderCommentUserRepository->create();
        $model->setData([
            'admin_id' => $adminId
        ]);
        $this->orderCommentUserRepository->save($model);
        return $result;
    }
}
4
  • Hi @Rk Rathod i am facing the error can you please help me. Aug 2, 2021 at 7:24
  • Share the Complete code on that file. Aug 2, 2021 at 10:19
  • where i am not understanding Aug 2, 2021 at 10:46
  • this file path : Plugin\Sales\Model\order.php Aug 2, 2021 at 10:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.