1

I'm trying to save the value of some custom customer attributes programmatically.

I have seen a few examples and everyone is proposing to create a controller for this. But I would like to save the attribute value without creating a controller and without calling it from the Object Manager.

In Magento 1 the code was this and it was enough to reload the page for it to work:

$customer = Mage::getSingleton('customer/session')->getCustomer()
$customer->setmyattribute(date('Y'));
$customer->save();

In my Magento 2 this is my code that i tried but not work:

namespace vendor\module\Block;

class SaveData
{

protected $customer;

protected $customerRepository;

public function __construct(
    \Magento\Customer\Model\Customer $customer,
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
)
{
    $this->customer = $customer;
    $this->customerRepository = $customerRepository;
}

public function saveDatagruppo()
{   
    $customer_id = 3;

    $customer_repo = $this->customerRepository->create();
    $customer = $customer_repo->getById($customer_id);

    // get Custome Attribute value
    $cust_att = $customer->getCustomAttribute("datagruppo")->getValue();
    print_r($cust_att);

    //set custom Attribute value
    $add_id = "2021";
    $customer->setCustomAttribute("datagruppo",$add_id);
    $customer_repo->save($customer);

}
}

And this is the code in frontend

$blockPHPCuong = $block->getLayout()->createBlock('\vendor\module\Block\SaveData');
$blockPHPCuong->saveDatagruppo();

How can I do so that I can save my attribute on page load? Thanks

3
  • check this link --- magento.stackexchange.com/a/200365/85907 Commented Jul 14, 2020 at 8:36
  • @MohitPatel Man i see and i tried but not work. Please see my update question!
    – Jackom
    Commented Jul 14, 2020 at 9:04
  • you save data in code write in block file like $custom->save(); . not call in other .phtml file Commented Jul 14, 2020 at 9:09

2 Answers 2

0

Try this code

$customer_id = 3;

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerRepository = $objectManager->create('Magento\Customer\Api\CustomerRepositoryInterface');
$customer = $customerRepository->getById($customer_id);

// get Custome Attribute value
$cust_att = $customer->getCustomAttribute("datagruppo")->getValue();
print_r($cust_att);

//set custom Attribute value
$add_id = "Value here";
$customer->setCustomAttribute("datagruppo",$add_id);
$customerRepository->save($customer);

Other Way

<?php

namespace vendor\module\Block;

use Magento\Framework\View\Element\Template;

class SaveData extends Template
{
    protected $customer;

    protected $customerRepository;

    public function __construct(
        Template\Context $context,
        \Magento\Customer\Model\Customer $customer,
        \Magento\Customer\Model\ResourceModel\CustomerFactory $customerFactory,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->customer = $customer;
        $this->customerRepository = $customerRepository;
    }
    public function saveDatagruppo()
    {
        $customer_id = 3;

        $customer_repo = $this->customerRepository->create();
        $customer_repo = $this->customerRepository->getById($customer_id);

        //set custom Attribute value
        $add_id = "2022";
        $customer_repo->setCustomAttribute("datagruppo",$add_id);
        $customer_repo->save();
        //$this->customerRepository->save($customer_repo);
    }
}

I Hope This Helps You.

0
-1

Much easier, guys

$customer->setSomeAttribute('val');
$customer->getResource()->saveAttribute($customer, 'some_attribute');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.