0

I'm trying to use Magento best practices by not using Object manager, and instead loading what I need through my blocks.

I've got this code in my newsletter.php block file:

    protected $_customerRepositoryInterface;
public function __construct(\Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface) {
    $this->_customerRepositoryInterface = $customerRepositoryInterface;
}

public function saveAttrs() {
    $customerId = "3";
    $customer = $this->customer->load($customerId);
    $data = 1;
    $customerData = $customer->getDataModel();
    $customerData->setCustomAttribute('new_products_attr', $data);
    $customer->updateData($customerData);
    $customerResource = $this->customerFactory->create();
    $customerResource->saveAttribute($customer, 'new_products_attr');
}

Which should work according to many posts, but I get this error:

Fatal error: Uncaught Error: Call to a member function dispatch() on null in C:\xampp\htdocs\hooplaHooray\vendor\magento\framework\View\Element\AbstractBlock.php:652 Stack trace: #0

I've tried this solution:

public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Customer\Model\Session $customerSession,  
\Magento\Framework\ObjectManagerInterface $objectManager,
array $data = []
) {
parent::__construct($context, $data);
$this->customerSession = $customerSession;
$this->_objectManager = $objectManager;
}

But that gives me this error:

Uncaught TypeError: Argument 2 passed to Magento\Customer\Block\Account\Dashboard::__construct() must be an instance of Magento\Customer\Model\Session, array given, called in C:\xampp\htdocs\hooplaHooray\vendor\magento\module-customer\Block\Newsletter.php on line 49 and defined in C:\xampp\htdocs\hooplaHooray\vendor\magento\module-customer\Block\Account\Dashboard.php:54 Stack trace: #0

Really stuck on this one, this is my first attempt at using constructs and functions so please bear with me.

Edit

Full code of block file:

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

use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;

/**
* Customer front  newsletter manage block
*
* @api
* @SuppressWarnings(PHPMD.DepthOfInheritance)
* @since 100.0.2
*/
class Newsletter extends \Magento\Customer\Block\Account\Dashboard
{
protected $_customerFactory;
protected $_addressFactory;

public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Customer\Model\AddressFactory $addressFactory,
        array $data = []
      ) {
        $this->_customerFactory = $customerFactory;
        $this->_addressFactory = $addressFactory;
        parent::__construct($context, $data);
}

public function getCustomerById($id) {
        return $this->_customerFactory->create()->load($id);
    }
public function saveAttrs() {
    $customer = $this->getCustomerById(3);
    $data = 1;
    $customerData = $customer->getDataModel();
    $customerData->setCustomAttribute('new_products_attr', $data);
    $customer->updateData($customerData);
    $customerResource = $this->customerFactory->create();
    $customerResource->saveAttribute($customer, 'new_products_attr');
}
/**
 * @var string
 */
protected $_template = 'form/newsletter.phtml';

/**
 * @return bool
 * @SuppressWarnings(PHPMD.BooleanGetMethodName)
 */
public function getIsSubscribed()
{
    return $this->getSubscriptionObject()->isSubscribed();
}

/**
 * Return the save action Url.
 *
 * @return string
 */
public function getAction()
{
    return $this->getUrl('newsletter/manage/save');
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.