0

I am using magento customer session outside magento. I have added section to signup and login from outside. But I dont know how to edit the customer accounts from outside magento. This code, I used to get customer values from customer account.

require_once ("../app/Mage.php");
umask(0);
Mage::app();
Mage::app()->loadArea('frontend');
Mage::getSingleton('core/session', array('name' => 'frontend'));
$layout = Mage::getSingleton('core/layout');
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
    $customer = Mage::getModel('customer/customer')->load(Mage::getSingleton('customer/session')->getId())->getData();
    $address = Mage::getModel("customer/address");
    $shipping = $address->load($customer['default_shipping'])->getData();
    ?>
    <form action="" method="post" name="thisForm">
<input type="hidden" name="customerId" id="customerId" value="<?= $customer['entity_id'] ?>" />
<input type="text" name="customerEmail" id="customerEmail" value="<?= $customer['email'] ?>" />
<input type="text" name="customerFirstname" id="customerFirstname" value="<?= $customer['firstname'] ?>" />
<input type="text" name="customerLastname" id="customerLastname" value="<?= $customer['lastname'] ?>" />
<input type="hidden" name="customerDefault_billing" id="customerDefault_billing" value="<?= $customer['default_billing'] ?>" />
<input type="hidden" name="customerDefault_shipping" id="customerDefault_shipping" value="<?= $customer['default_shipping'] ?>" />
<input type="hidden" name="shippingId" id="shippingId" value="<?= $shipping['entity_id'] ?>" />
<input type="hidden" name="shippingParent" id="shippingParent" value="<?= $shipping['parent_id'] ?>" />
<input type="hidden" name="shippingFirstname" id="shippingFirstname" value="<?= $shipping['firstname'] ?>" />
<input type="hidden" name="shippingLastname" id="shippingLastname" value="<?= $shipping['lastname'] ?>" />
<input type="text" name="shippingStreet" id="shippingStreet" value="<?= $shipping['street'] ?>" />
<?php
$collection = Mage::getModel('directory/region')->getResourceCollection()
                ->addCountryFilter('IN')->load();
?>
<select class="txtbox" name="shippingRegion" id="shippingRegion">
    <?php foreach ($collection as $region) { ?>
        <option value="<?= $region['region_id'] ?>" <?php if ($region['region_id'] == $shipping['region_id']) { ?> selected="selected" <?php } ?>><?= $region['name'] ?></option>
    <?php } ?>
</select>
<input type="text" name="shippingTelephone" id="shippingTelephone" value="<?= $shipping['telephone'] ?>" />
<input type="text" name="shippingRegion" id="shippingRegion" value="<?= $shipping['region'] ?>" />
<input type="text" name="shippingCity" id="shippingCity" value="<?= $shipping['city'] ?>" />
<input type="text" name="shippingPostcode" id="shippingPostcode" value="<?= $shipping['postcode'] ?>" />
<input type="text" name="shippingCountryId" id="shippingCountryId" value="<?= $shipping['country_id'] ?>" /> 

How to update the values in the form into Magento customer account?

2
  • What are the issue?
    – Amit Bera
    Commented Jun 8, 2015 at 7:03
  • @AmitBera How to edit customer account from outside Magento directory like personal info and address details? Commented Jun 8, 2015 at 7:06

2 Answers 2

3

change Mage::app(); to Mage::app('admin'); for current store as admin.

setWebsiteId(1) //put as static 

If want set to Customer data then use

$customer = Mage::getModel('customer/customer')->load($customerId);

Code:

$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($customer_email);

// check customer is exits.

if ($customer->getId()) {
/* load Customer by primary key for better save customer data */

    $customerId=$customer->getId();
    $customer = Mage::getModel('customer/customer')->load($customerId); 
   // set store as 
    $customer->setStore(Mage::app()->getStore()->getId());              
    $customer->setEmail($customer_email);
    $customer->setFirstname($customer_fname);
    $customer->setLastname($customer_lname);
    $customer->setPassword($customer_password);

    try {

        $customer->save();
        $customer->setConfirmation(null);
        $customer->save();
        $customer->sendNewAccountEmail();
    } catch (Exception $ex) {

    }
}

For more details Check inchoo blog : http://inchoo.net/magento/programming-magento/programmaticaly-adding-new-customers-to-the-magento-store/

1
  • Hi Amit, I have updated the question, How to update that form values[customer info & address ] in customer account Magento ? Commented Jun 9, 2015 at 3:59
1

I did the customer update section outside the Magento directory like this,

