0

Value get back from session. did'nt get values please someone review my code. need help

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 $_coreSession;

    protected $quoteValidator = null; 

     protected $vls = null;


    public function __construct(\Magento\Quote\Model\QuoteValidator $quoteValidator, \Magento\Framework\Session\SessionManagerInterface $coreSession)
    {
    $this->quoteValidator = $quoteValidator;
       $this->_coreSession = $coreSession;
    }



       public function getValue()
{
     $this->_coreSession->start();
    return $this->_coreSession->getMyValue(); //Get value from customer session
}



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




    global $total_cost;
       $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
$customerSession->setValue($total_cost); //set value in customer session
$vluas= $customerSession->getValue();

$this->vls = $vluas[0].$vluas[1];


    $exist_amount = 0; //$quote->getFee(); 
    $fee = $this->vls; //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)
    {



       global $total_cost;
       $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
$customerSession->setValue($total_cost); //set value in customer session
$vluas= $customerSession->getValue();

$this->vls = $vluas[0].$vluas[1];
//print_r($vls);

    return [
        'code' => 'fee',
        'title' => 'Shipping Price',
        'value' => $this->vls
    ];

    }

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

/home1/dukaania/public_html/testing2/vendor/magento/module-checkout/view/frontend/templates/cart/shipping.phtml

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

// @codingStandardsIgnoreFile

?>
<?php /** @var $block \Magento\Checkout\Block\Cart\Shipping */ ?>

<div id="block-shipping" class="block shipping" data-mage-init='{"collapsible":{"openedState": "active", "saveState": true}}'>
    <div class="title" data-role="title">
    <strong id="block-shipping-heading" role="heading" aria-level="2">
        <?= /* @escapeNotVerified */ $block->getQuote()->isVirtual() ? __('Estimate Tax') : __('Estimate Shipping and Tax') ?>
    </strong>
    </div>
    <div id="block-summary" data-bind="scope:'block-summary'" class="content" data-role="content" aria-labelledby="block-shipping-heading">

    <?php

 global $total_cost;
  global $value1;
$total_vendor_cost;

$this->_resources = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\App\ResourceConnection');
$connection= $this->_resources->getConnection();
$themeTable = $this->_resources->getTableName('mr_dukaaniatest');
$sql = "select * from  $themeTable ";
$result = $connection->fetchAll($sql);


foreach ($result as $results)

    //echo 'Value 1: ' .
    $value1 = $results['title'];
    echo '</br>';
    //echo 'Value 2: ' .
    $value2 = $results['author'];
// echo '</br>';




?>  
                <th class="col price" scope="col"><span><?php ?></span></th>

                <?php


$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 

 $items = $cart->getQuote()->getAllItems();

foreach($items as $item) {
    $itemname =  $item->getProductId();

    echo 'ID: '.
     $item->getProductId()
     //.'<br />';
    // echo "<br />";            

  ?>
                 <th class="col price" scope="col"><span><?php  ?></span></th>

                 <?php



}

$themeTable1 = $this->_resources->getTableName('catalog_product_entity_varchar');
$sql1 = "SELECT count(DISTINCT value) from  $themeTable1 where attribute_id=166 and entity_id = $itemname ";

$entities = $connection->fetchAll($sql1);

//$entity_name =  $entities['count'];
//var_dump($entity_name);


foreach ($entities as $row)
 echo 'Vendor Count: ' .
    $vendor_count = $row['count(DISTINCT value)'];
   echo '</br>';

    if($vendor_count > 1)
    {

     $vendor_counts = ($vendor_count - 1);

     $values_1 = $vendor_counts * $value2;

     $total_cost = $values_1 +$value1;

    echo 'Extra Fee Cost Total: '.
    $total_cost;
    }
    else
    {
    echo 'Extra Fee Cost: '.
       $total_cost = $value1;
    }

   // $entity_name =  $entity['value'];

    //var_dump($entities);








use Magento\Framework\Session\SessionManagerInterface as CoreSession;

class MyClass
{
     protected $_coreSession;

public function __construct(
    \Magento\Framework\Session\SessionManagerInterface $coreSession
    ){
    $this->_coreSession = $coreSession;
}





public function setValue($total_cost){
    $this->_coreSession->start();
    $this->_coreSession->setMyValue($total_cost);

}




}




?>




    <!-- ko template: getTemplate() --><!-- /ko -->
    <script type="text/x-magento-init">
        {
            "#block-summary": {
                "Magento_Ui/js/core/app": <?= /* @escapeNotVerified */ $block->getJsLayout() ?>
            }
        }
    </script>
    <script>
        window.checkoutConfig = <?= /* @escapeNotVerified */ $block->getSerializedCheckoutConfig() ?>;
        window.customerData = window.checkoutConfig.customerData;
        window.isCustomerLoggedIn = window.checkoutConfig.isCustomerLoggedIn;
        require([
            'mage/url',
            'Magento_Ui/js/block-loader'
        ], function(url, blockLoader) {
            blockLoader("<?= /* @escapeNotVerified */ $block->getViewFileUrl('images/loader-1.gif') ?>");
            return url.setBaseUrl('<?= /* @escapeNotVerified */ $block->getBaseUrl() ?>');
        })
    </script>
    </div>
</div>
0

Kindly use \Magento\Framework\Session\Generic class for default session,

public function __construct(
        \Magento\Quote\Model\QuoteValidator $quoteValidator,
        \Magento\Framework\Session\Generic $session
    )  {
        $this->quoteValidator = $quoteValidator;
        $this->_coreSession = $coreSession;
    }

    public function getValue()
    {
        return $this->_coreSession->getMyValue(); //Get value from customer session
    }

    public function setValue($total_cost)
    {
        return $this->_coreSession->setMyValue($total_cost);
    }
  • first time it shows me my default value that i set then it after another refresh it change it to 0 – jhon jhon doe Oct 8 '18 at 5:25
  • @rakesh I have followed the same concept but I am unable to retrieve on another page could you please help me on it. i am setting data on home page but unable to retrive on cart pages – Nagaraju K Dec 11 '19 at 14:50
  • Yes i have been facing same issue @jhonjhondoe do u have fix for this. could you please share me – Nagaraju K Dec 11 '19 at 15:12

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.