Jump to content

Extend invoice details


Simone Salerno

Recommended Posts

Hi everyone,

I struggled to find a way to add custom order details (products tags) in invoices' PDF, so I want to share with you my achivements, in hope you'll find them useful.

 

The key principle is that you override OrderInvoiceCore class. Here's my code, but you can easily build on it to do whatever you need:

 

file: override/classes/OrderInvoice.php

class OrderInvoice extends OrderInvoiceCore {
    
    //override parent behavior
    public function getProducts($products = false, $selectedProducts = false, $selectedQty = false) {
        $result = parent::getProducts($products, $selectedProducts, $selectedQty);
        
        foreach ($result as &$product) {
            $product['tags'] = $this->fetchTags($product['product_id']);
        }
        
        return $result;
    }
    
    /**
     * Fetch tags for product
     * @param int $product_id
     * @return string
     */
    protected function fetchTags($product_id) {
        //don't know if there's a Tag class to do this
        $tags = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
        SELECT tag.name
        FROM `'._DB_PREFIX_.'product_tag` ptag
        LEFT JOIN `'._DB_PREFIX_.'tag` tag
        ON ptag.id_tag = tag.id_tag
        WHERE ptag.`id_product` = '.(int)$product_id);

        //implode theme together
        return implode(',', array_map(function($tag) { return $tag['name']; }, $tags));
    }
}

Now you can access {$order_details.tags} in your invoice.tpl file

Edited by SimoneS93 (see edit history)
Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...