Possible way of getting address from checkout and make it default - magento

i'm trying to get address from checkout page for a registered customer. on the checkout page I have a form that a user has to fill in with address info. Now the trick is to get that address displayed on the account dashboard.
I did try to get the address on the quote on .phtml here is my code
$quote = Mage::getSingleton('checkout/session')->getQuote();
$quote_billing = $quote->getBillingAddress();
echo $quote_billing
Is there any other possible way to get and address from a session

Try:
$customer = Mage::getSingleton('customer/session')->getCustomer();
foreach ($customer->getAddresses() as $address) {
Zend_Debug::dump($address->getData());
}

You could also do what you did and just do getData() on the $quote_billing. That should provide you with the data that you need to pull.

Related

how can i fetch cart data from joomla session?

Hi I am working on a module where i have to fetch cart data from the session. For that I am use below mention code. I am able to fetch the data but all in one. Now my question is how do I get those data individually(product_id individually cart id individually etc)? Please guide me regarding this issue.
Code
$_data=unserialize($_SESSION['__vm']['vmcart']);
print_r($_data);
did you check the vm cart module? your solution seams to be there.
try :
foreach ($_data->products as $product){
//var_dump($product);
echo $product->virtuemart_product_id.' || '.$product->product_name.' || '.$product->product_price;
}
I got the solution with help of below mentioned code
$cart = VirtueMartCart::getCart(); //getting cart object
$cmpny=$cart->BT; // accessing cart's elements
foreach($cmpny as $key =>$data)
{
some usfull code
}

Magento returning incorrect customer data on frontend pages

isn't this the right method to get Name of logged in customer?
<?php echo Mage::helper('customer')->getCustomer()->getName(); ?>
I have a website with live chat functionality. Yesterday I have been asked to pass email address and the name of the logged into the user into the Javascript Tracking variable code placed in the head section of the website. So that the operators could see who is on the website and whom are they talking to without any need to ask about their information.
So I passed the information from Magento into the Javascript code but now I see this very strange thing happening. For example,
If I am logged in with credentials Name = John Email =
john12#yahoo.com
Then This name and email variable values are changing with the change of pages. For example if I click on any product page the variable values which I am passing changes to some other user's information.
Name becomes Ricky Email becomes ricky23#gmail.com
this variable values are kept on changing back to john and from john to something else with the change of pages. So operator does not have any idea whom are they talking because the values are kept on changing. Also, user ricky or who ever it changes to also exist in the database. so it is picking up random person from the database.
This is what i did to pass the code to javascript. Please let me know if that is not the right code to pass the information. Please check the php code I am using to fetch information from Magento. Roughly, I receive incorrect value once in 5 times. Please provide some assistance. Thanks in advance.
<?php
$customer = Mage::getSingleton('customer/session')->getCustomer();
$email = $customer->getEmail();
$firstname = $customer->getFirstname();
$lastname= $customer->getLastname();
$name = $firstname . ' ' . $lastname;
?>
<script type="text/javascript">
if (typeof(lpMTagConfig) == "undefined"){ lpMTagConfig = {};}
if (typeof(lpMTagConfig.visitorVar) == "undefined"){ lpMTagConfig.visitorVar = [];}
lpMTagConfig.visitorVar[lpMTagConfig.visitorVar.length] = 'Email=<?php echo $email; ?>';
lpMTagConfig.visitorVar[lpMTagConfig.visitorVar.length] = 'Name=<?php echo $name; ?>';
</script>
I'm also attaching a snap shot
I'd be interested to hear how you're adding this code to the page? Is it in it's own block, or are you adding it to footer.phtml, or similar? If your adding to an existing block be sure to check the block caching settings of that template.
To confirm the caching hypothesis I'd ask the following:
Do you get the same name, all the time, on the same page? When you refresh the page, do you get the same name and email in the Javascript?
Does the problem persist with caching disabled?
This doesn't sound like a singleton problem at all. Each execution of the PHP script is isolated from the others, serving one page request. There's no chance of another customer's object moving between invokations of the script.
It is a matter of understanding the singleton pattern. If you call your code twice:
$customer_1 = Mage::helper('customer')->getCustomer()->getName();
$customer_2 = Mage::helper('customer')->getCustomer()->getName();
you get two different instances of the object. But... if one of them has already implemented a singleton pattern in its constructor or has implemented a singleton getInstance then both objects will actually point to the same thing.
Looking at the customer/helper/Data.php code you can see the function
public function getCustomer()
{
if (empty($this->_customer)) {
$this->_customer = Mage::getSingleton('customer/session')->getCustomer();
}
return $this->_customer;
}
That means that in one of the cases singleton is already implemented/called and in other one - not as the property is already set.
The correct way to work with quote/customer/cart in order to get always the correct data is always to use the singleton pattern.
So using this:
$customer = Mage::getSingleton('customer/session')->getCustomer();
always guarantee that you get the correct customer in that session. And as may be you know singleton pattern is based on registry pattern in app/Mage.php:
public static function getSingleton($modelClass='', array $arguments=array())
{
$registryKey = '_singleton/'.$modelClass;
if (!self::registry($registryKey)) {
self::register($registryKey, self::getModel($modelClass, $arguments));
}
return self::registry($registryKey);
}
and looking at app/Mage.php:
public static function register($key, $value, $graceful = false)
{
if (isset(self::$_registry[$key])) {
if ($graceful) {
return;
}
self::throwException('Mage registry key "'.$key.'" already exists');
}
self::$_registry[$key] = $value;
}
...
public static function registry($key)
{
if (isset(self::$_registry[$key])) {
return self::$_registry[$key];
}
return null;
}
you can see that Magento checks is it is already set. If so, Magento will either throw an Exception, which is the default behavior or return null.
Hope this will help you to understand the issue you face.
I have sorted this out. I have moved the code from footer.phtml to head.phtml and it's working fine now.Values are not changing anymore. If anyone know the logic behind please post and I will change my answer. So far this is working.

