0

I want to fetch custom table data and show on cart page. Don't need to do from customer id (session). Please review my code and give me a solution.

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;
use Magento\Framework\DataObject;
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;
 protected $customerSession;

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

}

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 = 10; //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 = $this->customerSession->getCustomer()->getId();
//$customerId = $quote->getCustomerId();

$rewardspointCollection->addFieldToSelect('*')->addFieldToFilter('dukaaniatest_id',$customerId);
if(empty($rewardspointCollection)){
   return "Data not found";
} else {
    return 'your collection data';
}
}
/**
 * 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;


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

}

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

<?php
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');
}
}

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

<?php
namespace Sugarcode\Test\Model;


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

}

/home1/dukaania/public_html/testing2/app/code/Sugarcode/Test/Block/Sales/Order/Fee.php

<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

/**
* Tax totals modification block. Can be used just as subblock of 
\Magento\Sales\Block\Order\Totals
*/
namespace Sugarcode\Test\Block\Sales\Order;



class Fee extends \Magento\Framework\View\Element\Template
{
/**
 * Tax configuration model
 *
 * @var \Magento\Tax\Model\Config
 */
protected $_config;

/**
 * @var Order
 */
protected $_order;

/**
 * @var \Magento\Framework\DataObject
 */
protected $_source;

/**
 * @param \Magento\Framework\View\Element\Template\Context $context
 * @param \Magento\Tax\Model\Config $taxConfig
 * @param array $data
 */
public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Tax\Model\Config $taxConfig,
    array $data = []
) {
    $this->_config = $taxConfig;
    parent::__construct($context, $data);
}

/**
 * Check if we nedd display full tax total info
 *
 * @return bool
 */
public function displayFullSummary()
{
    return true;
}

/**
 * Get data (totals) source model
 *
 * @return \Magento\Framework\DataObject
 */
public function getSource()
{
    return $this->_source;
} 
public function getStore()
{
    return $this->_order->getStore();
}

  /**
 * @return Order
 */
public function getOrder()
{
    return $this->_order;
}

/**
 * @return array
 */
public function getLabelProperties()
{
    return $this->getParentBlock()->getLabelProperties();
}

/**
 * @return array
 */
public function getValueProperties()
{
    return $this->getParentBlock()->getValueProperties();
}

/**
 * Initialize all order totals relates with tax
 *
 * @return \Magento\Tax\Block\Sales\Order\Tax
 */
 public function initTotals()
{

    $parent = $this->getParentBlock();
    $this->_order = $parent->getOrder();
    $this->_source = $parent->getSource();

    $store = $this->getStore();

    $fee = new \Magento\Framework\DataObject(
            [
                'code' => 'fee',
                'strong' => false,
                'value' => 120,
                //'value' => $this->_source->getFee(),
                'label' => __('Fee'),
            ]
        );

        $parent->addTotal($fee, 'fee');
       // $this->_addTax('grand_total');


        return $this;

       }

    }
  • What is your database Name and primary_key please ? – PЯINCƏ Oct 2 '18 at 9:12
  • mr_dukaaniatest database name – jhon jhon doe Oct 2 '18 at 9:12
  • primary_key please – PЯINCƏ Oct 2 '18 at 9:15
  • dukaaniatest_id – jhon jhon doe Oct 2 '18 at 9:16
0

app/code/Sugarcode/Test/Model/Total/Fee.php

<?php
namespace Sugarcode\Test\Model\Total;
class Fee extends \Magento\Framework\Model\AbstractModel implements \Magento\Framework\DataObject\IdentityInterface
{
    const CACHE_TAG = 'mr_dukaaniatest';

    protected $_cacheTag = 'mr_dukaaniatest';

    protected $_eventPrefix = 'mr_dukaaniatest';

    protected function _construct()
    {
        $this->_init('Sugarcode\Test\Model\ResourceModel\Fee.php');
    }

    ...

app/code/Sugarcode/Test/Model/ResourceModel/Fee.php

<?php
namespace Sugarcode\Test\Model\ResourceModel;


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

    public function __construct(
        \Magento\Framework\Model\ResourceModel\Db\Context $context
    )
    {
        parent::__construct($context);
    }

    protected function _construct()
    {
        $this->_init('mr_dukaaniatest', 'dukaaniatest_id');
    }

}

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

<?php
namespace Sugarcode\Test\Model\ResourceModel\Fee;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
    protected $_idFieldName = 'dukaaniatest_id';
    protected $_eventPrefix = 'mr_dukaaniatest_collection';
    protected $_eventObject = 'fee_collection';

    /**
     * Define resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('Sugarcode\Test\Model\Total\Fee', 'Sugarcode\Test\Model\ResourceModel\Fee');
    }

}

Now you can get your data values from your database where you want to retrieve them by injecting the modelFactory dependency like this:

protected $_feeFactory;

public function __construct(
        \Sugarcode\Test\Model\Total\FeeFactory $feeFactory
    ) {
        parent::__construct($context)
        $this->_feeFactory = $feeFactory;
    }

    public function execute()
    {
        $fee = $this->_feeFactory->create();
        $collection = $fee->getCollection();
        foreach($collection as $item){
            \Zend_Debug::dump($item->getData());
        }
        exit();
    }
  • where i put this modelFactory class ? – jhon jhon doe Oct 2 '18 at 10:25
  • In your constructor __construct everything is explained – PЯINCƏ Oct 2 '18 at 11:14
  • so basically i need to iverwrite this class with your code /home1/dukaania/public_html/testing2/app/code/Sugarcode/Test/Model/Total/Fee.php – jhon jhon doe Oct 2 '18 at 11:20
  • facing this error Fatal error: Class Sugarcode\Test\Model\Total\Fee contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Magento\Framework\DataObject\IdentityInterface::getIdentities) in /home/dukaania/public_html/testing2/app/code/Sugarcode/Test/Model/Total/Fee.php on line 7 – jhon jhon doe Oct 2 '18 at 11:35

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.