<?php
require_once ("../../app/Mage.php");
umask(0);
Mage::app();
Mage::app()->loadArea('frontend');
Mage::getSingleton('core/session', array('name' => 'frontend'));
$layout = Mage::getSingleton('core/layout');
$_SESSION[msg] = "";
if (isset($_POST['updateCustomer'])) {
    $customerId = $_POST['customerId'];
    $customer_email = $_POST['customerEmail'];
    $customer_fname = $_POST['customerFirstname'];
    $customer_lname = $_POST['customerLastname'];
    $customer_password = $_POST['customerPassword'];
    $shipping_id = $_POST['shippingId'];
    $shippingParent = $_POST['shippingParent'];
    $street = $_POST['shippingStreet'];
    $city = $_POST['shippingCity'];
    $postal = $_POST['shippingPostcode'];
    $telephone = $_POST['shippingTelephone'];
    $region = $_POST['shippingRegion'];
    $country = 'IN';
    $customer = Mage::getModel('customer/customer')->load($customerId);
    $customer->setWebsiteId(Mage::app()->getWebsite()->getId());
    if ($customer->getId()) {
        $customer->setEmail($customer_email);
        $customer->setFirstname($customer_fname);
        $customer->setLastname($customer_lname);
        if ($customer_password != '') {
            $customer->setPassword($customer_password);
        }
        try {
            $customer->save();
            $customer->setConfirmation(null);
            $customer->save();
            $_SESSION['username'] = $customer_email;
            $_SESSION['firstname'] = $customer_fname;
            $_SESSION['first_order'] = "Y";
            $_SESSION['user_id'] = $customer->getId();
            $customer->sendNewAccountEmail();
        } catch (Exception $ex) {

        }
        $address = Mage::getModel("customer/address")->load($shipping_id);
        $address->setCustomerId($shippingParent)
                ->setShippingId($shipping_id)
                ->setFirstname($customer->getFirstname())
                ->setMiddleName($customer->getMiddlename())
                ->setLastname($customer->getLastname())
                ->setCountryId($country)
                ->setRegionId($region)
                ->setPostcode($postal)
                ->setCity($city)
                ->setTelephone($telephone)
                ->setStreet($street)
                ->setIsDefaultBilling('1')
                ->setIsDefaultShipping('1')
                ->setSaveInAddressBook('1');
        try {
            $address->save();
            $address->setConfirmation(null);
            $address->save();
        } catch (Exception $e) {

        }
    } else {
        echo "User does not exist";
    }
}

Form:

if (Mage::getSingleton('customer/session')->isLoggedIn()) {
    $customer = Mage::getModel('customer/customer')->load(Mage::getSingleton('customer/session')->getId())->getData();
    $address = Mage::getModel("customer/address");
    $shipping = $address->load($customer['default_shipping'])->getData();
}
?>
<form action="" method="post" name="thisForm">
    <input type="hidden" name="customerId" id="customerId" value="<?= $customer['entity_id'] ?>" />
    <input type="text" name="customerEmail" id="customerEmail" value="<?= $customer['email'] ?>" />
    <input type="text" name="customerFirstname" id="customerFirstname" value="<?= $customer['firstname'] ?>" />
    <input type="text" name="customerLastname" id="customerLastname" value="<?= $customer['lastname'] ?>" />
    <input type="hidden" name="customerDefault_billing" id="customerDefault_billing" value="<?= $customer['default_billing'] ?>" />
    <input type="hidden" name="customerDefault_shipping" id="customerDefault_shipping" value="<?= $customer['default_shipping'] ?>" />
    <input type="hidden" name="shippingId" id="shippingId" value="<?= $shipping['entity_id'] ?>" />
    <input type="hidden" name="shippingParent" id="shippingParent" value="<?= $shipping['parent_id'] ?>" />
    <input type="hidden" name="shippingFirstname" id="shippingFirstname" value="<?= $shipping['firstname'] ?>" />
    <input type="hidden" name="shippingLastname" id="shippingLastname" value="<?= $shipping['lastname'] ?>" />
    <input type="text" name="shippingStreet" id="shippingStreet" value="<?= $shipping['street'] ?>" />
    <?php
    $collection = Mage::getModel('directory/region')->getResourceCollection()
                    ->addCountryFilter('IN')->load();
    ?>
    <select class="txtbox" name="shippingRegion" id="shippingRegion">
        <?php foreach ($collection as $region) { ?>
            <option value="<?= $region['region_id'] ?>" <?php if ($region['region_id'] == $shipping['region_id']) { ?> selected="selected" <?php } ?>><?= $region['name'] ?></option>
        <?php } ?>
    </select>
    <input type="text" name="shippingTelephone" id="shippingTelephone" value="<?= $shipping['telephone'] ?>" />
    <input type="text" name="shippingRegion" id="shippingRegion" value="<?= $shipping['region'] ?>" />
    <input type="text" name="shippingCity" id="shippingCity" value="<?= $shipping['city'] ?>" />
    <input type="text" name="shippingPostcode" id="shippingPostcode" value="<?= $shipping['postcode'] ?>" />
    <input type="text" name="shippingCountryId" id="shippingCountryId" value="<?= $shipping['country_id'] ?>" /> 
</form>

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.