0

I am showing customer available customer credit limit and used customer credit limit in my custom module. When I run the php bin/magento cache:clean it's working and showing correct value. When I didn't run the above command it's showing old values, the recent values are not showing.

Could you please suggest me how to resolve this?

for this I have written below code.

Block/CreditLimit.php

<?php
namespace Vendor\Namespace\Block;

use Magento\Customer\Api\CustomerRepositoryInterface;


class Creditlimit extends \Magento\Framework\View\Element\Template
{


    /**
     * @var CustomerRepositoryInterface
     */
    protected $customerRepository;


    /**
     * @var \Magento\Customer\Model\SessionFactory
     */
    protected $_customerSession;

    /**
     * @var \Magento\Sales\Model\Order
     */
    protected $_salesOrder;

    /**
     * @var \Vendor\Namespace\Model\CreditlimitusedlogFactory
     */
    protected  $_creditlimitusedLogFactory;


    public function __construct
    (
        \Magento\Framework\View\Element\Template\Context $context,
        CustomerRepositoryInterface $customerRepository,
        \Magento\Customer\Model\SessionFactory $customerSession,
        \Vendor\Namespace\Model\CreditlimitusedlogFactory $creditlimitusedlogFactory,
        array $data = []
    )
    {
        parent::__construct($context, $data);
        $this->_customerSession = $customerSession;
        $this->_creditlimitusedLogFactory = $creditlimitusedlogFactory;
        $this->customerRepository = $customerRepository;

    }

    /**
     * Returns available credit limit used of customer
     *
     * @return mixed
     */
    public function getAvailableCreditLimit() {

        $creditLimits = 0;

        $customerSession = $this->_customerSession->create();
        $customerId = $customerSession->getCustomerId();
        $customer = $this->customerRepository->getById($customerId);
        $customAttributes = $customer->getCustomAttributes();

        foreach($customAttributes as $k => $v){
            if($v->getAttributeCode() == 'credit_limit'){
                $creditLimits = $v->getValue();
            }
        }

        if($creditLimits > 0){
            $creditLimit = $creditLimits;
        }else{
            $creditLimit = 0;
        }

       $customer_credit_limit = number_format($creditLimit, 2, '.', '');
        return $customer_credit_limit;
    }

    /**
     * Returns Used Credit Limit amout of Customer
     *
     * @return mixed
     */
    public function getUsedCreditLimit(){

        $order_total = 0;

        $customerSession = $this->_customerSession->create();
        $customerId = $customerSession->getCustomerId();
        $creditlimit = $this->_creditlimitusedLogFactory->create()->getCollection()->addFieldToFilter('customer_id', array('eq' => $customerId));

        foreach ($creditlimit as $_creditlimit) {
            $order_grand_total = $_creditlimit->getData('order_grand_total');
            $invoiced_grand_total = $_creditlimit->getData('invoiced_grand_total');
            $cal_total = $order_grand_total - $invoiced_grand_total;

            if($cal_total > 0){
                $order_total = $order_total + $cal_total;
            }
        }

        $customer_used_credit_limit = number_format($order_total, 2, '.', '');

        return $customer_used_credit_limit;

    }


}

view/frontend/layout/creditlimit_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <update handle="customer_account"/>
    <body>
        <referenceBlock name="page.main.title">
            <action method="setPageTitle">
                <argument translate="true" name="title" xsi:type="string">Credit Limit</argument>
            </action>
        </referenceBlock>
        <referenceContainer name="content">
            <block class="Vendor\Namespace\Block\Creditlimit" name="creditlimit" template="Vendor_Namespace::Creditlimit.phtml" />
        </referenceContainer>
    </body>
</page>

view/frontend/templates/Creditlimit.phtml

<?php /** @var $block Vendor\Namespace\Block\Creditlimit */?>
<br/>
<?php

$command = 'php bin/magento cache:clean';
shell_exec($command);

?>
<span style="font-weight: bold;">Available Credit Limit: $</span><?php echo $block->getAvailableCreditLimit(); ?>
<br/>
<span style="font-weight: bold;">Used Credit Limit: $</span><?php echo $block->getUsedCreditLimit(); ?>

Even though I have written the php bin/magento cache:clean command in .phtml file. But No luck.

Any help on this?

1

Replace your layout code

<block class="Vendor\Namespace\Block\Creditlimit" name="creditlimit" template="Vendor_Namespace::Creditlimit.phtml" />

to

<block class="Vendor\Namespace\Block\Creditlimit" name="creditlimit" template="Vendor_Namespace::Creditlimit.phtml" cacheable="false" />

I have added cacheable="false" in above code

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.