Community Commerce product data in emails

I have just started using the fantastic community commerce package and have a query about accessing product data. I have had a request to include the product description in the notification emails so I created the override order_receipt.php in application/mail to tweak the output but the $item object containing the ordered product data does not include the product description out of the box.

As the template is using the Product class I thought, much like accessing page properties, I could access a product using its id to get the product description…

$p = \Product::getByID($item->getID());
echo $p->getDesc();

but this says the class is not found?

As a workaround I have been able to update the src Product class to add the description to the $item object…

/**
* @ORM\return mixed
*/
public function getProductDescription()
{
    return $this->oiProductDescription;
}
/**
* @ORM\param mixed $oiProductDescription
*/
public function setProductDescription($oiProductDescription)
{
	$this->oiProductDescription = $oiProductDescription;
}
$productDescription = $product->getDesc();

which I can then add into the template as…

$item->getProductDescription();

but this is obviously not ideal as it is changing the core package files and subject to breaking when updating.

What would be the best way of accessing the product data within the order_receipt.php template?

Thanks, any pointers gratefully received!

On the order item object ($item), there’s a function available called getProductObject()
That’ll return the proper Product object, if it can still be found.

So you should be able to do something like this:

$product = $item->getProductObject();

if (is_object($product)) {
    $desc = $product->getDescription();
    echo $desc;
}

The way you were approaching it initially was sort of on the right track, but; you were using the ID of the order item, not the product, and the namespace of Product isn’t at the root level. So you would have have needed do to this:

$p = \Concrete\Package\CommunityStore\Src\CommunityStore\Product::getByID($item->getProductID());
if (is_object($p) {
    echo $p->getDescription();
}

That’s really just what getProductObject is doing, so either way does the same thing.

That is great - certainly a lot easier than I was trying to make it!

Thanks so much for taking the time to look at this, it works perfectly. Excellent explanation too so I can see exactly where I was going wrong and will be really helpful in future.

Thanks.
Paul