2

In the Invoice PDF I can get (logged in) customer account data with the following code:

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

If I try to use this code in the shipment PDF (Mage/Sales/Model/Order/Pdf/Shipment.pdf), the customer data remains empty.

Given the customer email in $order, how can I get the account data in the shipment PDF?

2 Answers 2

3
+50

I think you can use $order->getCustomerId() instead of getCustomerEmail. Then you can just load the customer like this:

$customerId = $order->getCustomerId();
if ($customerId) { //the order was not placed as guest
    Mage::getModel('customer/customer')->load($customerId);
}
else {
    //order was placed as guest.
}

Doing it like this, you won't need to set a website id to the customer model before loading it.

1

Since the PDF is created from the backend the current Website ID will be 0 (admin). Depending on in what method you are trying to use your code snippet you might want to do something like this:

function getPdf

$customer_email = $order->getCustomerEmail();
$customer = Mage::getModel("customer/customer"); 
$customer->setWebsiteId($shipment->getStore()->getWebsite()->getId()); 
$customer->loadByEmail($customer_email);

I'm using the getStore() data from the shipment object. This way it gets the store the order was placed from.

3
  • On the third line I get the following error: Fatal error: Call to a member function getId() on a non-object. As @Marius solutions works, this is not relevant to me anymore, but still wanted to give feedback.
    – SPRBRN
    May 7, 2014 at 15:20
  • @SPRBRN thank you for the feedback. Ill look into it later on to correct the answer May 7, 2014 at 15:45
  • @SanderMangel. Fixed it. It was getWebsiteId() but should be getWebsite()
    – Marius
    May 15, 2014 at 2:35

Your Answer

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

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