cancel
Showing results for 
Search instead for 
Did you mean: 

Uncaught Error: Call to a member function getQuote() on null

Uncaught Error: Call to a member function getQuote() on null

I am getting an error:

Call to a member function getQuote() on null 

 

in app/code/Vendor/Module/Block/Rewrite/Catalog/Product/View/Options/Type/Select.php

protected $checkoutSession;

public function ___construct( \Magento\Checkout\Model\Session $checkoutSession ) { $this->checkoutSession = $checkoutSession; } public function getValuesHtml() { $quote = $this->checkoutSession->getQuote();

Do you have any suggestions?

13 REPLIES 13

Re: Uncaught Error: Call to a member function getQuote() on null

Hello  @tvgarden

 

If you are doing anything on the product page, it will not affect on the second time, the page is coming from cache.

 

This is not the answer of your question, but make sure if you are trying to do based on quote then it will not affect while page cache so better to do using ajax.

 

 


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Uncaught Error: Call to a member function getQuote() on null

Thank you.

 

I need to get postcode when a customer entered it.

Could it be done in Select.php or do I have to use a kind of observer?

Re: Uncaught Error: Call to a member function getQuote() on null

Hello,

 

You can do like this

 

After page load, call one ajax and get a response from it and do necessary thing.

 

If you've found one of my answers useful, please give "Kudos" or "Accept as Solution"


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Uncaught Error: Call to a member function getQuote() on null

So which file would be suitable for using?

form.phtml or a custom javascript file?

Which PHP file would be the one to use?

 

Sorry for keep asking, but I am quite new.

 

I only need a postcode value on the cart page.

 

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

        $shippingAddress = $cart->getQuote()->getShippingAddress();
        $postcode = $shippingAddress->getData('postcode');

 

This code returns an empty array.

Re: Uncaught Error: Call to a member function getQuote() on null

Hello @tvgarden

 

it is depends upon requirement.

 

You can do like this call one html before body end

<referenceContainer name="before.body.end">
    <block class="Magento\Framework\View\Element\Html" template="yout template file"> 
    </block>
</referenceContainer>

 you can place into your catalog_product_view xml and into that template file you need to call that ajax and necessary step.

 

If you've found one of my answers useful, please give "Kudos" or "Accept as Solution"


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Uncaught Error: Call to a member function getQuote() on null

Thank you very much. I will make an ajax call.

 

The problem is the php is not returning postcode.

Is it because the code is in product/options/type and I am using it on cart page?

 

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

        $shippingAddress = $cart->getQuote()->getShippingAddress();
        $postcode = $shippingAddress->getData('postcode');

Re: Uncaught Error: Call to a member function getQuote() on null

Sorry this is the cart page and not the product page.

I need to get postcode on the cart page.

Need to know how to get postcode entered on the page.

Screen Shot 2018-04-23 at 12.07.50.png

Re: Uncaught Error: Call to a member function getQuote() on null

Hello @tvgarden

 

Can you please share image using online tool?


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Uncaught Error: Call to a member function getQuote() on null

Thank you I have done it like this, and it works:

require(['jquery', 
"Magento_Customer/js/customer-data",
 'Magento_Checkout/js/checkout-data'], 
 
   function($, 
      getPaymentInformationAction,
       customerData,   
  checkoutData){

function getPostcode(){

   var cartData = customerData.get('cart-data')(); 
    var checkout = checkoutData.getShippingAddressFromData(); 
console.log(checkout); return checkout.postcode; } });

However how I can detect postcode change?

jquery doesn't seem to work.

Sorry my image was not visible...