0

Please someone review my code and make necessary changes in it. Basically, i want to replace my static code to dyanmic in model.

Here's my code:

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

<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Sugarcode\Test\Model\Total;


class Fee extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
{
   /**
     * 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; 

    public function __construct(\Magento\Quote\Model\QuoteValidator $quoteValidator)
    {
        $this->quoteValidator = $quoteValidator;
    }
  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)
    {





        return [
            'code' => 'fee',
            'title' => 'Fee',
            'value' => 200
        ];
    }

    /**
     * Get Subtotal label
     *
     * @return \Magento\Framework\Phrase
     */
    public function getLabel()
    {
        return __('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);

            var_dump($collection);
         }else{
             return false;
         }

    }
}

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

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

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

<?php
namespace Sugarcode\Test\Model;
use Magento\Cron\Exception;
use Magento\Framework\Model\AbstractModel;
class Test extends AbstractModel
{
    /**
     * @var \Magento\Framework\Stdlib\DateTime
     */
    protected $_dateTime;

    /**
     * @return void
     */
    protected function _construct()
    {
        $this->_init(\Sugarcode\Test\Model\ResourceModel\Test::class);
    }
}

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

<?php
namespace Sugarcode\Test\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class Test extends AbstractDb
{
    public function _construct()
    {
        $this->_init('mr_dukaaniatest', 'dukaaniatest_id');
    }
}
  • What and where you want to change? – Amit Bera Oct 1 '18 at 8:11
  • I want to show dynamic values on that page /home1/dukaania/public_html/testing2/app/code/Sugarcode/Test/Model/Total/Fee.php replace those 200 values that set on static – jhon jhon doe Oct 1 '18 at 8:29
  • the value will be fetched from where? Sugarcode\Test\Model\ResourceModel\Test\Collection ? – Amit Bera Oct 1 '18 at 8:32
  • the value fetch from /home1/dukaania/public_html/testing2/app/code/Sugarcode/Test/Controller/Index/Index.php – jhon jhon doe Oct 1 '18 at 8:34
  • can i call data from database and show on same page sam like as: /home1/dukaania/public_html/testing2/app/code/Sugarcode/Test/Model/Total/Fee.php – jhon jhon doe Oct 1 '18 at 8:36
0

I guess that you have to call Collection class Sugarcode\Test\Model\ResourceModel\Test\Collection for fetch data for mr_dukaaniatest table at app/code/Sugarcode/Test/Model/Total/Fee

and then filter the collection by customer id which you will get from Magento\Quote\Model\Quote $quote

<?php


namespace Sugarcode\Test\Model\Total;

class Fee extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal {

    /**
     * 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\Test\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('TitleFieldName'),
            'value' => $firstItem->getData('fieldName')
        ];
        }else{
            return [
            'code' => 'fee',
            'title' => 'Fee',
            'value' => 0
        ];
        }



    }

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

}

You have to change TitleFieldName and fieldName repect to database field.

'title' => $firstItem->getData('TitleFieldName'),
'value' => $firstItem->getData('fieldName')
  • Quote is not tested . it give base on idea – Amit Bera Oct 1 '18 at 9:24
  • error Exception #0 (ReflectionException): Class Sugarcode\Test\Model\ResourceModel\Test\CollectionFactory does not exist Exception #1 (ReflectionException): Class Sugarcode\Test\Model\ResourceModel\Test\CollectionFactory does not exist Exception #2 (RuntimeException): Source class "\Sugarcode\Test\Model\ResourceModel\Test\Collection" for "Sugarcode\Test\Model\ResourceModel\Test\CollectionFactory" generation does not exist. – jhon jhon doe Oct 1 '18 at 9:41
  • have u did di complie? – Amit Bera Oct 1 '18 at 10:33
  • yes i run the di compile command as well but nothing happen – jhon jhon doe Oct 1 '18 at 10:37

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.