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?

SPRBRN
  • 1,307
  • 6
  • 19
  • 33

2 Answers2

3

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.

Marius
  • 197,939
  • 53
  • 422
  • 830
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.

Marius
  • 197,939
  • 53
  • 422
  • 830
Sander Mangel
  • 37,528
  • 5
  • 80
  • 148
  • 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 07 '14 at 15:20
  • @SPRBRN thank you for the feedback. Ill look into it later on to correct the answer – Sander Mangel May 07 '14 at 15:45
  • @SanderMangel. Fixed it. It was getWebsiteId() but should be getWebsite() – Marius May 15 '14 at 02:35