Magento Programmatically Edit Order Payment

I need to make a change of payment method to an order after it is placed. I have the order ID ($orderID), the order object ($order), a proper payment object, etc.
$service->retrievePaymentType() Returns the payment in the form of Mage_Sales_Model_Order_Payment
All of this happens in an extension of Mage_Checkout_Model_Type_Onepage
Does anybody know how I would go about doing this?
$order = Mage::getModel('sales/order')->load($orderID);
$service = Mage::getModel('sales/service_quote', $this->getQuote());
// Update Saved Order Payment Method
// $order->getPaymentsCollection()->clear();
$order->setPayment($service->retrievePaymentType());
$order->getPaymentsCollection()->save();
$order->save();
Thanks in advance!
Unfortunately, I had to do a direct SQL query, which is not Magento spec, but it gets the job done. If someone want's the code, leave me a comment, and I will dig it up.
Thanks though!
EDIT:
I managed to in fact get this working with Magento API:
// The payment type I want to change the target order to
$service = Mage::getModel('sales/service_quote', $this->getQuote());
$payment = $service->retrievePaymentType();
$paymentData = $payment->getData();
$oldPayment = $order->getAllPayments();
$oldPayment = $oldPayment[0];
foreach ($paymentData as $n => $v) {
$oldPayment->setData($n,$v);
}
It is a little bit hackish, but pretty effective.

Magento - redirect user group after login

How can I redirect different user groups to different pages in Magento ?
what I want to do is to assign a category to each customer group to redirect the user to it on login.
Thanks a lot.
I have not tried this. I think you need to do the following on the customer_login event:
$session = Mage::getSingleton('customer/session');
$category = // A category object based on $session->getCustomerGroupId()
$session->setBeforeAuthUrl($category->getUrl());
Go to /app/code/local/MagentoPycho/Customer/controllers/AccountController.php and replace this:
$session->setBeforeAuthUrl(Mage::helper('customer')->getAccountUrl());
with this:
$groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
if($groupId == 5) {
$session->setBeforeAuthUrl('http://google.com');
} else {
// Set default URL to redirect customer to
$session->setBeforeAuthUrl(Mage::helper('customer')->getAccountUrl());
}
Be sure to put your Group ID instead of the "5" and your redirect URL instead of google.com.
I just tried it.
How about this for an approach:
Consider starting with an existing module such as:
http://www.magentocommerce.com/magento-connect/MagePsycho/extension/3763/custom_login_redirect
Then adding your own logic. For the group name of a customer you can try this:
$groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
$group = Mage::getModel ('customer/group')->load($groupId)->getCode();
If you then have your categories named as per your groups you can do a redirect to http:// + base_url + $group therfore removing the need to explicitly work out what category page to load.
In order to redirect the customer as per customer group, there is a commercial extension:
http://www.magepsycho.com/custom-login-redirect-pro.html
really worths trying. FYI many merchants are using this module for their magento sites.
Happy e-commerce!!
Regards

