cancel
Showing results for 
Search instead for 
Did you mean: 

Call third party module block function to custom module block - Error: Call to a member function get

Call third party module block function to custom module block - Error: Call to a member function get

Third party module Block:

 

Path: app\code\Amasty\Storelocator\Block\View\Attributes.php

<?php
/**
 * @author Amasty Team
 * @copyright Copyright (c) 2021 Amasty (https://www.amasty.com)
 * @package Amasty_Storelocator
 */


namespace Amasty\Storelocator\Block\View;

use Magento\Framework\View\Element\Template;

/**
 * Class Attributes
 */
class Attributes extends Template
{
    /**
     * Show attributes
     *
     * @return string
     */
    public function toHtml()
    {
        if (!$this->getLocationAttributes()) {
            return '';
        }

        return parent::toHtml();
    }

    public function getLocationAttributes()
    {
    
               //logger
               $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/log1.log');
               $logger = new \Zend\Log\Logger();
               $logger->addWriter($writer);
               $logger->info($this->getLocation()->getAttributes());

                   
        return $this->getLocation()->getAttributes();
    }
}

log file return values

 

Try to call getLocationAttributes() in my custom module,

 

Path : app\code\Zero\Storelocator\Block\Customerreview.php

<?php
namespace Zero\Storelocator\Block;
use Amasty\Storelocator\Model\ConfigProvider;


class Customerreview extends \Magento\Framework\View\Element\Template
{
     protected $helper;
     protected $_amastyconfigProvider;

        
     public function __construct(        
        \Amasty\Storelocator\Block\view\Attributes $attributes,
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Amasty\Storelocator\Block\View\Attributes $myValue,
        \Amasty\Storelocator\Block\View\Location $myValue1,
        ConfigProvider $amastyConfigProvider,     
        array $data = []
     ) {   
        $this->_attributes = $attributes;     
        $this->scopeConfig = $scopeConfig;
        $this->_amastyconfigProvider     =   $amastyConfigProvider;
        parent::__construct($context, $data);
    }
  
    protected function _prepareLayout()
    {
        parent::_prepareLayout();
        
    }

    public function getCustomerReview(){   
            
       
        $authentication = $this->scopeConfig->getValue('customerreview/general/review_auth', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
        
        $accountId = $this->scopeConfig->getValue('customerreview/general/review_account_id', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

         // try to print values from third party module
         $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/fin.log');
         $logger = new \Zend\Log\Logger();
         $logger->addWriter($writer);
         $logger->info(print_r($this->_attributes->getLocationAttributes())); // print the values
           
        $authorization = "Authorization:".$authentication;
          
        $url = 'https://mybusiness.googleapis.com/v4/accounts/'.$accountId.'/locations/123456789/reviews';

        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        $output = curl_exec($ch);        
        $allData = json_decode($output, TRUE); // You will get all the data
        return $allData;
    }
}

I am getting following error:

 

Error: Call to a member function getAttributes() on null in C:\xampp\htdocs\m3\app\code\Amasty\Storelocator\Block\View\Attributes.php:39

 

39th line -> $logger->info($this->getLocation()->getAttributes());

 

How to solve this error, i want to get my third party module function values to my custom module?

1 REPLY 1

Re: Call third party module block function to custom module block - Error: Call to a member function

Hi @Aveeva ,

 

Could you please replace your custom block code with below code and check

class Customerreview extends \Magento\Framework\View\Element\Template
{
     protected $helper;
     protected $_amastyconfigProvider;
     protected $storeAttributes;

        
     public function __construct(        
        \Amasty\Storelocator\Block\view\Attributes $storeAttributes,
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        ConfigProvider $amastyConfigProvider,     
        array $data = []
     ) {   
        $this->storeAttributes = $storeAttributes,
        $this->scopeConfig = $scopeConfig;
        $this->_amastyconfigProvider     =   $amastyConfigProvider;
        parent::__construct($context, $data);
    }
  
    protected function _prepareLayout()
    {
        parent::_prepareLayout();
        
    }

    public function getCustomerReview(){   
            
       
        $authentication = $this->scopeConfig->getValue('customerreview/general/review_auth', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
        
        $accountId = $this->scopeConfig->getValue('customerreview/general/review_account_id', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

         // try to print values from third party module
         $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/fin.log');
         $logger = new \Zend\Log\Logger();
         $logger->addWriter($writer);
         $logger->info(print_r($this->storeAttributes->getLocationAttributes())); // print the values
           
        $authorization = "Authorization:".$authentication;
          
        $url = 'https://mybusiness.googleapis.com/v4/accounts/'.$accountId.'/locations/123456789/reviews';

        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        $output = curl_exec($ch);        
        $allData = json_decode($output, TRUE); // You will get all the data
        return $allData;
    }
}

Hope this helps you!

Problem Solved! Click Kudos & Accept as Solution!