Magento 2 How to add new address in 'customer_address_entity' from helper?
I am using helper data function to add the new address in customer_address_entity
but it is not working. code as below :
<?php
namespace NameModuleHelper;
use MagentoFrameworkAppHelperAbstractHelper;
class Data extends AbstractHelper
{
protected $addressDataFactory;
protected $addressRepository;
public function __construct(
MagentoCustomerApiDataAddressInterface $addressDataFactory,
MagentoCustomerApiAddressRepositoryInterface $addressRepository
) {
$this->addressDataFactory = $addressDataFactory;
$this->addressRepository = $addressRepository;
}
public function assignAddressToCustomer($location,$customer)
{
$address = $this->addressDataFactory;
$address->setFirstname($customer->getData('firstname'))
->setLastname($customer->getData('lastname'))
->setCustomerId($customer->getData('entity_id'))
->setCountryId($location->getData('country_id'))
->setRegionId($location->getData('region_id'))
//->setRegion($location->getData('region'))
->setCity($location->getData('city'))
->setPostcode($location->getData('postcode'))
->setStreet(array($location->getData('street')))
->setTelephone($location->getData('telephone'))
->setData('save_in_address_book', '1');
try{
$this->addressRepository->save($address);
}catch (Exception $e) {
Zend_Debug::dump($e->getMessage());
}
return;
}
}
It goes in try section, but in the database, there is no update.
Is there something missing?
i am trying but it gives error as below :
it gives error in apache log "PHP Fatal error:
Uncaught Exception: User Error: Some transactions have not been committed or rolled back in C:\xampp\htdocs\M2\vendor\magento\framework\DB\Adapter\Pdo\Mysql.php on line 3955 in C:\xampp\htdocs\M2\vendor\magento\framework\App\ErrorHandler.php:61nStack trace:n#0 [internal function]: Magento\Framework\App\ErrorHandler->handler(256, 'Some transactio...', 'C:\\xampp\\htdocs...', 3955, Array)n#1 C:\xampp\htdocs\M2\vendor\magento\framework\DB\Adapter\Pdo\Mysql.php(3955): trigger_error('Some transactio...', 256)n#2 [internal function]: Magento\Framework\DB\Adapter\Pdo\Mysql->__destruct()n#3 {main}n thrown in C:\xampp\htdocs\M2\vendor\magento\framework\App\ErrorHandler.php on line 61, referer: http://127.0.0.1/M2/admin/customer/index/new/key/1f129dbde8115d6f0c4789e039353302bbf9c039d78881f155a623a083f49d70/"
magento2 customer eav customer-address helper
add a comment |
I am using helper data function to add the new address in customer_address_entity
but it is not working. code as below :
<?php
namespace NameModuleHelper;
use MagentoFrameworkAppHelperAbstractHelper;
class Data extends AbstractHelper
{
protected $addressDataFactory;
protected $addressRepository;
public function __construct(
MagentoCustomerApiDataAddressInterface $addressDataFactory,
MagentoCustomerApiAddressRepositoryInterface $addressRepository
) {
$this->addressDataFactory = $addressDataFactory;
$this->addressRepository = $addressRepository;
}
public function assignAddressToCustomer($location,$customer)
{
$address = $this->addressDataFactory;
$address->setFirstname($customer->getData('firstname'))
->setLastname($customer->getData('lastname'))
->setCustomerId($customer->getData('entity_id'))
->setCountryId($location->getData('country_id'))
->setRegionId($location->getData('region_id'))
//->setRegion($location->getData('region'))
->setCity($location->getData('city'))
->setPostcode($location->getData('postcode'))
->setStreet(array($location->getData('street')))
->setTelephone($location->getData('telephone'))
->setData('save_in_address_book', '1');
try{
$this->addressRepository->save($address);
}catch (Exception $e) {
Zend_Debug::dump($e->getMessage());
}
return;
}
}
It goes in try section, but in the database, there is no update.
Is there something missing?
i am trying but it gives error as below :
it gives error in apache log "PHP Fatal error:
Uncaught Exception: User Error: Some transactions have not been committed or rolled back in C:\xampp\htdocs\M2\vendor\magento\framework\DB\Adapter\Pdo\Mysql.php on line 3955 in C:\xampp\htdocs\M2\vendor\magento\framework\App\ErrorHandler.php:61nStack trace:n#0 [internal function]: Magento\Framework\App\ErrorHandler->handler(256, 'Some transactio...', 'C:\\xampp\\htdocs...', 3955, Array)n#1 C:\xampp\htdocs\M2\vendor\magento\framework\DB\Adapter\Pdo\Mysql.php(3955): trigger_error('Some transactio...', 256)n#2 [internal function]: Magento\Framework\DB\Adapter\Pdo\Mysql->__destruct()n#3 {main}n thrown in C:\xampp\htdocs\M2\vendor\magento\framework\App\ErrorHandler.php on line 61, referer: http://127.0.0.1/M2/admin/customer/index/new/key/1f129dbde8115d6f0c4789e039353302bbf9c039d78881f155a623a083f49d70/"
magento2 customer eav customer-address helper
add a comment |
I am using helper data function to add the new address in customer_address_entity
but it is not working. code as below :
<?php
namespace NameModuleHelper;
use MagentoFrameworkAppHelperAbstractHelper;
class Data extends AbstractHelper
{
protected $addressDataFactory;
protected $addressRepository;
public function __construct(
MagentoCustomerApiDataAddressInterface $addressDataFactory,
MagentoCustomerApiAddressRepositoryInterface $addressRepository
) {
$this->addressDataFactory = $addressDataFactory;
$this->addressRepository = $addressRepository;
}
public function assignAddressToCustomer($location,$customer)
{
$address = $this->addressDataFactory;
$address->setFirstname($customer->getData('firstname'))
->setLastname($customer->getData('lastname'))
->setCustomerId($customer->getData('entity_id'))
->setCountryId($location->getData('country_id'))
->setRegionId($location->getData('region_id'))
//->setRegion($location->getData('region'))
->setCity($location->getData('city'))
->setPostcode($location->getData('postcode'))
->setStreet(array($location->getData('street')))
->setTelephone($location->getData('telephone'))
->setData('save_in_address_book', '1');
try{
$this->addressRepository->save($address);
}catch (Exception $e) {
Zend_Debug::dump($e->getMessage());
}
return;
}
}
It goes in try section, but in the database, there is no update.
Is there something missing?
i am trying but it gives error as below :
it gives error in apache log "PHP Fatal error:
Uncaught Exception: User Error: Some transactions have not been committed or rolled back in C:\xampp\htdocs\M2\vendor\magento\framework\DB\Adapter\Pdo\Mysql.php on line 3955 in C:\xampp\htdocs\M2\vendor\magento\framework\App\ErrorHandler.php:61nStack trace:n#0 [internal function]: Magento\Framework\App\ErrorHandler->handler(256, 'Some transactio...', 'C:\\xampp\\htdocs...', 3955, Array)n#1 C:\xampp\htdocs\M2\vendor\magento\framework\DB\Adapter\Pdo\Mysql.php(3955): trigger_error('Some transactio...', 256)n#2 [internal function]: Magento\Framework\DB\Adapter\Pdo\Mysql->__destruct()n#3 {main}n thrown in C:\xampp\htdocs\M2\vendor\magento\framework\App\ErrorHandler.php on line 61, referer: http://127.0.0.1/M2/admin/customer/index/new/key/1f129dbde8115d6f0c4789e039353302bbf9c039d78881f155a623a083f49d70/"
magento2 customer eav customer-address helper
I am using helper data function to add the new address in customer_address_entity
but it is not working. code as below :
<?php
namespace NameModuleHelper;
use MagentoFrameworkAppHelperAbstractHelper;
class Data extends AbstractHelper
{
protected $addressDataFactory;
protected $addressRepository;
public function __construct(
MagentoCustomerApiDataAddressInterface $addressDataFactory,
MagentoCustomerApiAddressRepositoryInterface $addressRepository
) {
$this->addressDataFactory = $addressDataFactory;
$this->addressRepository = $addressRepository;
}
public function assignAddressToCustomer($location,$customer)
{
$address = $this->addressDataFactory;
$address->setFirstname($customer->getData('firstname'))
->setLastname($customer->getData('lastname'))
->setCustomerId($customer->getData('entity_id'))
->setCountryId($location->getData('country_id'))
->setRegionId($location->getData('region_id'))
//->setRegion($location->getData('region'))
->setCity($location->getData('city'))
->setPostcode($location->getData('postcode'))
->setStreet(array($location->getData('street')))
->setTelephone($location->getData('telephone'))
->setData('save_in_address_book', '1');
try{
$this->addressRepository->save($address);
}catch (Exception $e) {
Zend_Debug::dump($e->getMessage());
}
return;
}
}
It goes in try section, but in the database, there is no update.
Is there something missing?
i am trying but it gives error as below :
it gives error in apache log "PHP Fatal error:
Uncaught Exception: User Error: Some transactions have not been committed or rolled back in C:\xampp\htdocs\M2\vendor\magento\framework\DB\Adapter\Pdo\Mysql.php on line 3955 in C:\xampp\htdocs\M2\vendor\magento\framework\App\ErrorHandler.php:61nStack trace:n#0 [internal function]: Magento\Framework\App\ErrorHandler->handler(256, 'Some transactio...', 'C:\\xampp\\htdocs...', 3955, Array)n#1 C:\xampp\htdocs\M2\vendor\magento\framework\DB\Adapter\Pdo\Mysql.php(3955): trigger_error('Some transactio...', 256)n#2 [internal function]: Magento\Framework\DB\Adapter\Pdo\Mysql->__destruct()n#3 {main}n thrown in C:\xampp\htdocs\M2\vendor\magento\framework\App\ErrorHandler.php on line 61, referer: http://127.0.0.1/M2/admin/customer/index/new/key/1f129dbde8115d6f0c4789e039353302bbf9c039d78881f155a623a083f49d70/"
magento2 customer eav customer-address helper
magento2 customer eav customer-address helper
edited Dec 26 '18 at 6:00
Ashish Viradiya
681621
681621
asked Dec 14 '18 at 14:54
Utsav GuptaUtsav Gupta
377115
377115
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I changed into the helper function and its working now. I found the solution as below in hlper function:
public function assignAddressToCustomer($location,$customer) {
$defaultAddress = $this->addressFactory->create();
$defaultAddress->setData('street', $location->getData('street'));
$defaultAddress->setData('city', $location->getData('city'));
$defaultAddress->setData('country_id', $location->getData('country_id'));
$defaultAddress->setData('postcode', $location->getData('postcode'));
$defaultAddress->setData('region', $location->getData('region'));
$defaultAddress->setData('region_id', $location->getData('region_id'));
$defaultAddress->setData('telephone', $location->getData('telephone'));
$defaultAddress->setData('fax', $location->getData('fax'));
$defaultAddress->setData('company', $location->getData('company'));
$defaultAddress->setData('firstname', $customer->getData('firstname'));
$defaultAddress->setData('lastname', $customer->getData('lastname'));
$defaultAddressObj = $defaultAddress->getDataModel();
$defaultAddressObj->setCustomAttribute('custom_id', $location->getData('custom_id'));
$defaultAddress->updateData($defaultAddressObj);
$defaultAddress->save();
return;
}
Also we need to add dependency for addressFactory as below:
MagentoCustomerModelAddressFactory $addressFactory
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f253691%2fmagento-2-how-to-add-new-address-in-customer-address-entity-from-helper%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I changed into the helper function and its working now. I found the solution as below in hlper function:
public function assignAddressToCustomer($location,$customer) {
$defaultAddress = $this->addressFactory->create();
$defaultAddress->setData('street', $location->getData('street'));
$defaultAddress->setData('city', $location->getData('city'));
$defaultAddress->setData('country_id', $location->getData('country_id'));
$defaultAddress->setData('postcode', $location->getData('postcode'));
$defaultAddress->setData('region', $location->getData('region'));
$defaultAddress->setData('region_id', $location->getData('region_id'));
$defaultAddress->setData('telephone', $location->getData('telephone'));
$defaultAddress->setData('fax', $location->getData('fax'));
$defaultAddress->setData('company', $location->getData('company'));
$defaultAddress->setData('firstname', $customer->getData('firstname'));
$defaultAddress->setData('lastname', $customer->getData('lastname'));
$defaultAddressObj = $defaultAddress->getDataModel();
$defaultAddressObj->setCustomAttribute('custom_id', $location->getData('custom_id'));
$defaultAddress->updateData($defaultAddressObj);
$defaultAddress->save();
return;
}
Also we need to add dependency for addressFactory as below:
MagentoCustomerModelAddressFactory $addressFactory
add a comment |
I changed into the helper function and its working now. I found the solution as below in hlper function:
public function assignAddressToCustomer($location,$customer) {
$defaultAddress = $this->addressFactory->create();
$defaultAddress->setData('street', $location->getData('street'));
$defaultAddress->setData('city', $location->getData('city'));
$defaultAddress->setData('country_id', $location->getData('country_id'));
$defaultAddress->setData('postcode', $location->getData('postcode'));
$defaultAddress->setData('region', $location->getData('region'));
$defaultAddress->setData('region_id', $location->getData('region_id'));
$defaultAddress->setData('telephone', $location->getData('telephone'));
$defaultAddress->setData('fax', $location->getData('fax'));
$defaultAddress->setData('company', $location->getData('company'));
$defaultAddress->setData('firstname', $customer->getData('firstname'));
$defaultAddress->setData('lastname', $customer->getData('lastname'));
$defaultAddressObj = $defaultAddress->getDataModel();
$defaultAddressObj->setCustomAttribute('custom_id', $location->getData('custom_id'));
$defaultAddress->updateData($defaultAddressObj);
$defaultAddress->save();
return;
}
Also we need to add dependency for addressFactory as below:
MagentoCustomerModelAddressFactory $addressFactory
add a comment |
I changed into the helper function and its working now. I found the solution as below in hlper function:
public function assignAddressToCustomer($location,$customer) {
$defaultAddress = $this->addressFactory->create();
$defaultAddress->setData('street', $location->getData('street'));
$defaultAddress->setData('city', $location->getData('city'));
$defaultAddress->setData('country_id', $location->getData('country_id'));
$defaultAddress->setData('postcode', $location->getData('postcode'));
$defaultAddress->setData('region', $location->getData('region'));
$defaultAddress->setData('region_id', $location->getData('region_id'));
$defaultAddress->setData('telephone', $location->getData('telephone'));
$defaultAddress->setData('fax', $location->getData('fax'));
$defaultAddress->setData('company', $location->getData('company'));
$defaultAddress->setData('firstname', $customer->getData('firstname'));
$defaultAddress->setData('lastname', $customer->getData('lastname'));
$defaultAddressObj = $defaultAddress->getDataModel();
$defaultAddressObj->setCustomAttribute('custom_id', $location->getData('custom_id'));
$defaultAddress->updateData($defaultAddressObj);
$defaultAddress->save();
return;
}
Also we need to add dependency for addressFactory as below:
MagentoCustomerModelAddressFactory $addressFactory
I changed into the helper function and its working now. I found the solution as below in hlper function:
public function assignAddressToCustomer($location,$customer) {
$defaultAddress = $this->addressFactory->create();
$defaultAddress->setData('street', $location->getData('street'));
$defaultAddress->setData('city', $location->getData('city'));
$defaultAddress->setData('country_id', $location->getData('country_id'));
$defaultAddress->setData('postcode', $location->getData('postcode'));
$defaultAddress->setData('region', $location->getData('region'));
$defaultAddress->setData('region_id', $location->getData('region_id'));
$defaultAddress->setData('telephone', $location->getData('telephone'));
$defaultAddress->setData('fax', $location->getData('fax'));
$defaultAddress->setData('company', $location->getData('company'));
$defaultAddress->setData('firstname', $customer->getData('firstname'));
$defaultAddress->setData('lastname', $customer->getData('lastname'));
$defaultAddressObj = $defaultAddress->getDataModel();
$defaultAddressObj->setCustomAttribute('custom_id', $location->getData('custom_id'));
$defaultAddress->updateData($defaultAddressObj);
$defaultAddress->save();
return;
}
Also we need to add dependency for addressFactory as below:
MagentoCustomerModelAddressFactory $addressFactory
edited 30 mins ago
Teja Bhagavan Kollepara
2,94841847
2,94841847
answered Dec 26 '18 at 5:42
Utsav GuptaUtsav Gupta
377115
377115
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f253691%2fmagento-2-how-to-add-new-address-in-customer-address-entity-from-helper%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown