3

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;
     }

}

}

  • You have to ask to the same type questions time several time. Your code is not right.Please define model, resource class and collection class properly – Amit Bera Oct 1 '18 at 13:35
  • please can you review this code it returns me 0 /home1/dukaania/public_html/testing2/app/code/Sugarcode/Test/Model/Total/Fee.php – jhon jhon doe Oct 2 '18 at 5:09
  • sure ,, will do – Amit Bera Oct 2 '18 at 6:33
1

**You did not define Model, Resource model,Collection class properly.**You have 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
        );
    }

}
  • can you please review my code once it shows me 0 on shopping cart page /home1/dukaania/public_html/testing2/app/code/Sugarcode/Test/Model/Total/Fee.php – jhon jhon doe Oct 2 '18 at 4:19
1

I was facing the same issue On version 2.2.3 where is 2.2.2 work absolutely fine not sure but something change in 2.2.3 that the bellow does not accepts the use:

...

use \Vendor\Module\Model\Entity;
use \Vendor\Module\Model\ResourceModel\Entity as EntityResourceModel;

...

$this->_init(Entity::class,EntityResourceModel::class);

...

2.2.3 version changing to :

$this->_init(
        \Vendor\Module\Model\Entity::class,
        \Vendor\Module\Model\ResourceModel\Entity::class
        );

Works!

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.