No default customer addresses returned in magento - php

I'm pulling customer addresses out of magento with
$customers = Mage::getModel('customer/customer')->getCollection();
foreach($customers as $customer){
$primary[$i] = $customer->getPrimaryAddresses();
}
The problem is I never get any addresses from that function. I can get the customers addresses via $customer->getAddresses() or $customer->getAdditionalAddresses(); but no primary addresses. I've also tried the getDefaultShippingAddress / getDefaultBillingAddress functions neither of which return any addresses.
Getting a bit annoyed now I just can't figure out whats going on. In the Magento backend I can see which address is set to default/primary so I know the address has been set.

Just leaving this here for the next guy/girl...
As of 1.8
getPrimaryAddresses calls getPrimaryBillingAddress and getPrimaryShippingAddress which calls get getPrimaryAddress with the attribute code default_shipping or default_billing
So either the address is not set as the default or..
Its also possible that the customer (or admin) placed the order but did not save the address to their account (guest). So the getPrimaryBillingAddress method (called by getPrimaryAddresses) of the customer object will return null as the address is persisted in the order.

Try this
$customers = Mage::getModel('customer/customer')->getCollection();
foreach($customers as $customer){
$addressId = $customer->getDefaultBilling();
if ($addressId){
$address = Mage::getModel('customer/address')->load($customerAddressId);
$htmlAddress = $address->format('html');
}
}
You can find more information here http://www.magentocommerce.com/boards/viewthread/71270/

Related

API/SDK Checkout.com - Set Phone and Shipping Address to a payment

I'm using Checkout.com and its PHP SDK to set up payment on a website.
I try to set the Phone and The Shipping Address, but i'm doing wrong i suppose.
The API is not so amazing about this as well (or i don't know how to use it to find what i want)
I did something like this for the phone but doesn't work
$customerPhone = new \Checkout\Models\Phone();
$customerPhone->number = $phone;
$payment->shipping = new \stdClass();
$payment->shipping->phone = $customerPhone;
And i have no idea for the address.
If someone knows ?
Thanks
The correct way of sending shipping data on payments is:
$shipping = new \Checkout\Models\Payments\Shipping($address, $phone);
Where $address is an object of Checkout\Models\Address and $phone is an object of Checkout\Models\Phone.

add tracking number only to specific shipping method

hello based on this thread: add tracking number automatically
i have managed to add tracking numbers to orders when button 'ship' is pressed
but the question is can i make somehow check before adding tracking number? cause i want to add tracking number only to specific shipping method (carrier)
how can i do that?
i have tried to add if statement before adding tracking number like this:
public function salesOrderShipmentSaveBefore($observer)
{
$rate = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingRatesCollection();
$method = $rate->getCarrier();
if ($method == 'mycompany_mycarrier'){
$trackNumber='123456789';
$shipment = $observer->getEvent()->getShipment();
$track = Mage::getModel('sales/order_shipment_track')
->setNumber($trackNumber)
->setCarrierCode('mycompany_mycarrier')
->setTitle('My Carrier');
$shipment->addTrack($track);
}
but when i press ship button error says that i am calling undefined method - Mage_Sales_Model_Resource_Quote_Address_Rate_Collection::getCarrier()
maybe there is other way how can i check that it is my carrier and then add tracking number; cause this code adds tracking number but to all orders, all i want is that it add tracking number to my own created shipping method
any help would be great
// After Order Place event
$iOrderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$oOrder = Mage::getModel('sales/order')->loadByIncrementId($iOrderId);
$oOrder->getShippingDescription();
Does the method even exist?
It tells you that you are missing a piece of code.
solved my problem, the Observer.php:
public function salesOrderShipmentSaveBefore($observer)
{
$trackNumber='987654321';
$shipment = $observer->getEvent()->getShipment();
$order = $shipment->getOrder();
$orderIncrementId=$order->getIncrementId();
$oOrder = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
$shipping = $oOrder->getShippingDescription();
if($shipping =='My Carrier - My Method')
{
$track = Mage::getModel('sales/order_shipment_track')
->setNumber($trackNumber)
->setCarrierCode('mycompnay_mycarrier')
->setTitle('My Carrier');
$shipment->addTrack($track);
}
}
now everything works just like i wanted, the tracking number is added to my shipping carrier only.
Thank you Kingshuk Deb, you were right since the begining, i just didnt understood that.

How to get customer profile using CIM in Authorize.Net?

I am working of CIM (Customer information manager) and i have created customer profile using CIM function. But i want to get customer profile using customer id instead of customer profile id.
$cim = new AuthnetCIM('***MASKED***', '***MASKED***', AuthnetCIM::USE_DEVELOPMENT_SERVER);
$cim->setParameter('email', 'fakeemail#example.com');
$cim->setParameter('description', 'Profile for Joe Smith'); // Optional
$cim->setParameter('merchantCustomerId', '7789812');
//create profile function
$ss=$cim->createCustomerProfile();
//and get profile by..
$profile_id = $cim->getProfileID();
You can't. You can only get the profile using the profile ID. This means you'll want to store that ID in your database and associate it with the customer's record so whenever you need to get their profile you know what their Profile ID is.
Actually it is possible if you must, however I would still recommend storing it if possible, but this alternative might be of help.
Authorize.Net defines a unique customer profile by the composite key (Merchant Customer Id, Email, and Description), thus you must ensure this is unique. The CreateCustomerProfile(..) API method will enforce uniqueness and return an error if you attempt to create the same composite key again, as it should. However, the message in this response will contain the conflicting customer profile id, and since your composite key is unique, and Authorize.Net enforces uniqueness of this composite key, then this must be the Authorize.Net customer profile id of your customer.
Code sample in C#
private long customerProfileId = 0;
var customerProfile = new AuthorizeNet.CustomerProfileType()
{
merchantCustomerId = "123456789",
email = "user#domain.com",
description = "John Smith",
};
var cpResponse = authorize.CreateCustomerProfile(merchantAuthentication, customerProfile, ValidationModeEnum.none);
if (cpResponse.resultCode == MessageTypeEnum.Ok)
{
customerProfileId = cpResponse.customerProfileId;
}
else
{
var regex = new Regex("^A duplicate record with ID (?<profileId>[0-9]+) already exists.$", RegexOptions.ExplicitCapture);
Match match = regex.Match(cpResponse.messages[0].text);
if (match.Success)
customerProfileId = long.Parse(match.Groups["profileId"].Value);
else
//Raise error.
}

Add custom attribute in order email templates - Magento

I have created a 'Companyname' attribute which gets added up in my Customer's Account information and is a required field.
It gets filled up on registration, form and edit pages fine and gets displayed on Customer's Grid in the back-end too.
However I am not able to display the Company name in any of my order email templates.
I believe this is because there is neither any column called 'companyname' in my order tables nor do I have any custom variable which I can pass to order/invoice/shipment templates to display Company name right next to the line after Customer's name.
Can any one point out the file where I can create this custom variable containing my custom 'companyname' attribute and pass it to all types of sales email templates
Thanks
After a little bit of searching I found the right file to make the changes. Since I already had 'companyname' as one of my attributes I retrieved the value of this field and passed it as a param in the following function
app/code/core/Mage/Sales/Model/Order.php
public function sendNewOrderEmail()
{
/*Existing Code*/
if ($this->getCustomerIsGuest()) {
$templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $storeId);
$customerId = Mage::getModel('customer/customer')->load($this->getCustomerId());
$companyname = $customerId->getCompanyname();
$customerName = $this->getBillingAddress()->getName();
} else {
$templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $storeId);
$customerId = Mage::getModel('customer/customer')->load($this->getCustomerId());
$companyname = $customerId->getCompanyname();
$customerName = $this->getCustomerName();
}
/*Existing Code*/
$mailer->setTemplateParams(array(
'order' => $this,
'billing' => $this->getBillingAddress(),
'payment_html' => $paymentBlockHtml,
'companyname' => $companyname
));
/*Rest of the code remains the same*/
}
After making this change. I edited my Transactional Email to include this param. Since I wanted to display inside Shipping Address, I placed my variable just before this line in
System > Transactional Emails > New Order Email
{{ var companyname }}
{{var order.getShippingAddress.format('html')}}
If your companyname is getting saved as a part of Customer Information then this would get displayed in your Order Email in 'Shipping Address' Information right at the Start.
You can do the same for Invoice and Shipment Emails.
Hope this helps someone !!! :-)

