Fix Error: Does Not Extend \Magento\Framework\DataObject

cartdatabase

Facing this error Exception #0 (InvalidArgumentException): Sugarcode\Test\Model\Fee does not extend \Magento\Framework\DataObject

Here's my code

/home1/dukaania/public_html/testing2/app/code/Sugarcode/Test/Model/Total/Fee.php

/**
 * Collect grand total address amount
 *
 * @param \Magento\Quote\Model\Quote $quote
 * @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment
 * @param \Magento\Quote\Model\Quote\Address\Total $total
 * @return $this
 */
protected $quoteValidator = null;

protected $rewardspointCollectionFactory;

public function __construct(
        \Magento\Quote\Model\QuoteValidator $quoteValidator,
        \Sugarcode\Test\Model\ResourceModel\Fee\CollectionFactory $rewardspointCollectionFactory
        ) {
    $this->quoteValidator = $quoteValidator;
    $this->rewardspointCollectionFactory = $rewardspointCollectionFactory;
}

public function collect(
\Magento\Quote\Model\Quote $quote, \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment, \Magento\Quote\Model\Quote\Address\Total $total
) {
    parent::collect($quote, $shippingAssignment, $total);


    $exist_amount = 0; //$quote->getFee(); 
    $fee = 200; //Excellence_Fee_Model_Fee::getFee();
    $balance = $fee - $exist_amount;

    $total->setTotalAmount('fee', $balance);
    $total->setBaseTotalAmount('fee', $balance);

    $total->setFee($balance);
    $total->setBaseFee($balance);

    $total->setGrandTotal($total->getGrandTotal() + $balance);
    $total->setBaseGrandTotal($total->getBaseGrandTotal() + $balance);


    return $this;
}

protected function clearValues(Address\Total $total) {
    $total->setTotalAmount('subtotal', 0);
    $total->setBaseTotalAmount('subtotal', 0);
    $total->setTotalAmount('tax', 0);
    $total->setBaseTotalAmount('tax', 0);
    $total->setTotalAmount('discount_tax_compensation', 0);
    $total->setBaseTotalAmount('discount_tax_compensation', 0);
    $total->setTotalAmount('shipping_discount_tax_compensation', 0);
    $total->setBaseTotalAmount('shipping_discount_tax_compensation', 0);
    $total->setSubtotalInclTax(0);
    $total->setBaseSubtotalInclTax(0);
}

/**
 * @param \Magento\Quote\Model\Quote $quote
 * @param Address\Total $total
 * @return array|null
 */

/**
 * Assign subtotal amount and label to address object
 *
 * @param \Magento\Quote\Model\Quote $quote
 * @param Address\Total $total
 * @return array
 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
 */
public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total) {


    $rewardspointCollection = $this->rewardspointCollectionFactory->create();
    $customerId = $quote->getCustomerId();
    $rewardspointCollection->addFieldToSelect('*')
            ->addFieldToFilter('dukaaniatest_id',$customerId);
    if(count($rewardspointCollection) > 0){
       $firstItem =  $rewardspointCollection->geFirstItem();
       return [
        'code' => 'fee',
        'title' => $firstItem->getData('title'),
        'value' => $firstItem->getData('author')
    ];
    }else{
        return [
        'code' => 'fee',
        'title' => 'Fee',
        'value' => 0
    ];
    }



}

/**
 * Get Subtotal label
 *
 * @return \Magento\Framework\Phrase
 */
public function getLabel() {
    return __('Fee');
}
}

/home1/dukaania/public_html/testing2/app/code/Sugarcode/Test/Model/ResourceModel/Fee/Collection.php

<?php
namespace Sugarcode\Test\Model\ResourceModel\Fee;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Magento\Framework\DataObject;
class Collection extends AbstractCollection
{
/**
 * Initialize resource collection
 *
 * @return void
 */
public function _construct()
{
    $this->_init('Sugarcode\Test\Model\Fee', 
        'Sugarcode\Test\Model\ResourceModel\Fee');
}
}

/home1/dukaania/public_html/testing2/app/code/Sugarcode/Test/Controller/Index/Index.php

<?php
namespace Sugarcode\Test\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
protected $rewardspointsFactory;
protected $customerSession ;
public function _construct(
    \Sugarcode\Test\Model\RewardspointsFactory $rewardspointsFactory,
    \Magento\Customer\Model\Session $customerSession 
){
    $this->rewardspointsFactory = $rewardspointsFactory;
    $this->customerSession = $customerSession;
}

public function rewardPoints()
{
     if($this->customerSession->isLoggedIn()) {

        $Rewardspoints = $this->rewardspointsFactory->create();
        $collection = $Rewardspoints->getCollection();
        // customerId
        $customerId = $this->customerSession->getId();
        return $collection->addFieldToFilter('dukaaniatest_id',$customerId);


     }else{
         return false;
     }

}

}

Best Answer

You did not define Model, Resource Model, Collection class properly. You have to define properly as below:

Model Class: app/code/Sugarcode/Test/Model/Fee.php

<?php
/**
 * User: Amit Bera
 * Email: dev.amitbera@gmail.com
 */

namespace Sugarcode\Test\Model;


class Fee extends  \Magento\Framework\Model\AbstractModel
{
    public function _construct()
    {
        $this->_init(\Sugarcode\Test\Model\ResourceModel\Fee::class);
    }

}

Resource Model Class: app/code/Sugarcode/Test/Model/ResourceModel/Fee.php

<?php
/**
 * User: Amit Bera
 * Email: dev.amitbera@gmail.com
 */

namespace Sugarcode\Test\Model\ResourceModel;


class Fee extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{

    /**
     * Resource initialization
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('mr_dukaaniatest', 'dukaaniatest_id');
    }
}

Collection Class: app/code/Sugarcode/Test/Model/ResourceModel/Fee/Collection.php

<?php
/**
 * User: Amit Bera
 * Email: dev.amitbera@gmail.com
 */

namespace Sugarcode\Test\Model\ResourceModel\Fee;


class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
    public function _construct()
    {
        $this->_init(
            \Sugarcode\Test\Model\Fee::class,
            \Sugarcode\Test\Model\ResourceModel\Fee::class
        );
    }

}
Related Topic