Magento 1.4 paypal bug

i try to get the paypal payment run in my magento 1.4 but there is a serious problem with the workflow. after i select paypal and get routed to the paypal account to send the money you normally come back automatically into the magento shop to finish the order, but in my case magento tells you there is aproblem with the adress field. paypal does'nt send the adress back to magento correctly:
Error: Please check shipping address information. Please enter last name.
is this a known bug or is there a patch or workaround?
please help!
thnx.
The error seems to be in /app/code/core/Mage/Paypal/Model/Api/Nvp.php. Looks like the vars aren't mapped well. Because I couldn't find the concrete error in this file, I did a bit dirty workaround in /app/code/core/Mage/Paypal/Model/Express/Checkout.php.
In 1.4.2 just replace the method returnFromPaypal() with the following code...
public function returnFromPaypal($token)
{
$this->_getApi();
$this->_api->setToken($token)
->callGetExpressCheckoutDetails();
// import billing address
$billingAddress = $this->_quote->getBillingAddress();
$exportedBillingAddress = $this->_api->getExportedBillingAddress();
// import shipping address
$exportedShippingAddress = $this->_api->getExportedShippingAddress();
if (!$this->_quote->getIsVirtual()) {
$shippingAddress = $this->_quote->getShippingAddress();
if ($shippingAddress) {
if ($exportedShippingAddress) {
foreach ($exportedShippingAddress->getExportedKeys() as $key) {
if('firstname' == $key || 'lastname' == $key){
continue;
} // if
$shippingAddress->setDataUsingMethod($key, $exportedShippingAddress->getData($key));
$billingAddress->setDataUsingMethod($key, $exportedShippingAddress->getData($key));
}
// Correct First- and Lastnames
list($_firstname, $_lastname) = explode(' ', $exportedShippingAddress->getData('firstname'));
$shippingAddress->setDataUsingMethod('firstname', $_firstname);
$billingAddress->setDataUsingMethod('firstname', $_firstname);
$shippingAddress->setDataUsingMethod('lastname', $_lastname);
$billingAddress->setDataUsingMethod('lastname', $_lastname);
$shippingAddress->setCollectShippingRates(true);
}
// import shipping method
$code = '';
if ($this->_api->getShippingRateCode()) {
if ($code = $this->_matchShippingMethodCode($shippingAddress, $this->_api->getShippingRateCode())) {
// possible bug of double collecting rates :-/
$shippingAddress->setShippingMethod($code)->setCollectShippingRates(true);
}
}
$this->_quote->getPayment()->setAdditionalInformation(self::PAYMENT_INFO_TRANSPORT_SHIPPING_METHOD, $code);
}
}
$this->_ignoreAddressValidation();
// import payment info
$payment = $this->_quote->getPayment();
$payment->setMethod($this->_methodType);
Mage::getSingleton('paypal/info')->importToPayment($this->_api, $payment);
$payment->setAdditionalInformation(self::PAYMENT_INFO_TRANSPORT_PAYER_ID, $this->_api->getPayerId())
->setAdditionalInformation(self::PAYMENT_INFO_TRANSPORT_TOKEN, $token)
;
$this->_quote->collectTotals()->save();
}
The modified code replaces the whole billing address with the shipping address and pushes the name given in $firstname into $firstname and $lastname.
not clean, but working. :-)
any luck finding a solution to this, I'm having the same issue.
--- update ---
I finally figured out what was happening on this one for me. I had the Custom Shipping Admin module installed and it was overriding the address controller that validates the order. I updated the overrided module to reflect the version of Magento I was on and it worked.. no problems. Hope this helps someone.

Resources