How to mark an order failed programmatically - Magento

I would just like to know what is that one property which when set would mark the order as failed so that failureAction() of app/code/core/Mage/Checkout/controller/OnepageController.php gets called.
I have an Observer.php where in I'm creating invoice for successful payments and for failed payments saving order with pending_payment status and wanting to redirect them to the cart page with an error message at the top.
All this happens fine. Just that for unsuccessful / failed payments, along with saving the order with pending_payment status n redirecting them to cart page with error message, I would also like to retain/save the cart from getting empty.
But to no luck
Observer.php
public function implementOrderStatus($event)
{
$order = $event->getEvent()->getOrder();
if ($this->_getPaymentMethod($order) == 'mypaymentmodule')
{
$quote = Mage::getModel('sales/quote')->load($order->getQuoteId());
if($order->getPayment()->getCcTransId() == NULL)
{
$order->cancel();
$order->setStatus('canceled');
$order->save();
$quote->setIsActive(1)->save();
/*$state = 'pending_payment';
$status = 'pending_payment';
$comment = 'Payment transaction failed due to incorrect AVS/CVD details.';
$isNotified = false;
$order->setState($state,$status,$comment,$isNotified)->save();
$order->setCanSendNewEmailFlag(false);*/
Mage::getSingleton('checkout/session')->addError('Sorry, either of your card information (billing address or card validation digits) dint match. Please try again');
Mage::app()->getFrontController()->getResponse()->setRedirect('checkout/cart/')->sendResponse();
}
else
{
if ($order->canInvoice())
$this->_processOrderStatus($order);
}
}
return $this;
}
But $quote->setIsActive(true)->save() does not seem to be doing the trick. Any help as to how can I save my cart from getting empty after saving the order with 'canceled' status.
You maybe should have a look at ./app/code/core/Mage/Sales/Model/Order.php. There you will find several constants for an order, which may be used to set the state of an order like this:
<?php
// [...] all your code within your custom action or script
//load your order by order_id (or by increment_id, if you like to, here, it's your order id
$order = Mage::getModel('sales/order')->load($your_order_id);
$order->setState(Mage_Sales_Model_Order::STATE_CANCELED); //or whatever distinct order status you'd like
$order->save();
The failureAction in the controiller action does nothing of the sort, if you want to call it manually, you can build it's url using Mage::getUrl()

Categories